Sub topic 9
Choosing `ON DELETE` and `ON UPDATE` Behavior
Assume you know CREATE TABLE, INSERT, UPDATE, DELETE, and NULL. This chapter focuses on the actions a foreign key takes when its referenced parent row changes.
A foreign key connects a child row to a parent row. For example, orders.customer_id can reference customers.customer_id. The database must decide what happens when someone deletes or changes the customer.
The main referential actions
A referential action is the rule after ON DELETE or ON UPDATE.
| Action | Effect on child rows | Typical use |
|---|---|---|
| `NO ACTION` | Rejects the statement if child rows would become invalid | Default, when deletion must be prevented |
| `RESTRICT` | Rejects the parent change immediately | Strict protection against deletion |
| `CASCADE` | Deletes or updates matching child rows | Dependent data with no independent meaning |
| `SET NULL` | Sets the foreign key to `NULL` | Optional relationships |
| `SET DEFAULT` | Uses the column's default value | A valid fallback parent exists |
NO ACTION and RESTRICT often behave the same in ordinary statements. In PostgreSQL, NO ACTION can be deferred until the transaction ends when the constraint is deferrable. RESTRICT checks immediately.
Choosing delete behavior
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
order_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id INTEGER NOT NULL,
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
ON DELETE RESTRICT
);
CREATE TABLE order_notes (
note_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id INTEGER NOT NULL,
note TEXT NOT NULL,
FOREIGN KEY (order_id)
REFERENCES orders (order_id)
ON DELETE CASCADE
);The orders rule prevents deleting a customer while orders refer to that customer. The order_notes rule deletes notes automatically when their order is deleted. This is appropriate when a note belongs only to that order.
Use SET NULL when the child should remain but the relationship is optional:
CREATE TABLE support_tickets (
ticket_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
agent_id INTEGER,
FOREIGN KEY (agent_id)
REFERENCES customers (customer_id)
ON DELETE SET NULL
);The foreign key must allow NULL. A required column cannot receive NULL.
Choosing update behavior
Primary keys usually do not change. Therefore, ON UPDATE NO ACTION is often sufficient. If a parent key can change and child values should follow it, use ON UPDATE CASCADE.
CREATE TABLE country_codes (
code CHAR(2) PRIMARY KEY
);
CREATE TABLE addresses (
address_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
country_code CHAR(2) NOT NULL,
FOREIGN KEY (country_code)
REFERENCES country_codes (code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);Changing country_codes.code updates matching addresses.country_code values. Prefer an immutable surrogate key when possible. It reduces the need for update cascades.
Exercise
An order must remain after its customer is removed, but it may no longer have a customer. Which action and column definition fit?
Show answer
Use ON DELETE SET NULL, and define orders.customer_id as nullable.