On Delete Cascade — SQL Technical Interview Questions & Answers

On Delete Cascade

On Delete Cascade

  • FOREIGN KEY (roll) REFERENCES students(roll) ON DELETE CASCADE means deleting Asha deletes her marks rows too. ON DELETE SET NULL sets marks.roll to NULL (column must allow NULL). Default is reject the delete.
  • Use CASCADE only when the child has no meaning without the parent (marks without a student). Don’t CASCADE a student to a payments table you must keep for audit.
  • Trap: CASCADE on every FK ‘so DELETE works’.
  • On the example next to this theory: On Delete Cascade — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).

Technical Interview Tip: ON DELETE CASCADE vs SET NULL vs RESTRICT.

Code Examples for On Delete Cascade

On Delete Cascade — sample query

CREATE TABLE tags (id INTEGER PRIMARY KEY, label TEXT);
INSERT INTO tags VALUES (1, 'old'), (2, 'keep');
DELETE FROM tags WHERE label = 'old';
SELECT * FROM tags;

On Delete Cascade — dELETE removes matching rows. TRUNCATE clears a whole table faster (MySQL).

How to Explain On Delete Cascade to an Interviewer

CASCADE vs RESTRICT?

CASCADE deletes children RESTRICT blocks parent delete