Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 21st, 2003, 12:48 PM
Authorized User
Join Date: Jun 2003
Location: , , Romania.
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default duplicate nulls in a column with unique constraint

Hello forum,

I little while ago i had to create the following constraint: the rows in a certain table had to have null values (0 or more) or unique non null values. This rule had to be in the database (let's please not go into 'why in the database and not in the bo layer?').

I always believed that a unique constraint on that column, plus an allow null setting for that column, will accomplish this task, but i never did this type of constraint before. Well... it didn't. The table accepts a maximum of one row with null value for that column. The i began to mess around with the ansi_nulls setting, but i could still not get what i wanted. SQL BOL is not 100 percent clear in this matter.

Am I missing something here guys? Is a check constraint with a UDF the only solution?


Reply With Quote
  #2 (permalink)  
Old October 21st, 2003, 01:43 PM
sal sal is offline
Friend of Wrox
Join Date: Oct 2003
Location: Clarksville, TN, USA.
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts

Are you inserting an empty string


Reply With Quote
  #3 (permalink)  
Old October 21st, 2003, 02:41 PM
Friend of Wrox
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

Unfortunately, for purposes of entries in a UNIQUE constraint or index, all NULLs are considered equal (this despite the fact that NULLs are not considered equal to one another everywhere else) (unless SET_ANSI_NULLS is set false to disable SQL-92 standard behavior). As you've seen, SET_ANSI_NULLS does not affect the behavior of NULLs in index/constraints. Thus, you can only have one row with a NULL value in a UNIQUE constraint or index.

I know of no way around this except via a CHECK constraint as you suggest.

Jeff Mason
Custom Apps, Inc.
Reply With Quote
  #4 (permalink)  
Old October 22nd, 2003, 01:58 AM
Authorized User
Join Date: Jun 2003
Location: , , Romania.
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts

No Sal, I am not inserting an empty string.

Thanks Jeff Mason! Things are now clear to me.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DataSet Column with Unique Constrain Problem niceFEPF C# 5 July 31st, 2009 07:04 PM
Why not using a unique constraint in ch 5? asptwodev BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 September 20th, 2007 09:25 AM
How to delete unique rows but with duplicate colum winder SQL Server 2005 3 January 3rd, 2007 04:57 PM
Dealing with duplicate rows/column values SQLScott SQL Server 2000 2 September 29th, 2005 09:04 PM
Insert Record Fails W/ Unique Constraint snw C# 0 September 2nd, 2005 02:14 PM

All times are GMT -4. The time now is 09:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.