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

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.

Discuss this Article 3

Aaron Ellis (not verified)
on 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
Phil (not verified)
on 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 (not verified)
on 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

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.