CASCADE
A foreign key action that deletes child rows automatically when their parent row is deleted.
A single delete can remove many dependent rows with no warning.
In more detail
Deleting one basket with ON DELETE CASCADE also deletes all its lines. That is right for parts that cannot exist alone and dangerous for valuable records, because one DELETE can silently remove far more rows than it names.
Example
basket_id INTEGER REFERENCES basket(id) ON DELETE CASCADERelated terms
- referential action — The rule, declared on a foreign key, for what happens to child rows when the parent row they point at is deleted or changed.
- foreign key — A column holding another table's primary key value, which the database guarantees points at a row that exists.
- DELETE statement — The SQL statement that removes rows from a table, choosing them with WHERE.
- soft delete — Marking a row as deleted, for example by setting a deleted_at date, instead of removing it.
Taught in
- Keys, Constraints And Referential Integrity — PostgreSQL, Indexing & Relational Data Modeling
- Updating And Deleting Safely — PostgreSQL, Indexing & Relational Data Modeling