CS FundamentalsVery hardGATE CS · Amazon/Microsoft SDE MCQ

P(id PRIMARY KEY) has rows 1, 2. C(id, pid REFERENCES P ON DELETE CASCADE) has (10, 1), (11, 1), (12, 2). G(id, cid REFERENCES C ON DELETE SET NULL) has (100, 10), (101, 10), (102, 11), (103, 12), (104, NULL). After DELETE FROM P WHERE id = 1 (foreign keys enforced), how many rows of G have a NULL cid?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 1

Correct answer: B. 4

Deleting P 1 cascades to C rows 10 and 11. That fires SET NULL in G for 100, 101 and 102, which joins the already-NULL 104: 4 NULLs, and no G row is deleted. Thinking the cascade continues into G would delete rows instead.

More DBMS and SQL questions

Exams that ask this topic