← Back to SQL, page 9

sql-0177 · Medium mid

Compare ON DELETE CASCADE, SET NULL and RESTRICT. Which would you choose for order lines, and which for an employee's manager?

CASCADE deletes child rows with the parent, SET NULL clears the reference, and RESTRICT (or the default NO ACTION) blocks the delete while children exist. Order lines belong to their order, so CASCADE fits. An employee should not vanish when their manager leaves, so SET NULL or RESTRICT fits, with reassignment handled explicitly.

Full answer
sql
PRAGMA foreign_keys = ON;
CREATE TABLE orders (id INTEGER PRIMARY KEY);
CREATE TABLE lines (order_id INTEGER REFERENCES orders(id) ON DELETE CASCADE, product TEXT);
CREATE TABLE emp (id INTEGER PRIMARY KEY, manager_id INTEGER REFERENCES emp(id) ON DELETE SET NULL);
INSERT INTO orders VALUES (1), (2);
INSERT INTO lines VALUES (1, 'Pen'), (1, 'Bag'), (2, 'Lamp');
INSERT INTO emp VALUES (10, NULL), (11, 10);
DELETE FROM orders WHERE id = 1;
DELETE FROM emp WHERE id = 10;
SELECT 'lines' AS t, COUNT(*) AS n FROM lines UNION ALL SELECT 'emp 11 manager', manager_id FROM emp WHERE id = 11;
tn
lines1
emp 11 managerNULL

Deleting order 1 removed its lines. Deleting manager 10 kept employee 11 and cleared the link.

RESTRICT prevents the delete instead:

sql
PRAGMA foreign_keys = ON;
CREATE TABLE cust (id INTEGER PRIMARY KEY);
CREATE TABLE inv (id INTEGER PRIMARY KEY, cust_id INTEGER REFERENCES cust(id) ON DELETE RESTRICT);
INSERT INTO cust VALUES (1);
INSERT INTO inv VALUES (100, 1);
DELETE FROM cust WHERE id = 1;
-- error on purpose
text
Error: FOREIGN KEY constraint failed

Guidelines that hold up in practice:

  • CASCADE for parts that have no meaning alone (order lines, comment attachments).
  • SET NULL for optional relationships (manager, assigned reviewer).
  • RESTRICT for references that must be resolved by a person first (a customer with invoices).

CASCADE chains can delete far more than expected; review every path before using it on central tables.

sql
PRAGMA foreign_keys = ON;
CREATE TABLE orders (id INTEGER PRIMARY KEY);
CREATE TABLE lines (order_id INTEGER REFERENCES orders(id) ON DELETE CASCADE, product TEXT);
CREATE TABLE emp (id INTEGER PRIMARY KEY, manager_id INTEGER REFERENCES emp(id) ON DELETE SET NULL);
INSERT INTO orders VALUES (1), (2);
INSERT INTO lines VALUES (1, 'Pen'), (1, 'Bag'), (2, 'Lamp');
INSERT INTO emp VALUES (10, NULL), (11, 10);
DELETE FROM orders WHERE id = 1;
DELETE FROM emp WHERE id = 10;
SELECT 'lines' AS t, COUNT(*) AS n FROM lines UNION ALL SELECT 'emp 11 manager', manager_id FROM emp WHERE id = 11;

-- error on purpose
PRAGMA foreign_keys = ON;
CREATE TABLE cust (id INTEGER PRIMARY KEY);
CREATE TABLE inv (id INTEGER PRIMARY KEY, cust_id INTEGER REFERENCES cust(id) ON DELETE RESTRICT);
INSERT INTO cust VALUES (1);
INSERT INTO inv VALUES (100, 1);
DELETE FROM cust WHERE id = 1;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • CASCADE deletes dependent rows
  • SET NULL keeps children and clears the reference
  • RESTRICT or NO ACTION blocks the delete
  • Choose by whether the child makes sense without the parent

Common mistakes

  • Using CASCADE on a central table and losing history
  • Using SET NULL on a NOT NULL column, which then fails

Follow-ups

  • When do RESTRICT and NO ACTION behave differently?
  • How would you soft-delete instead?

Tips

  • Tie each option to a concrete relationship