What happens to post 5's comments?
CREATE TABLE comments (
id INT PRIMARY KEY,
post_id INT REFERENCES posts(id)
ON DELETE CASCADE
);
-- post 5 has 3 comments
DELETE FROM posts WHERE id = 5;
- The delete is rejected
- Their
post_idbecomes NULL - They stay, pointing at 5
- They are deleted as well
ON DELETE CASCADE propagates the parent delete to child rows that reference it.