Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Question of the month Apr 2012 - Why does SQL Server allow only one NULL value on unique constraint?

Apr 11 2012 12:00AM by Madhivanan   

Run the following code

	declare @t table(i int unique)
	insert into @t 
	select 1 union all
	select null union all
	select null 

You will get the following error

Msg 2627, Level 14, State 1, Line 3
Violation of UNIQUE KEY constraint 'UQ__#A989971__3BD019979D26A3BB'. Cannot insert duplicate key in object 'dbo.@t'. The duplicate key value is ().
The statement has been terminated.

But other RDBMSs like ORACLE, MySQL, etc will allow multiple NULL values on a column defined as Unique key. It is because that NULL values cannot be equal to each other

Why does SQL Server allow only one NULL value on a column that has unique constraint?

Tags: t-sql, sql_server, sqlserver, tsql, question,


Madhivanan
3 · 39% · 12441
11
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

13  Comments  

  • The answer is, It is because that NULL values cannot be eaqual to each other.

    That means that server is not able to distinguish these values so it treats them as one value.

    commented on Apr 13 2012 2:13AM
    Adam Tokarski
    58 · 3% · 1021
  • It was already raised as suggestion: http://connect.microsoft.com/SQLServer/feedback/details/299229/change-unique-constraint-to-allow-multiple-null-values#

    commented on Apr 13 2012 2:29AM
    Adam Tokarski
    58 · 3% · 1021
  • NULL, to me, is undefined. Whenever a value is undefined, it cannot be classified as unique, or cannot be used in a comparison opeartion.

    To go back to the electronics world, we have ON (1), OFF (0) and a third "tri-state", which can be used to understand NULL or "undefined". Because undefined cannot be compared, we only have 1 NULL allowed.

    commented on Apr 13 2012 9:49AM
    Nakul Vachhrajani
    4 · 33% · 10587
  • Though ANSI standard SQL92 states that a UNIQUE constraint should allow multiple NULL values, SQL Server implements its own way of UNIQUE constraints and disallow multiple NULLs in unique constraints. First I faced with such problem during database migration from MySQL to SQL Server.

    commented on Apr 15 2012 11:51PM
    Olga Medvedeva
    66 · 3% · 843
  • NULL value is also treated as an unique value like 1,2,3..... Since it has an unique constraint, it wont allow a duplicate value another NULL value.

    commented on Apr 26 2012 6:49AM
    krtyknm
    1823 · 0% · 10
  • This is due the Three-Valued Logic behavior of the SQL Server.

    UNIQUE constraints, set operators (such as UNION and EXCEPT), and sorting or grouping operations, NULLs are treated as equal in SQL Server. Hence we can have only one NULL value on UNIQUE constraint.

    commented on Apr 27 2012 1:53AM
    v_siva_v
    262 · 1% · 165
  • The definition of Unique key says that only Unique values are allows. Means if we have 2 null values, both will be same and violate the definition.

    commented on Apr 27 2012 2:44AM
    Yugal Kishore
    2895 · 0% · 2
  • Its all possible right now!!

    CREATE TABLE TestUniqueIndex(Id int identity(1,1) PRIMARY KEY, Name varchar(20) NULL);

    GO

    CREATE UNIQUE INDEX IXTestUniqueIndexName ON TestUniqueIndex(Name) WHERE Name IS NOT NULL;

    GO

    INSERT INTO TestUniqueIndex SELECT 'Bill';

    INSERT INTO TestUniqueIndex SELECT 'Bob';

    INSERT INTO TestUniqueIndex SELECT 'Barry';

    INSERT INTO TestUniqueIndex SELECT 'Boris';

    INSERT INTO TestUniqueIndex SELECT null;

    INSERT INTO TestUniqueIndex SELECT null;

    INSERT INTO TestUniqueIndex SELECT 'Bill'; --fails

    commented on Apr 27 2012 6:37AM
    renato_buda
    2895 · 0% · 2
  • You can get around this limitation by using a unique index with a WHERE IS NOT NULL clause.

    commented on Apr 27 2012 7:02AM
    Renato Buda
    1059 · 0% · 24
  • My view is: Eventhough NULL is not equal to NULL, The NULL value's nature is UNKNOWN, So to enforce uniqueness KNOWN values allowed only once, likewise only one UNKNOWN values is allowed

    commented on Apr 27 2012 7:55AM
    govind
    2639 · 0% · 4
  • It was a design decision made years ago by Sybase (SQL Server on Windows was originally written by Sybase and licensed by Microsoft). In order to maintain backwards compatability, the decision has been carried forward.

    commented on Apr 28 2012 3:38PM
    Marc Jellinek
    97 · 2% · 546
  • The reason behind not allowing Multiple NULLs is becuase of the Unique Index that is created when a unique constraint is created. Even though NULL in not considered equal to NULL, but when Unique Index is created it treats two NULL values as equal and that is the reason for not allowing multiple NULLs when you have unique constraint on a column. To change this behavior MS might have to change a lot of code and hence the reason for keeping it same til yet.

    commented on May 1 2012 11:21AM
    satpalyadav
    2895 · 0% · 2
  • declare @t table(i int unique) insert into @t select 1 union all select null union all select null

    sql treats nulls as same, replace union all with union and see what happen.

    or check the difference by adding below select * from @t select distinct * from @t

    commented on Aug 8 2012 2:05AM
    mhwasim
    2895 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

"Question of the month Apr 2012 - Why does SQL Server allow only one NULL value on unique constraint?" rated 5 out of 5 by 11 readers
Question of the month Apr 2012 - Why does SQL Server allow only one NULL value on unique constraint? , 5.0 out of 5 based on 11 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]