ON DELETE CASCADE Multi Level Chains
ON DELETE CASCADE walks the whole reference chain: grandchild rows vanish with the root, and one RESTRICT deeper down blocks the entire delete.
Code
-- multi-level chain: CASCADE walks all levels
-- 三级链 project → task → time_entry:插入必须父行先行
CREATE TEMP TABLE projects (
project_id int PRIMARY KEY
);
CREATE TEMP TABLE tasks (
task_id int PRIMARY KEY,
project_id int NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE
);
CREATE TEMP TABLE time_entries (
entry_id int PRIMARY KEY,
task_id int NOT NULL REFERENCES tasks (task_id) ON DELETE CASCADE
);
INSERT INTO projects VALUES (1);
INSERT INTO tasks VALUES (11, 1), (12, 1);
INSERT INTO time_entries VALUES (21, 11), (22, 12);
-- 删掉根行:CASCADE 沿链条逐级传导,两层子表一起清空
DELETE FROM projects WHERE project_id = 1;
SELECT count(*) AS tasks_left FROM tasks;
SELECT count(*) AS entries_left FROM time_entries;
-- 链条末端换成 RESTRICT:删除顶端父行被挡下,整条链都不动
CREATE TEMP TABLE invoices (
invoice_id int PRIMARY KEY
);
CREATE TEMP TABLE invoice_lines (
line_id int PRIMARY KEY,
invoice_id int NOT NULL REFERENCES invoices (invoice_id) ON DELETE CASCADE
);
CREATE TEMP TABLE line_notes (
note_id int PRIMARY KEY,
line_id int NOT NULL REFERENCES invoice_lines (line_id) ON DELETE RESTRICT
);
INSERT INTO invoices VALUES (500);
INSERT INTO invoice_lines VALUES (501, 500);
INSERT INTO line_notes VALUES (502, 501);
-- ERROR: update or delete on table "invoice_lines" violates foreign key constraint
-- DELETE FROM invoices WHERE invoice_id = 500;The three-level chain and the delete that walks it
projects → tasks → time_entries is wired with REFERENCES … ON DELETE CASCADE at each hop, and the inserts run parent-first (project 1, tasks 11 and 12, entries 21 and 22). Deleting the root row — DELETE FROM projects WHERE project_id = 1 — cascades through both levels: PostgreSQL removes tasks 11 and 12 because they reference the project, then removes entries 21 and 22 because they reference those tasks.
What the counts show
tasks_left | entries_left
------------+--------------
0 | 0
Both SELECT count(*) queries return zero — the grandchildren vanished with the root, no per-level deletes needed. The second half of the script rebuilds the same shape as invoices → invoice_lines → line_notes but ends the chain with ON DELETE RESTRICT; the commented-out DELETE FROM invoices WHERE invoice_id = 500 fails with the violation message quoted in the file’s own comment (update or delete on table "invoice_lines" violates foreign key constraint), because the cascade tries to remove invoice_lines while line_notes still references it — one RESTRICT anywhere blocks the whole delete.
The line that blocks the chain
line_id int NOT NULL REFERENCES invoice_lines (line_id) ON DELETE RESTRICT
RESTRICT raises immediately when a referencing row exists; CASCADE propagates instead — the two behaviors compose along a chain in exactly the order the tables declare them.
Interactive Tools for This Topic
FAQ
- Which tables does the SQL touch?
- The references are `projects`, `tasks`, `time_entries`, `invoices`, `invoice_lines`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.