From: Jan Wieck <janwieck@Yahoo.com>
To: Blaise Carrupt <bc@mjtsa.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Help creating rules/triggers/functions
Date: Fri, 2 Mar 2001 10:34:02 -0500 (EST)
Message-ID: <200103021534.KAA03563@jupiter.jw.home> (raw)
In-Reply-To: <200102271643.QAA02463@mjtsa.com>
Blaise Carrupt wrote:
> Hi all !
>
> I use PostgreSQL 7.0.2 on a HP-UX system.
>
> I would like to create a simple function and a simple trigger (or rule) that
> deny a delete from a table if the row is referenced in another table.
>
> I though it should look like this (from my Ingres experience... :) :
>
> create function A_del(int4 i_id)
> BEGIN
> SELECT id
> FROM b
> where a_id = :i_id;
>
> if rowcount > 0 then
> RAISE EXCEPTION "not allowed !"
> end if;
> END
>
CREATE FUNCTION A_del () RETURNS opaque AS '
DECLARE
nrefs integer;
BEGIN
nrefs := count(*) FROM b WHERE a_id = OLD.i_id;
IF nrefs > 0 THEN
RAISE EXCEPTION ''a_id % still referenced from b'', OLD.i_id;
END IF;
RETURN OLD;
END;'
LANGUAGE 'plpgsql';
>
> create trigger before delete from A for each row execute procedure A_del(old.id)
CREATE TRIGGER A_del BEFORE DELETE ON A
FOR EACH ROW EXECUTE PROCEDURE A_del();
>
>
> But it seems to be much more complicated with Postgres (create a C function
> using CurrentTriggerData,...). May I have missed something or is it really much
> more complicated ?
Alternatively (IMHO preferred) you could use a referential
integrity constraint in table B, which would also cover
UPDATE on A and check values inserted/updated into/in B.
CREATE TABLE B ( ...
FOREIGN KEY (i_id) REFERENCES A (a_id)
Jan
--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck@Yahoo.com #
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
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: janwieck@Yahoo.com, bc@mjtsa.com
Subject: Re: Help creating rules/triggers/functions
In-Reply-To: <200103021534.KAA03563@jupiter.jw.home>
References: <200102271643.QAA02463@mjtsa.com> <200103021534.KAA03563@jupiter.jw.home>
* 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