Building "ON DELETE" rules to keep the referential integrity of a database - Mailing list pgsql-sql

From Stoyan Genov
Subject Building "ON DELETE" rules to keep the referential integrity of a database
Date
Msg-id 199911171342.PAA19426@lorna.digsys.bg
Whole thread Raw
List 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



pgsql-sql by date:

Previous
From: jose soares
Date:
Subject: Re: [SQL] ODBC/Openlink/Delphi
Next
From: jose soares
Date:
Subject: Re: [SQL] NULL