Foreign key ON UPDATE behavior controls what happens to related data when the original data changes. It helps keep data connected and consistent.
Foreign key ON UPDATE behavior in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
FOREIGN KEY (child_column) REFERENCES parent_table(parent_column) ON UPDATE {CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION}
CASCADE updates child rows automatically when parent changes.
RESTRICT or NO ACTION prevent update if child rows exist.
Examples
SQL
FOREIGN KEY (department_id) REFERENCES departments(id) ON UPDATE CASCADE
SQL
FOREIGN KEY (category_id) REFERENCES categories(id) ON UPDATE SET NULL
SQL
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE RESTRICT
Sample Program
This example shows how updating the department id in the departments table automatically updates the department_id in employees because of ON UPDATE CASCADE.
SQL
CREATE TABLE departments ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department_id INT, FOREIGN KEY (department_id) REFERENCES departments(id) ON UPDATE CASCADE ); INSERT INTO departments VALUES (1, 'Sales'); INSERT INTO employees VALUES (101, 'Alice', 1); -- Update department id from 1 to 10 UPDATE departments SET id = 10 WHERE id = 1; -- Check employees table SELECT * FROM employees;
Important Notes
ON UPDATE behavior only works if the database supports it and the foreign key is defined with it.
CASCADE is useful but can cause unexpected changes if not used carefully.
RESTRICT and NO ACTION are similar; they stop updates if related rows exist.
Summary
ON UPDATE controls what happens to child rows when parent keys change.
Common options: CASCADE, SET NULL, RESTRICT, NO ACTION.
Use ON UPDATE to keep data consistent automatically.
Practice
1. What does the
ON UPDATE CASCADE option do in a foreign key constraint?easy
Solution
Step 1: Understand ON UPDATE CASCADE behavior
ON UPDATE CASCADE means if the parent key changes, the child foreign keys update automatically to match.Step 2: Compare options with definition
Only Automatically updates child rows when the parent key changes. describes automatic update of child rows on parent key change.Final Answer:
Automatically updates child rows when the parent key changes. -> Option BQuick Check:
ON UPDATE CASCADE = automatic child update [OK]
Hint: CASCADE means child rows change with parent keys [OK]
Common Mistakes:
- Confusing CASCADE with DELETE behavior
- Thinking CASCADE prevents updates
- Mixing SET NULL with CASCADE
2. Which of the following is the correct syntax to add a foreign key with ON UPDATE SET NULL in SQL?
easy
Solution
Step 1: Identify correct ON UPDATE syntax
The syntax for foreign key with ON UPDATE SET NULL is: FOREIGN KEY (...) REFERENCES ... ON UPDATE SET NULL.Step 2: Check options for ON UPDATE SET NULL
FOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE SET NULL matches the correct syntax exactly. Others either use ON DELETE or different actions.Final Answer:
FOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE SET NULL -> Option AQuick Check:
ON UPDATE SET NULL syntax = FOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE SET NULL [OK]
Hint: ON UPDATE SET NULL sets child keys to NULL on parent update [OK]
Common Mistakes:
- Using ON DELETE instead of ON UPDATE
- Mixing CASCADE and SET NULL in syntax
- Omitting REFERENCES keyword
3. Given these tables:
What will be the value of
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?medium
Solution
Step 1: Understand ON UPDATE CASCADE effect
ON UPDATE CASCADE updates child foreign keys automatically when parent keys change.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.Final Answer:
2 -> Option AQuick Check:
ON UPDATE CASCADE updates child keys = 2 [OK]
Hint: CASCADE updates child keys to new parent key value [OK]
Common Mistakes:
- Expecting child key to stay the same
- Thinking update causes error
- Assuming child key becomes NULL
4. You have this foreign key:
What happens if you try to update a parent
FOREIGN KEY (child_id) REFERENCES parent(id) ON UPDATE RESTRICT
What happens if you try to update a parent
id that is referenced by a child row?medium
Solution
Step 1: Understand ON UPDATE RESTRICT behavior
ON UPDATE RESTRICT prevents updating parent keys if child rows reference them.Step 2: Apply update attempt on referenced parent key
Since child rows exist, update is blocked and error occurs.Final Answer:
The update is blocked and an error is raised. -> Option DQuick Check:
ON UPDATE RESTRICT blocks update if child exists [OK]
Hint: RESTRICT blocks parent key update if child exists [OK]
Common Mistakes:
- Confusing RESTRICT with CASCADE
- Expecting child keys to update automatically
- Thinking child keys become NULL
5. Consider these tables:
What will be the value of
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 SET NULL
);
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, and why?hard
Solution
Step 1: Understand ON UPDATE SET NULL behavior
ON UPDATE SET NULL sets child foreign keys to NULL when the parent key changes.Step 2: Apply update to parent and child
Parent id changes from 1 to 2, so child.parent_id is set to NULL automatically.Final Answer:
NULL, because ON UPDATE SET NULL sets child keys to NULL on parent update. -> Option CQuick Check:
ON UPDATE SET NULL sets child keys NULL on parent update [OK]
Hint: SET NULL sets child keys to NULL on parent key change [OK]
Common Mistakes:
- Confusing SET NULL with CASCADE
- Expecting child keys to update to new parent id
- Assuming update causes error
