Bird
0
0

Consider these tables:

medium📝 query result Q4 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 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?
ANULL
B2
C1
DUpdate will fail
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ON UPDATE CASCADE effect

    The foreign key has ON UPDATE CASCADE, so child.parent_id updates when parent.id changes.
  2. Step 2: Apply update to child table

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

    2 -> Option B
  4. Quick Check:

    ON UPDATE CASCADE updates child keys = 2 [OK]
Quick Trick: ON UPDATE CASCADE changes child keys on parent update [OK]
Common Mistakes:
MISTAKES
  • Expecting child key to remain 1
  • Thinking update fails
  • Assuming child key becomes NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes