Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 21st, 2003, 12:48 PM
Authorized User
 
Join Date: Jun 2003
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?

Thanks!

Defiant.
 
Old October 21st, 2003, 01:43 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you inserting an empty string

""



Sal
 
Old October 21st, 2003, 02:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
www.custom-apps.com
 
Old October 22nd, 2003, 01:58 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No Sal, I am not inserting an empty string.

Thanks Jeff Mason! Things are now clear to me.

defiant.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.