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 NULLs?

Re: Unique NULLs?

From: Erwin Dondorp <erwindon_at_wxs.nl>
Date: Wed, 08 Dec 1999 19:24:19 +0100
Message-ID: <384EA253.C2FF32E7@wxs.nl>


"W. Scott Moore" wrote:
> Is it possible to create a Unique field that can have one, and only one,
> null record in it?

NO
Each NULL value is different from another, but you can create a trigger on
a shadow field. This shadow field contains the same value as the original column,
but some insane value when the original column has a NULL value. You can then use a UNIQUE constraint on that column.

Starting with:
CREATE TABLE t ( n NUMBER ) ;

Something like:
REM Add a shadow column
ALTER TABLE t ADD ( shadow_n NUMBER );

REM Copy all the existing values
UPDATE t SET shadow_n = NVL(n, -99999);

REM Set the constraints
ALTER TABLE t MODIFY ( shadow_n UNIQUE NOT NULL );

REM Create the trigger that maintains the shadow value CREATE TRIGGER t1 ON INSERT OR UPDATE
BEGIN
        new.shadow_n = NVL(new.n, -99999)
END;
/

[p.s. the above was not tested and spelling may be incorrect]

Needless to say that the 'insane' value used here is '-99999'.

--
Erwin Dondorp
<http://www.dondorp.com/> Received on Wed Dec 08 1999 - 12:24:19 CST

Original text of this message

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