Foreign key ON UPDATE behavior in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a foreign key has an ON UPDATE rule, the database may need to update related rows automatically.
We want to understand how the time to update grows as the number of related rows increases.
Analyze the time complexity of this foreign key update behavior.
ALTER TABLE Orders
ADD CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES Customers(id)
ON UPDATE CASCADE;
UPDATE Customers
SET id = id + 100
WHERE id = 5;
This code sets a foreign key with ON UPDATE CASCADE, then updates a customer ID, causing related Orders rows to update.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating all rows in the child table (Orders) that reference the updated parent row.
- How many times: Once for each related row in Orders with the matching customer_id.
As the number of related Orders rows grows, the database must update more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates to Orders rows |
| 100 | 100 updates to Orders rows |
| 1000 | 1000 updates to Orders rows |
Pattern observation: The work grows directly with the number of related rows to update.
Time Complexity: O(n)
This means the update time grows linearly with the number of related rows that need updating.
[X] Wrong: "Updating the parent row is always a quick single operation regardless of child rows."
[OK] Correct: Because ON UPDATE CASCADE forces updates on all related child rows, the time depends on how many child rows exist.
Understanding how foreign key updates scale helps you reason about database performance and data integrity in real projects.
"What if the ON UPDATE rule was SET NULL instead of CASCADE? How would the time complexity change?"
Practice
ON UPDATE CASCADE option do in a foreign key constraint?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]
- Confusing CASCADE with DELETE behavior
- Thinking CASCADE prevents updates
- Mixing SET NULL with CASCADE
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]
- Using ON DELETE instead of ON UPDATE
- Mixing CASCADE and SET NULL in syntax
- Omitting REFERENCES keyword
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?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]
- Expecting child key to stay the same
- Thinking update causes error
- Assuming child key becomes NULL
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?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]
- Confusing RESTRICT with CASCADE
- Expecting child keys to update automatically
- Thinking child keys become NULL
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?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]
- Confusing SET NULL with CASCADE
- Expecting child keys to update to new parent id
- Assuming update causes error
