$ ./guess_the_languageSQL & Databases
SQL & Databases · medium · #35

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;
  1. The delete is rejected
  2. Their post_id becomes NULL
  3. They stay, pointing at 5
  4. They are deleted as well
ON DELETE CASCADE propagates the parent delete to child rows that reference it.
Play SQL & Databases ▸