Skip to content

SQL Relationships

Foreign Keys

ON DELETE CASCADE

sql - PostgreSQL: FOREIGN KEY/ON DELETE CASCADE - Stack Overflow

Deleting any clan (the other table) will delete any hobbits

If you delete a hobbit (current table), nothing will happen to the other table

  • unless there's a many-to-many relationship
CREATE TABLE clan (
    id serial PRIMARY KEY,
    clan varchar
);

CREATE TABLE hobbit (
    id serial PRIMARY KEY,
    hobbit varchar,
    clan_id integer REFERENCES shire.clans (id) ON DELETE CASCADE
);

Last update: 2022-11-04