Bird
0
0

You wrote this SQL:

medium📝 Debug Q14 of 15
SQL - Table Constraints
You wrote this SQL:
ALTER TABLE child ADD CONSTRAINT fk_parent FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL;

But when you delete a parent row, the child row's parent_id does not become NULL. What is the likely problem?
AThe foreign key syntax is incorrect and ignored by the database.
BON DELETE SET NULL only works with CASCADE.
CThe <code>parent_id</code> column is NOT NULL, so it cannot be set to NULL.
DYou must delete child rows manually before deleting parent.
Step-by-Step Solution
Solution:
  1. Step 1: Check column nullability

    ON DELETE SET NULL requires the foreign key column to allow NULL values.
  2. Step 2: Identify the cause

    If parent_id is NOT NULL, the database cannot set it to NULL, so it leaves it unchanged.
  3. Final Answer:

    The parent_id column is NOT NULL, so it cannot be set to NULL. -> Option C
  4. Quick Check:

    SET NULL needs nullable foreign key column [OK]
Quick Trick: Foreign key must allow NULL for SET NULL to work [OK]
Common Mistakes:
MISTAKES
  • Assuming syntax error causes issue
  • Thinking SET NULL requires CASCADE
  • Believing manual delete is always needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes