EnjoY | Database Research And Development: SQL Server: Allow Multiple NULL Values in UNIQUE Constraint

Friday, September 4, 2020

SQL Server: Allow Multiple NULL Values in UNIQUE Constraint

 

In this post, I am sharing a demonstration on how to allow multiple NULLs for UNIQUE Constraint in SQL Server.

As per the ANSI, UNIQUE constraint allows multiple NULLs. But in the SQL Server, it allows only one NULL value.
With the UNIQUE constraint, you cannot insert multiple NULLs.

But you can create UNIQUE NONCLUSTERED INDEX with the NOT NULL filter and can insert multiple NULLs.

Please check the below demonstration:

Create a sample table with UNIQUE Constraint:

Try to insert multiple NULLs:

Now, try to insert with single NULL:

Drop the table:

Create the same table with UNIQUE NONCLUSTERED INDEX:

Now, INSERT Multiple Nulls:
As we defined UNIQUE on ID NOT NULL, so we can insert multiple NULLs.

No comments:

Post a Comment

It’s all about friendly conversation here at small review :) I’d love to be hear your thoughts!

Be sure to check back again because I do make every effort to reply to your comments here.

Featured Post

SQL Server : SELECT all columns to be good or bad in database system

This article is half-done without your Comment! *** Please share your thoughts via Comment *** In this post, I am going to write about one o...

Popular Posts