Обсуждение: Building "ON DELETE" rules to keep the referential integrity of a database

Поиск
Список
Период
Сортировка

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

От
"Stoyan Genov"
Дата:
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