Tuesday, April 8, 2014

Question of the Week -08/04/2014


Suppose we need to find out the name of all the months in a given year (Year will be given as an input parameter) where first day of the month falls on weekend (Saturday or Sunday). 

So what should be the query/stored procedure to get the above result?

DMCA.com

Answer - Objective Question (How many Null values in an unique constraint can be inserted in SQL Server)

Question:- How many NULL value allowed in a column in which Unique constraint is defined in SQL Server?

Options:-

a) 1

b) 2

c) More than 1

Answer :- Option a) 1

In SQL Server, we can Insert only one null value in a column where we have defined the Unique Constraint.

For Example , suppose we made a table test_unique, whose script is given below:-

create table test_unique (id int unique)

Now we insert the following values in it.

insert into test_unique(id) values (1)

insert into test_unique(id) values (2)

insert into test_unique(id) values (NULL)

insert into test_unique(id) values (3)

Here we inserted one null value before inserting 3.

If we see the records in the table , it shows the following data.


Let try to insert another NULL value and see what error it gives.

insert into test_unique(id) values (NULL)

Then it will gives the following error


So, In SQL server we can insert only one NULL value in a column where Unique constraint is defined.


DMCA.com