Re: Foreign keys - Mailing list pgsql-general

From Bruno Wolff III
Subject Re: Foreign keys
Date
Msg-id 20030626115516.GC6568@wolff.to
Whole thread Raw
In response to Foreign keys  ("Matt Browne" <mattb@fusion-advertising.co.uk>)
List pgsql-general
On Thu, Jun 26, 2003 at 12:00:07 +0100,
  Matt Browne <mattb@fusion-advertising.co.uk> wrote:
>
> Other tables also reference records in the address table, using a
> similar sort of scheme.
>
> I have foreign keys set up so that if, for example, a record in customer
> is deleted, the corresponding records in the customer_addresses table
> are also removed. However, I can't find a way of ensuring records in the
> address table are deleted too, given that lots of different tables will
> reference address.id.
>
> What I'd like is for records in the address table to be automatically
> deleted at the end of each transaction if nothing references them any
> more. Is there any way to achieve this?

You need to write custom triggers. Any time you delete an address id from
a referencing table you need to check if the referenced id no longer
has any references. Any time you insert (or update the primary key)
a record in the address table you need to check that it is referenced.
You will want this latter check to be deferable.

pgsql-general by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Question regarding performance (large objects involved)
Next
From: Benjamin Jury
Date:
Subject: FW: Foreign keys