2. Comments, and posts that belong to nobody · Concepts

Concepts

  • A foreign key says "this column must match a row in that other table". posts.user_id should only ever hold an id that exists in users. With a foreign key, the database rejects anything else.
  • This is a one-to-many relationship: one user has many posts, each post has one author. The foreign key always lives on the "many" side.
  • Inline, when creating a table: column type REFERENCES other_table (column)
  • On an existing table: ALTER TABLE name ADD CONSTRAINT constraint_name FOREIGN KEY (column) REFERENCES other_table (column);
  • ON DELETE decides what happens to the children when the parent row is deleted. You add it after the REFERENCES clause:
    • ON DELETE RESTRICT (the default behaviour): refuse to delete a parent that still has children.
    • ON DELETE CASCADE: delete the children too.
    • ON DELETE SET NULL: keep the children, blank out the link.