Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

04 December 2011

Does UNIQUE KEY supports more than one NULL Value

Does UNIQUE KEY supports more than one NULL Value?

No. In SQL server we can insert only one NULL value to column which has UNIQUE KEY constraint.

 Example:

CREATE TABLE SamepleUnique
(
Id INT PRIMARY KEY,
Name varchar(20) UNIQUE
)
INSERT INTO SamepleUnique VALUES(1,'Ram')
INSERT INTO SamepleUnique VALUES(2,'Krish')
INSERT INTO SamepleUnique VALUES(3,Null)
INSERT INTO SamepleUnique VALUES(4,Null) --Error as Violation of UNIQUE KEY constraint 'UQ__SamepleU__737584F603317E3D'. Cannot insert duplicate key in object 'dbo.SamepleUnique'.
--The statement has been terminated.
Output:

0 Comments:

Post a Comment