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

Поиск
Список
Период
Сортировка
От Stoyan Genov
Тема Building "ON DELETE" rules to keep the referential integrity of a database
Дата
Msg-id 199911171342.PAA19426@lorna.digsys.bg
обсуждение исходный текст
Список 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 по дате отправления:

Предыдущее
От: jose soares
Дата:
Сообщение: Re: [SQL] ODBC/Openlink/Delphi
Следующее
От: jose soares
Дата:
Сообщение: Re: [SQL] NULL