Concept · Designing databases

ON UPDATE CASCADE with FOREIGN KEY

ON UPDATE CASCADE updates child references when the parent row key changes.

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.

SQL verified in SQLite
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.

Practical decision
ScenarioTypical approachReason
A natural key that may be correctedConsider it ON UPDATE CASCADEAvoids leaving references pointing at the old value
A stable technical idUsually never change itA surrogate key is normally designed to keep the identity stable
The change represents a different entityDo not use a cascade to “rename” itIt 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.

Stable technical key
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.

Referential actions on update
ActionIdea
CASCADECopies the parent's new value into the child references
NO ACTION / RESTRICTRejects the change if it would leave invalid references; exactly when it is checked can vary
SET NULLSets the reference to NULL if the column allows it
SET DEFAULTUses 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.