What are 'Cascading Actions' like ON DELETE CASCADE in foreign keys, and what are the risks of using them in a complex schema?
Last updated
Cascading actions make a foreign key propagate parent changes to child rows:
ON DELETE CASCADEdeletes children automatically (alternatives:SET NULL,RESTRICT).Risks: chained cascades can silently wipe many tables, the impact is invisible in the DELETE statement, and large deletes cause heavy locking. Multiple cascade paths can also error (notably in SQL Server).
Use cascades for tightly-owned children (e.g. order lines). Prefer
RESTRICTor soft deletes for shared or critical data.
- https://www.postgresql.org/docs/current/ddl-constraints.html
- https://learn.microsoft.com/en-us/sql/relational-databases/tables/primary-and-foreign-key-constraints?view=sql-server-ver17
- https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-1785-database-engine-error?view=sql-server-ver17
- https://www.bytebase.com/blog/sql-review-rule-explained-prohibit-cascade/
- https://medium.com/@Rohan_Dutt/10-techniques-to-handle-cascading-deletes-in-relational-databases-2584067502ec
- https://milanjovanovic.tech/blog/ef-core-cascade-delete