From: | Jan Wieck <janwieck(at)Yahoo(dot)com> |
---|---|
To: | Blaise Carrupt <bc(at)mjtsa(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Help creating rules/triggers/functions |
Date: | 2001-03-02 15:34:02 |
Message-ID: | 200103021534.KAA03563@jupiter.jw.home |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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(at)Yahoo(dot)com #
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
From | Date | Subject | |
---|---|---|---|
Next Message | Peter Eisentraut | 2001-03-02 16:10:19 | Re: SQL copy from csv with explicit field ordering |
Previous Message | Hannu Krosing | 2001-03-02 15:03:05 | Re: [HACKERS] why the DB file size does not reduce when 'delete' the data in DB? |