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 UPDATE CASCADE
);
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (10, 1);
UPDATE parent SET id = 2 WHERE id = 1;

What will be the value of parent_id in the child table after the update?
A2
BUpdate fails with error
CNULL
D1
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON UPDATE CASCADE effect

    ON UPDATE CASCADE updates child foreign keys automatically when parent keys change.
  2. Step 2: Apply update to parent and child

    Parent id changes from 1 to 2, so child.parent_id updates from 1 to 2 automatically.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    ON UPDATE CASCADE updates child keys = 2 [OK]
Quick Trick: CASCADE updates child keys to new parent key value [OK]
Common Mistakes:
MISTAKES
  • Expecting child key to stay the same
  • Thinking update causes error
  • Assuming child key becomes NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes