Re: Unique Constraint with Multiple NULLS

From: David Portas <REMOVE_BEFORE_REPLYING_dportas_at_acm.org>
Date: Thu, 1 Jan 2004 23:21:02 -0000
Message-ID: <cZOdnQ0dA-cnN2mi4p2dnA_at_giganews.com>


Rather than create two identical tables you could just replicate the constrained columns and enforce uniqueness through a nullable foreign key constraint. Example:

CREATE TABLE foo1 (prikey INTEGER, fookey1 INTEGER, fookey2 INTEGER, UNIQUE (fookey1,fookey2), PRIMARY KEY (prikey,fookey1,fookey2))

CREATE TABLE foo2 (prikey INTEGER, fookey1 INTEGER, fookey3 INTEGER, UNIQUE (fookey1,fookey3), PRIMARY KEY (prikey,fookey1,fookey3))

CREATE TABLE Sometable (prikey INTEGER PRIMARY KEY, fookey1 INTEGER NULL, fookey2 INTEGER NULL, fookey3 INTEGER NULL, FOREIGN KEY (prikey,fookey1,fookey2) REFERENCES foo1 (prikey,fookey1,fookey2), FOREIGN KEY (prikey,fookey1,fookey3) REFERENCES foo2 (prikey,fookey1,fookey3) /* , ... other columns */)

INSERT INTO foo1 VALUES (100,1,2)
INSERT INTO foo1 VALUES (101,1,3)

INSERT INTO Sometable VALUES (100,1,2,NULL) INSERT INTO Sometable VALUES (101,1,3,NULL)

-- 
David Portas
------------
Please reply only to the newsgroup
--
Received on Fri Jan 02 2004 - 00:21:02 CET

Original text of this message