Databases¶
ON DELETE Constraints¶
child table defines a foreign key to reference a parent table
- CASCADE: If a row is deleted from the parent table, the corresponding row in the child table (where the foreign key relationship is defined) will also be deleted automatically.
- RESTRICT: If you attempt to delete a row from a parent table while the corresponding row in the child table still exists, the delete operation will be prevented. In this case, you need to delete all the child rows before deleting the parent row.
E.g. Student -> Enrollment <- Class
- Student and Class are the parent tables, Enrollment is a child table to both (with two foreign key relationships defined, one for each parent table)
- CASCADE: If a row in Student was deleted, all corresponding rows in Enrollment would also be deleted
- RESTRICT: If you try to delete a row in Class but there are still Students enrolled (i.e. there are child rows in Enrollment), the DELETE operation will be prevented. You would need to delete all the child rows in Enrollment first