Re: possible to DELETE CASCADE?

From: Thomas Braad Toft <pgsql-general(at)magicx(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: possible to DELETE CASCADE?
Date: 2004-12-30 19:28:16
Message-ID: 41D456D0.7080509@magicx.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Miles Keaton wrote:
> Is it possible for a query to delete a record and all of its
> foreign-key dependents?
>
> I see DROP CASCADE, but not a DELETE CASCADE.
>
> What I'm trying to do:
> I have a "clients" table.
> I have many different tables that use the clients.id as a foreign key.
> When I delete a client, I want it to delete all records in those many
> different tables that reference this client.
>
> Right now I have my script passing many queries to delete them
> individually. ("delete from history where client_id=?; delete from
> payments where client_id=?" -- etc)

You just have to use ON DELETE CASCADE on your foreign key definition in
all the table which reference the client.

See http://www.postgresql.org/docs/7.4/interactive/sql-createtable.html
and look for FOREIGN KEY and ON DELETE CASCADE. That will have the
effect you are looking for. If this is not enough you will have to
create a trigger for the scenario.

--
Thomas Braad Toft

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Miles Keaton 2004-12-30 19:40:21 Re: possible to DELETE CASCADE?
Previous Message Miles Keaton 2004-12-30 19:10:38 possible to DELETE CASCADE?