0
0
SQLquery~10 mins

Foreign key ON UPDATE behavior in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Foreign key ON UPDATE behavior
Start: Update parent key
Check foreign key constraint
ON UPDATE CASCADE
Update child keys
Commit changes
End
When a parent key is updated, the database checks the foreign key rule and applies the ON UPDATE action to child rows accordingly.
Execution Sample
SQL
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
);

UPDATE parent SET id = 2 WHERE id = 1;
This code creates two tables with a foreign key and updates the parent key to show ON UPDATE CASCADE effect.
Execution Table
StepActionParent.id BeforeParent.id AfterChild.parent_id BeforeChild.parent_id AfterNotes
1Initial state1111Parent and child have id=1
2Update parent.id from 1 to 21211Parent id updated, child not yet updated
3Check foreign key ON UPDATE CASCADE2212Child.parent_id updated to 2 to match parent
4Commit changes2222Update complete, foreign key constraint maintained
5End2222Execution finished
💡 Update completes successfully with child keys updated due to ON UPDATE CASCADE
Variable Tracker
VariableStartAfter Step 2After Step 3Final
parent.id1222
child.parent_id1122
Key Moments - 2 Insights
Why does child.parent_id change after updating parent.id?
Because the foreign key is defined with ON UPDATE CASCADE, the child rows automatically update their foreign key to match the new parent key, as shown in step 3 of the execution_table.
What would happen if ON UPDATE SET NULL was used instead of CASCADE?
The child.parent_id would be set to NULL after the parent.id update, instead of being updated to the new value. This behavior differs from the CASCADE action shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of child.parent_id after step 2?
A1
B2
CNULL
DUndefined
💡 Hint
Check the 'Child.parent_id After' column in row for step 2.
At which step does the child.parent_id get updated to match the new parent.id?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'Child.parent_id After' changes from 1 to 2.
If the foreign key used ON UPDATE SET NULL, what would child.parent_id be after the update?
A1
BNULL
C2
DUnchanged
💡 Hint
Refer to key_moments explanation about ON UPDATE SET NULL behavior.
Concept Snapshot
Foreign key ON UPDATE behavior:
- When a parent key changes, child keys react based on ON UPDATE rule.
- ON UPDATE CASCADE: child keys update to new parent key.
- ON UPDATE SET NULL: child keys become NULL.
- Ensures referential integrity automatically.
- Defined in FOREIGN KEY constraint syntax.
Full Transcript
This visual execution shows how foreign key ON UPDATE behavior works in SQL. When a parent table's key is updated, the database checks the foreign key constraint on the child table. If ON UPDATE CASCADE is set, child rows update their foreign keys to match the new parent key, maintaining integrity. The execution table traces each step: initial state, parent update, child update, and commit. Key moments clarify why child keys change and what would happen with different ON UPDATE actions. The quiz tests understanding of these steps and effects. This helps beginners see how foreign keys keep data consistent when parent keys change.