Before continuing: This entry is part of the constraints and keys guide. If you are new to the topic, start with CREATE TABLE.
Example
The example below uses readable codes so the effect is easy to see. In SQLite, foreign key enforcement is switched on explicitly before the tables are created.
PRAGMA foreign_keys = ON;
CREATE TABLE categories_update (
code TEXT PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE products_update (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
category_code TEXT NOT NULL,
FOREIGN KEY (category_code)
REFERENCES categories_update(code)
ON UPDATE CASCADE
);
INSERT INTO categories_update VALUES
('ELEC', 'Electronics'),
('HOG', 'Home');
INSERT INTO products_update VALUES
(1, 'Monitor', 'ELEC'),
(2, 'Keyboard', 'ELEC'),
(3, 'Lamp', 'HOG');
UPDATE categories_update
SET code = 'TEC'
WHERE code = 'ELEC';
SELECT id, name, category_code
FROM products_update
ORDER BY id;Only the two rows that referenced ELEC change to TEC. The lamp keeps HOG because its relationship does not depend on the updated key.
How it is used
In a foreign key relationship there is a referenced table (the parent) and a table that stores the reference (the child). If products.category_code points to categories.code, changing the parent's code can leave stale references in the products. With ON UPDATE CASCADE, the engine copies the new value into the related child rows.
What changes from case to case
When ON UPDATE CASCADE makes sense
The action is most useful when the referenced key can legitimately change and the change keeps the same logical identity. A business code that may need correcting, such as a category code, can be a reasonable case if many rows reference it.
| Scenario | Typical approach | Reason |
|---|---|---|
| A natural key that may be corrected | Consider it ON UPDATE CASCADE | Avoids leaving references pointing at the old value |
| A stable technical id | Usually never change it | A surrogate key is normally designed to keep the identity stable |
| The change represents a different entity | Do not use a cascade to “rename” it | It may be better to create a new row and reassign the relationships explicitly |
Do not add the cascade just because an UPDATE on the parent fails. That error may be telling you that you are trying to change an identity the model treats as stable.
Why a stable PRIMARY KEY reduces the problem
Many databases use a technical identifier, for example category_id, as the PRIMARY KEY and store the visible code in another column. That way the code can change without touching the key the relationships use.
CREATE TABLE categories (
id INTEGER PRIMARY KEY,
code TEXT UNIQUE NOT NULL,
name TEXT NOT NULL
);
-- Changing the code does not change the identity referenced by id.
UPDATE categories
SET code = 'TEC'
WHERE id = 10;This does not make surrogate keys compulsory everywhere; the decision depends on the domain. What matters is distinguishing between identity and attributes that can change.
ON UPDATE CASCADE compared with other actions
Foreign keys can define what happens when the parent key changes. The exact options vary by engine, but the most common ones express different decisions.
| Action | Idea |
|---|---|
CASCADE | Copies the parent's new value into the child references |
NO ACTION / RESTRICT | Rejects the change if it would leave invalid references; exactly when it is checked can vary |
SET NULL | Sets the reference to NULL if the column allows it |
SET DEFAULT | Uses the default value where the engine and the constraint allow it |
Not every combination is available in the same way on every engine. A value produced by SET NULL or SET DEFAULT must also still satisfy the other constraints.