What is referential integrity, and can you explain the different On Delete actions like CASCADE, SET NULL, and RESTRICT?
Last updated
Referential integrity means a foreign key must match an existing parent key or be
NULL, preventing orphaned rows. It is enforced by aFOREIGN KEYconstraint.ON DELETE CASCADEdeletes matching child rows with the parent.SET NULLkeeps the child but nulls its FK (column must be nullable).RESTRICT/NO ACTIONblock deleting a parent that has children (RESTRICTchecks immediately,NO ACTIONcan defer to end of statement).
- https://seedfa.st/blog/referential-integrity
- https://medium.com/@sunnywilson.veshapogu/restrict-vs-cascade-vs-set-null-in-sql-choosing-the-right-foreign-key-rule-6d7c98484710
- https://www.postgresql.org/docs/current/ddl-constraints.html
- https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign-keys.html
- https://www.hackerone.com/blog/navigating-waters-foreign-key-constraints-role-update-and-delete
- https://www.dbvis.com/thetable/postgres-on-delete-cascade-a-guide/