Building "ON DELETE" rules to keep the referential integrity of a database

From: "Stoyan Genov" <genov(at)digsys(dot)bg>
To: pgsql-sql(at)postgresql(dot)org
Subject: Building "ON DELETE" rules to keep the referential integrity of a database
Date: 1999-11-17 13:42:46
Message-ID: 199911171342.PAA19426@lorna.digsys.bg
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I cannot figure out how to construct an "ON DELETE" rule to keep the
referential integrity.

Having tables t1 (id, ... ), t2 (t1_id, ...), the best I got to was:

CREATE RULE t1_t2_del AS ON DELETE TO t1 DO
DELETE FROM t2 WHERE t2.t1_id NOT IN (SELECT id FROM t1);

which deletes records from t2 "a step back" - it deletes already "orphaned"
records from t2, and not those being orphaned by the current DELETE.

I would like also to build a rule which works on the "reverse DELETE"
condition, and disallows deleting t1 records if there is a "child" in t2
present.

Does somebody have any ideas? Thanks in advance.

Stoyan Genov

Browse pgsql-sql by date

  From Date Subject
Next Message jose soares 1999-11-17 13:56:16 Re: [SQL] NULL
Previous Message jose soares 1999-11-17 13:34:36 Re: [SQL] ODBC/Openlink/Delphi