From: Stoyan Genov <genov@digsys.bg>
To: pgsql-sql@postgresql.org
Subject: Building "ON DELETE" rules to keep the referential integrity of a database
Date: Wed, 17 Nov 1999 15:42:46 +0200
Message-ID: <199911171342.PAA19426@lorna.digsys.bg> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: genov@digsys.bg
Subject: Re: Building "ON DELETE" rules to keep the referential integrity of a database
In-Reply-To: <199911171342.PAA19426@lorna.digsys.bg>
References: <199911171342.PAA19426@lorna.digsys.bg>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox