• subscribe
December 19, 2001 12:00 AM

Using a CHECK Constraint to Enforce a Trigger’s Unique Value

SQL Server Pro
InstantDoc ID #23325
Downloads
23325.zip

I have a column in a SQL Server 7.0 table that allows NULL values. I want this column to be unique when it has a value other than NULL. What's the best way to program this behavior? If I put a UNIQUE constraint on the column, I can include only one record that has a NULL value. I'm using a trigger to enforce this restriction, but can you recommend a simpler way to ensure that all non-NULL values are unique?

SQL Server has no built-in mechanism to prohibit duplicates except NULLs, so you need to implement a custom CHECK constraint to enforce this restriction. For example, Listing 1, page 64, shows a code snippet that enforces the kind of integrity you're looking for. In SQL Server 2000, you can use INSTEAD OF triggers as well to carry out this enforcement. For information about INSTEAD OF triggers, see Itzik Ben-Gen, T-SQL Black Belt, "Tricks with INSTEAD OF Triggers," December 2000, InstantDoc ID 15828; and Kalen Delaney, Inside SQL Server, "INSTEAD OF Triggers on Views," December 2000, InstantDoc ID 15791, and Inside SQL Server, "INSTEAD OF Triggers," November 2000, InstantDoc ID 15524.



ARTICLE TOOLS

Comments
  • Phil
    5 years ago
    Apr 27, 2007

    Uhh... the title of this is "Using a CHECK Constraint to Enforce a Trigger’s Unique Value" if you download the sample code, a trigger is being used... since when is a trigger the same thing as a CHECK constraint? wtf??

  • Aaron Ellis
    11 years ago
    Dec 27, 2001

    --Forgot about updating to NULL. =Þ

    CREATE TRIGGER tr_BaseTable_iud
    ON BaseTable
    FOR INSERT,
    UPDATE,
    DELETE

    AS

    /*
    ** æ 12/26/01 org: Trig to maintain unique values.
    ** æ 12/27/01 Bug fix: UPDATE to NULL.
    */

    SET NOCOUNT ON

    DELETE FROM ut
    FROM dbo.UniqueTable ut
    JOIN deleted d
    ON ut.unique1 = d.unique1
    LEFT JOIN inserted i
    ON d.pkid = i.pkid
    WHERE i.unique1 IS NULL

    UPDATE ut
    SET ut.unique1 = i.unique1
    FROM dbo.UniqueTable ut
    JOIN deleted d
    ON ut.unique1 = d.unique1
    JOIN inserted i
    ON d.pkid = i.pkid
    WHERE d.unique1 <> i.unique1
    AND i.unique1 IS NOT NULL

    INSERT INTO dbo.UniqueTable
    SELECT i.unique1
    FROM inserted i
    LEFT JOIN deleted d
    ON i.pkid = d.pkid
    WHERE d.pkid IS NULL
    AND i.unique1 IS NOT NULL

    GO

  • Aaron Ellis
    11 years ago
    Dec 26, 2001

    Although slightly more complicated, using a second table with a Unique index would help keep performance optimized regardless on the size on the base table:

    CREATE TABLE BaseTable(
    pkid int IDENTITY,
    data1 varchar(10) NULL,
    unique1 int NULL )
    GO

    ALTER TABLE BaseTable
    ADD CONSTRAINT pk_BaseTable
    PRIMARY KEY ( pkid)
    GO

    CREATE TABLE UniqueTable(
    unique1 int NOT NULL )
    GO

    ALTER TABLE UniqueTable
    ADD CONSTRAINT pk_UniqueTable
    PRIMARY KEY ( unique1)
    GO

    CREATE TRIGGER tr_BaseTable_iud
    ON BaseTable
    FOR INSERT,
    UPDATE,
    DELETE

    AS

    SET NOCOUNT ON

    DELETE FROM ut
    FROM dbo.UniqueTable ut
    JOIN deleted d
    ON ut.unique1 = d.unique1
    LEFT JOIN inserted i
    ON d.pkid = i.pkid
    WHERE i.pkid IS NULL

    UPDATE ut
    SET ut.unique1 = i.unique1
    FROM dbo.UniqueTable ut
    JOIN deleted d
    ON ut.unique1 = d.unique1
    JOIN inserted i
    ON d.pkid = i.pkid
    WHERE d.unique1 <> i.unique1

    INSERT INTO dbo.UniqueTable
    SELECT i.unique1
    FROM inserted i
    LEFT JOIN deleted d
    ON i.pkid = d.pkid
    WHERE d.pkid IS NULL
    AND i.unique1 IS NOT NULL

    GO

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...