~/ learn/ postgresql/ cards/ Foreign-key actions
1 of 2

Demonstrate ON DELETE CASCADE: create parent(id) and child(id, parent_id REFERENCES parent ON DELETE CASCADE), insert parent 1 and child (10,1), delete parent 1, then count remaining children (should be 0). Column: remaining_children.

Demonstrate ON DELETE CASCADE: create parent(id) and child(id, parent_id REFERENCES parent ON DELETE CASCADE), insert parent 1 and child (10,1), delete parent 1, then count remaining children (should be 0). Column: remaining_children.

Answer

CREATE TABLE parent (id int PRIMARY KEY); CREATE TABLE child (id int PRIMARY KEY, parent_id int REFERENCES parent(id) ON DELETE CASCADE); INSERT INTO parent VALUES (1); INSERT INTO child VALUES (10, 1); DELETE FROM parent WHERE id = 1; SELECT count(*) AS remaining_children FROM child;

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/7b7cfc95-2de4-4a3f-a169-d9d0c457a60a/flashcard utf-8 LF