Re: Foreign Unique Constraint

From: "Jon Horsman" <horshaq(at)gmail(dot)com>
To: "chester c young" <chestercyoung(at)yahoo(dot)com>
Cc: "sql pgsql" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Foreign Unique Constraint
Date: 2007-03-29 21:23:02
Message-ID: 4f4c2a010703291423y1fe42ff6rc425a78221f918a3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> use a pre-insert triggers - one for each table. include something like

I ended up going this route and it seems to work. Thanks for the help
from all. I figured i'd post the solution to the list so it shows up
when googled. Also, if my solution can be simplfied i'd appreciate
knowing how.

This would be the trigger for table1 in my example.

CREATE FUNCTION function_name() RETURNS trigger AS '
DECLARE
result RECORD;
BEGIN
SELECT INTO result * FROM table2 WHERE extension=NEW.extension;
IF FOUND THEN
RAISE EXCEPTION ''The extension % is already
in use'', NEW.extension;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;

CREATE TRIGGER function_name BEFORE INSERT OR UPDATE ON table1 FOR
EACH ROW EXECUTE PROCEDURE function_name();

Again, thanks for the help.

Jon.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Kyle Bateman 2007-03-29 21:33:27 Re: olympics ranking query
Previous Message Rajesh Kumar Mallah 2007-03-29 19:48:39 Re: Track query status