CS FundamentalsHardGATE CS (PYQ pattern)

Tables: P(id); C(id, p) with p referencing P(id) ON DELETE CASCADE; G(id, c) with c referencing C(id) ON DELETE SET NULL. Rows: P = {1, 2}; C = (10,1), (11,1), (12,2); G = (100,10), (101,10), (102,11), (103,12). After DELETE FROM P WHERE id = 1, how many rows remain in C and in G, and how many G rows have c = NULL?

  1. A. C: 1 row, G: 1 row, 0 with NULL
  2. B. C: 1 row, G: 4 rows, 3 with NULL
  3. C. C: 3 rows, G: 4 rows, 0 with NULL
  4. D. C: 1 row, G: 2 rows, 1 with NULL

Correct answer: B. C: 1 row, G: 4 rows, 3 with NULL

Deleting P(1) cascades to C rows 10 and 11, leaving only (12,2). Deleting those C rows triggers the SET NULL action on G: rows 100, 101, 102 keep existing but get c = NULL; row 103 is unchanged. So G keeps all 4 rows, 3 with NULL.

More DBMS and SQL questions

Exams that ask this topic