Sub topic 10
Choosing ON DELETE and ON UPDATE Behavior
Prerequisites: you should understand tables, primary keys, foreign keys, and basic INSERT, UPDATE, and DELETE statements. In a foreign-key relationship, the parent table contains the referenced key. The child table stores a foreign key that points to it.
For example, orders.customer_id can reference customers.customer_id. The foreign key protects referential integrity, which means an order cannot point to a customer that does not exist. Referential actions decide what happens when the referenced parent row is deleted or its key changes.
Choosing an `ON DELETE` action
The ON DELETE clause controls what happens to child rows when their parent row is deleted.
| Action | Result | Suitable when |
|---|---|---|
| `RESTRICT` | Rejects the delete while child rows exist | Child records must remain valid |
| `NO ACTION` | Usually rejects the delete at statement or transaction checking time | You want the database default behavior |
| `CASCADE` | Deletes matching child rows automatically | Child rows have no meaning without the parent |
| `SET NULL` | Changes the foreign key to `NULL` | The child can exist without its former parent |
| `SET DEFAULT` | Replaces the key with its column default | A valid default parent is defined |
For orders, deleting a customer while orders still exist is often dangerous. Use RESTRICT or NO ACTION:
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
ON DELETE RESTRICT
);For order items, cascading deletion can be appropriate. An order item belongs only to one order, so deleting the order can delete its items:
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id)
REFERENCES orders (order_id)
ON DELETE CASCADE
);Choosing an `ON UPDATE` action
The ON UPDATE clause controls changes to the referenced parent key. Primary keys are usually stable, so ON UPDATE CASCADE is less commonly needed than ON DELETE CASCADE.
ON UPDATE RESTRICTrejects a parent-key change when children reference it.ON UPDATE CASCADEcopies the new key into matching child rows.ON UPDATE SET NULLorSET DEFAULTchanges child keys, subject to nullability and validity rules.
Common errors and dialect differences
SET NULL fails conceptually when the foreign-key column is NOT NULL. SET DEFAULT also requires the resulting value to reference an existing parent row. A cascade can travel through several related tables, so test the complete effect before using it.
RESTRICT and NO ACTION can differ in when the database checks the rule, especially with deferred constraints. Support for actions and their timing varies by database system. Some systems also require foreign-key enforcement to be enabled explicitly. Check your database documentation rather than assuming every engine behaves identically.
Exercise
An order item cannot exist without its order, but a customer must not be deleted while orders remain. Which delete actions should the two foreign keys use?
Show answer
Use ON DELETE CASCADE from order_items to orders, and ON DELETE RESTRICT or NO ACTION from orders to customers.