Bird
0
0

Given these tables:

medium📝 query result Q13 of 15
SQL - Table Constraints
Given 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 RESTRICT);
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (10, 1);

What happens if you run DELETE FROM parent WHERE id = 1;?
AThe parent row is deleted, and the child row is also deleted.
BThe parent row is deleted, but the child row's parent_id is set to NULL.
CThe delete succeeds and leaves the child row with a dangling parent_id.
DThe delete fails because the child row exists and ON DELETE RESTRICT blocks it.
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON DELETE RESTRICT effect

    RESTRICT prevents deleting a parent row if any child rows reference it.
  2. Step 2: Apply to given data

    Since child row with parent_id=1 exists, deleting parent id=1 is blocked.
  3. Final Answer:

    The delete fails because the child row exists and ON DELETE RESTRICT blocks it. -> Option D
  4. Quick Check:

    ON DELETE RESTRICT blocks delete if children exist [OK]
Quick Trick: RESTRICT blocks delete if child rows exist [OK]
Common Mistakes:
MISTAKES
  • Assuming CASCADE behavior with RESTRICT
  • Thinking child foreign key becomes NULL
  • Believing delete always succeeds

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes