Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends.
Is Unique key accept null value. In my application it does not accept multiple null value.but from i studied it accept null for unique column.suppose it accept, how can i modify my unique column table for accepting null value

Advance Thanks
Posted
Updated 20-Jan-20 20:58pm
v3
Comments
Abhishek Pant 4-Nov-12 4:27am    
Will you eloborate what type of unique key you used a primary key or unique key.... in case of unique key 1 null value is accepted for every row and column. while in case of primary key no null values are accepted.if you built a primary key then||alter table <table name> drop constraint constraint_name Primary key(column_name)

There is a difference between Unique key and unique index.
A unique key is a unique index but Unique key can have only one null value and unique index can have multiple null values, try this:

Unique key:
CREATE TABLE tbl_TestUniquekey (ID INT)
GO
ALTER TABLE tbl_TestUniquekey
ADD CONSTRAINT uk_id UNIQUE NONCLUSTERED (ID )
Go
INSERT INTO tbl_TestUniquekey
VALUES (1),(NULL),(NULL),(2),(NULL)
/*output:
Msg 2627, Level 14, State 1, Line 29
Violation of UNIQUE KEY constraint 'uk_id'. Cannot insert duplicate key in object 'dbo.tbl_TestUniquekey'. The duplicate key value is (<null>).
The statement has been terminated.
*/



Unique Index:

CREATE TABLE tbl_TestUnique (ID INT)
GO

CREATE UNIQUE NONCLUSTERED INDEX idx_tbl_TestUnique_ID
ON tbl_TestUnique(ID)
WHERE ID IS NOT NULL
GO
INSERT INTO tbl_TestUnique
VALUES (1),(NULL),(NULL),(2),(NULL)

/*Output:
(5 rows affected)
*/
 
Share this answer
 
Comments
Richard Deeming 21-Jan-20 9:54am    
The index only allows multiple Null values because it's a filtered index which explicitly excludes those values.

Create Filtered Indexes - SQL Server | Microsoft Docs[^]

This is supported in SQL Server 2008 or later. Given the age of the question, it's possible that the OP may still have been using an earlier version.
Logically, any key which is allowed to contain non duplicate (unique) values is a unique key, NULL is a permissible value in SQL Server , so it can have NULL for a single time just like any other value.

But as per standards, there is no such rule that only ONE NULL is allowed. So, with SQL Server you are stuck with just one time NULL value.
Refer:
http://en.wikipedia.org/wiki/Unique_key[^]
http://connect.microsoft.com/SQLServer/feedback/details/299229/change-unique-constraint-to-allow-multiple-null-values[^]
 
Share this answer
 
v2
If you mean on a primary key column, then no - you can't have null primary keys.

If you mean a unique index on a nullable column, well...not really a good idea...but...it is possible.

Have a look here: http://sqlservercodebook.blogspot.co.uk/2008/04/multiple-null-values-in-unique-index-in.html[^]
 
Share this answer
 
Comments
Sandeep Mewara 4-Nov-12 4:24am    
Yeah, but there is a limitation/issue with SQL Server with multiple NULL values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900