Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Unique constraint over 2 columns with allowable NULLs

Re: Unique constraint over 2 columns with allowable NULLs

From: DA Morgan <damorgan_at_psoug.org>
Date: Thu, 29 Nov 2007 08:50:56 -0800
Message-ID: <1196355048.499835@bubbleator.drizzle.com>


mikew01 wrote:
> Hello, I need to add a unique constraint over 2 columns where one of
> the columns could be NULL.
> A standard UNIQUE constraint applied over these 2 columns will break
> when someone tries to put a second NULL into the allowable NULL column
> so Im wondering how to go about enforcing this constraint?
>
> TIA
SQL> CREATE TABLE t (

   2 col1 NUMBER,
   3 col2 NUMBER);

Table created.

SQL> ALTER TABLE t

   2 ADD CONSTRAINT uc_t
   3 UNIQUE (col1, col2)
   4 USING INDEX; Table altered.

SQL> INSERT INTO t VALUES (1, NULL);

1 row created.

SQL> INSERT INTO t VALUES (2, NULL);

1 row created.

SQL> INSERT INTO t VALUES (3, NULL);

1 row created.

SQL> COMMIT; Commit complete.

SQL>

-- 
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan_at_x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Received on Thu Nov 29 2007 - 10:50:56 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US