Explain Codes LogoExplain Codes Logo

Sql ON DELETE CASCADE, Which Way Does the Deletion Occur?

sql
referential-integrity
database-schema
foreign-keys
Anton ShumikhinbyAnton Shumikhin·Aug 25, 2024
TLDR

Deletion with ON DELETE CASCADE cascades downward from the parent table to the child table. Upon deleting a parent row, linked child rows are also purged.

-- Deletes parent row plus linked child rows, clean as a polar bear sliding on ice DELETE FROM parent_table WHERE id = parent_id;

Remember the sequence: Parent row deletion leads to Child rows deletion.

CASCADE: the automatic cleaning tool

The ON DELETE CASCADE clause in SQL can be a valuable tool for maintaining referential integrity of your data. By deleting related rows in the child table when a parent row is deleted, it prevents orphaned rows that could skew your data analysis or application logic.

Schema design: the heart of it all

Designing your database schema properly is a bedrock principle. When creating tables and their relationships:

  1. Implement foreign keys wisely and use ON DELETE CASCADE for automatic cleanup of related data.
  2. Consider ON DELETE PROTECT to block deletion when foreign key references still exist.
  3. Use ON DELETE SET_NULL to clear the relationship when you don’t want to delete the record, only signify a loss of connection.

For instance, if you have a Courses table and a BookCourses table, you'd set a foreign key on BookCourses referencing Courses with ON DELETE CASCADE to ensure no orphaned course-book assignments remain if a course is deleted.

Beware: CASCADE is not always your friend

While ON DELETE CASCADE can be a savior, not all situations call for its use:

  • When your system needs to track historical data and maintain records even after a relationship ends.
  • When deletions should be explicit and involve a manual review for maintaining data integrity.
  • When a child row signifies precious data that you'd hate to lose inadvertently because a related parent was removed.

Consequences of CASCADE: Exploring key scenarios

It pays to deepen your understanding of ON DELETE CASCADE by investigating different scenarios:

CASCADE with multiple child tables

If you have multiple child tables linked to a parent table, deleting a row in the parent will cascade down to all child tables with ON DELETE CASCADE. A reckless delete can wash away a lot of data like a faulty Hoover Dam!

Self-referencing tables

With self-referencing tables (like an Employee table where a manager column refers back to the same table), setting up ON DELETE CASCADE needs extra care. A cascading delete here can be like a domino effect, removing all employees under that manager when the manager’s data is deleted.

Cyclic references

For cyclic references among tables, ensure cascades don't lead to unintended deletions or locks due to the tangled nature of the delete process - it can be like unwinding a Rubik's cube!

Constraints in the child table

If your child table has uniqueness constraints or other restrictions depending on certain rows, a cascade delete that removes those rows could leads to violations. This can cause havoc with your application logic, like a broken GPS!