From: | Bob Dowling <rjd4(at)cam(dot)ac(dot)uk> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Constraint for two fields unique any order |
Date: | 2006-07-19 17:09:44 |
Message-ID: | Pine.LNX.4.61.0607191806260.13493@noether.csi.cam.ac.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Wed, 19 Jul 2006 MargaretGillon(at)chromalloy(dot)com wrote:
> I have a junction table that is recording relationships between two
> records in another table. Is there a way that I can create a constraint so
> that the values are not repeated in any order?
I think the following does what you need, though my "foo_table" won't be
the same as your junction table. If you can't put a UNIQUE constraint in
your table, add to the PERFORM in the function.
CREATE TABLE foo_table
(
id SERIAL PRIMARY KEY,
fkey1 INTEGER NOT NULL,
fkey2 INTEGER NOT NULL,
UNIQUE(fkey1, fkey2)
);
CREATE FUNCTION other_way_test()
RETURNS TRIGGER
AS $$
BEGIN
PERFORM id FROM foo_table WHERE foo_table.fkey1=NEW.fkey2 AND foo_table.fkey2=NEW.fkey1;
IF FOUND
THEN
RAISE NOTICE 'pair of numbers violates reverse order uniqueness';
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END
$$
LANGUAGE 'plpgsql'
;
CREATE TRIGGER other_way_test
BEFORE INSERT
ON foo_table
FOR EACH ROW
EXECUTE PROCEDURE other_way_test()
;
--
Bob Dowling: Head of Unix Systems Division, University Computing Service
rjd4(at)cam(dot)ac(dot)uk New Museums Site, Pembroke Street, Cambridge. CB2 3QH
01223 334710 http://www-uxsup.csx.cam.ac.uk/~rjd4/
--- Those who do not learn from Dilbert are doomed to repeat it. ---
From | Date | Subject | |
---|---|---|---|
Next Message | Reece Hart | 2006-07-19 17:13:04 | Re: Constraint for two fields unique any order |
Previous Message | Joe Conway | 2006-07-19 17:08:32 | Re: Problem creating a function |