Bird
0
0

Consider these tables:

medium📝 query result Q5 of 15
SQL - Table Constraints
Consider these tables:
CREATE TABLE parent (id INT PRIMARY KEY);
CREATE TABLE child (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL);
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (10, 1);
DELETE FROM parent WHERE id = 1;
What is the value of parent_id in the child row after the delete?
A1
BNULL
C0
DThe row is deleted
Step-by-Step Solution
Solution:
  1. Step 1: Recall ON DELETE SET NULL behavior

    When parent row is deleted, child foreign key is set to NULL.
  2. Step 2: Apply to the child row

    Child row's parent_id changes from 1 to NULL after parent deletion.
  3. Final Answer:

    parent_id is set to NULL in the child row. -> Option B
  4. Quick Check:

    ON DELETE SET NULL sets foreign key to NULL [OK]
Quick Trick: SET NULL clears foreign key on parent delete [OK]
Common Mistakes:
MISTAKES
  • Assuming child row is deleted
  • Thinking foreign key remains unchanged
  • Confusing NULL with zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes