From: | Ed Loehr <eloehr(at)austin(dot)rr(dot)com> |
---|---|
To: | Manuel Cabido <manny(at)tinago(dot)msuiit(dot)edu(dot)ph> |
Cc: | pgsql-general(at)hub(dot)org |
Subject: | Re: [GENERAL] Re: referential integrity (fwd) |
Date: | 2000-01-10 02:53:51 |
Message-ID: | 387949BF.13A2AB35@austin.rr.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Manuel Cabido wrote:
>
> My problem is how would i implement referential integrity so that
> whenever i insert a new order, it should check first if the custno exist
> in the customber table. Can somebody please show me an example how would i
> do it in this case?
You may do this via triggers and procedures using the semi-built-in
postgresql language called "PL/pgSQL". Here are a couple of useful
references:
http://www.deja.com/getdoc.xp?AN=548176178 (to activate the language in
your db)
http://www.postgresql.org/docs/postgres/sql-createtrigger.htm
http://www.postgresql.org/docs/postgres/sql-createfunction.htm
There are numerous other examples in the .../src/test/regress/sql/plpgsql.sql
file. Yours will look something like this:
CREATE FUNCTION check_for_customer() RETURNS OPAQUE AS
'DECLARE
cust RECORD;
BEGIN
SELECT INTO temp * FROM customer WHERE custno = NEW.custno;
if not found then
RAISE EXCEPTION ''TRIGGER % % % % on RELATION % : Cannot
insert an order with a non-existent customer number (%)'', TG_NAME, TG_WHEN,
TG_LEVEL, TG_OP, TG_RELNAME, NEW.custno;
end if;
RETURN NEW;
END;'
LANGUAGE 'plpgsql';
CREATE TRIGGER order_insert_trigger
AFTER INSERT ON order
FOR EACH ROW EXECUTE PROCEDURE check_for_customer ();
There is work in progress to provide more automated support for this in
version 7.x, possibly sometime this year.
Cheers,
Ed Loehr
From | Date | Subject | |
---|---|---|---|
Next Message | Aaron Holtz | 2000-01-10 04:01:28 | Re: [GENERAL] can't seem to use index |
Previous Message | Manuel Cabido | 2000-01-10 01:32:01 | Re: referential integrity (fwd) |