Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Unique Key

Author  Topic 

singh_nirajan
Starting Member

2 Posts

Posted - 2008-07-07 : 09:22:38
Why unique key accept only one Null value?
Answer is another null is duplicate of existing null. I think this not correct answer. One NULL is never equal to another NULL.

Declare @OneNull varchar(5), @anotherNull varchar(5)

Set @OneNull = NULL
Set @anotherNull = NULL

If @OneNull = @anotherNull
Print 'True'
Else
Print 'False'

------------
Result is always : False

any one please explain the logic behind unique key accepting only one Null.


Nirajan Singh

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-07-07 : 10:55:19
"One NULL is never equal to another NULL"

A NULL *could be* equal to another NULL. They both represent missing or incomplete data so they could be the same.

And your logic isn't complete: the result isn't false it is in fact NULL


DECLARE @a INT, @b INT

SELECT @a = NULL, @b = NULL

IF @a = @b PRINT 'EQUALS'
IF @a <> @b PRINT 'NOT EQUAL'


doesn't print anything.

Check out NULL in wikipedia -- has a good triple state logic overview.

-------------
Charlie
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-07-07 : 12:22:31
quote:
Originally posted by singh_nirajan
...any one please explain the logic behind unique key accepting only one Null...



The logic behind it doesn’t really matter; it’s just the way SQL Server works.

If you really want it explained, contact Microsoft.


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -