Learn SQL
-- constraints.fk_actions_cascade

FK Actions: CASCADE / SET NULL / RESTRICT in SQL

Every CASCADE is a deletion blast radius to review

--concept

Foreign keys govern deletes: CASCADE deletes children with the parent (convenient, dangerous — one delete fans out); SET NULL orphans safely (child survives, FK nulled — column must be nullable); RESTRICT/NO ACTION blocks while children exist (safest default; RESTRICT checks immediately, NO ACTION at statement end). Default RESTRICT; CASCADE only for genuinely owned children (order lines, never users).

--how it works
1

CASCADE fans out

Children die with parent — order lines with orders. One delete removes N rows.

2

SET NULL orphans safely

Child survives unlinked — FK must be nullable. Profile survives user soft-delete.

3

RESTRICT blocks

Delete fails while children exist — safest default. RESTRICT immediate, NO ACTION at end.

4

Index + review

Unindexed FK slows parent deletes (child scans). Every CASCADE reviewed as blast radius.

--visualization
CASCADEOrder + 3 lines deleted atomically (owned only!)
Stage 1 / 1
Unindexed FK slows parent deletes (child scans) — index the child. Review every CASCADE.
--example
query.sql
1-- order lines die with their order:
2FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE
Result
actionmeaning
CASCADEchildren die with parent
SET NULLchildren survive unlinked
RESTRICTdelete blocked

Deleting an order with three lines removes four rows atomically under CASCADE. Under RESTRICT the same delete errors until lines are handled — the safe default for customer-owned data.

--variants

Lines die with order

Genuinely owned children only.

query.sql
1FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE
Result
actioneffect
delete order3 lines deleted too
1Ownership is total — lines meaningless without order.
2Atomic: parent + children commit together.
3Users/tenants never CASCADE — RESTRICT instead.

Users RESTRICT by default

Block, then handle explicitly.

query.sql
1FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE RESTRICT
Result
actionresult
delete customer with ordersERROR — handle orders first
1Engine refuses while children reference.
2App archives/migrates orders, then deletes.
3SET NULL alternative keeps orders unlinked if nullable.
--key facts
CASCADEowned children only — lines, never users
SET NULLneeds nullable FK
Index FKparent deletes probe child column
--use cases
Owned lifecycles: lines, items, attempts — CASCADE
Guarded roots: users, tenants, accounts — RESTRICT
--under the hood
CASCADE deletes fire child triggers + WAL per row — large fan-outs chunk like bulk deletes.
FK checks probe parent PK (indexed) + child FK (index it or parent deletes scan).

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.