Can We Have NULL Value in Primary Key? – Interview Question of the Week #075

This is a very interesting question. I really do not know why interviews are fascinated with this question. One of the professional services I offer is to help organizations select an ideal employee for their organization and last week, I was on a conference call with a large MNC who was looking for performance tuning expert for their organization. We interviewed around 50 different employees and offered jobs to 2 experts and 1 has so far accepted it. One of the questions which I see my friends from the interview panel asking again and again was about Primary Key. I noticed that most of the candidate got it correct.

Question: Can we have NULL Value in the Primary Key Column?

Answer: No. We can’t have a Primary Key column with a NULL value.

Can We Have NULL Value in Primary Key? - Interview Question of the Week #075 errornull

The reason for the same is very simple, primary key purpose is to uniquely identify records. If two records of a single column have a NULL value, the column values are not considered equal. In simple words two NULL values are not considered as equal. This is the reason, Primary Key can’t have NULL values as they are not compared with any other value.

Here is a simple script if you execute, it will throw an error because Primary Key can’t have NULL value.

-- Following script will give ERROR
CREATE TABLE TestTable(
	[ID] [int] NULL,
	[Col1] [nvarchar](60) NOT NULL	
 CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
))
GO

Here is the output, which we receive when we execute above query.

Well, that’s it for today. Do let me know if you have any other interview question, which other users might be interested to read.

Reference: Pinal Dave (https://blog.sqlauthority.com)

, , ,
Previous Post
SQL SERVER – How Much Free Space I Have in My Database?
Next Post
What is a Master Database in SQL Server? – Interview Question of the Week #076

Related Posts

7 Comments. Leave new

  • What about unique key. How unique key maintain only one null value.

    Reply
  • What about unique key. How it maintains only one null values.

    Reply
  • Vinod Andani
    June 13, 2016 4:47 pm

    Pinal, “If two records of a single column have a NULL value, the column values are not considered equal”, then why GROUP BY considers NULLS as same when more than 1 record have nulls and groups by as same. Same case with distinct.
    Purpose of Primary Key cannot have Nulls : It should contain a valid atomic value which uniquely identifies record in the table, which can be indexed and further be linked to other tables using Foreign Keys. Nulls are UNKNOWN and unknowns cannot be referred nor is a candidate value and so Primary Key cannot have nulls.

    some examples :

    create table test_nulls (id int identity(1,1),
    col_value varchar(25))
    go

    insert into test_nulls(col_value)
    values(null), (‘NULL’), (‘some value’), (null),(’10’), (null),(‘text’)
    go
    select * from test_nulls

    select col_value, count(1) from test_nulls
    group by col_value

    Reply
  • Purpose of Primary Key cannot have Nulls : It should contain a valid atomic value which uniquely identifies record in the table, “which can be indexed and further be linked to other tables using Foreign Key. Nulls are UNKNOWN and unknowns cannot be referred …”
    Not agreed with this statement.

    To reference any field as foreign key, its not necessary that it should be primary key. Unique key can also referred as foreign key. Unique key can contain NULL values and so Foreign key can contain NULL values too.

    Reply
  • Vinod Andani
    July 18, 2016 7:11 pm

    Yes its true that Unique Key can also be referred as foreign keys, Thanks for letting me know this !! But you cannot make Unique keys as potential candidate unless imposing NOT NULL. And its of no value to represent NULL as a candidate value and so Primary Keys cannot have NULLs. On the other hand, attributes with Primary Keys when referred as foreign keys in child tables can have NULLs for which parent value is unknown.

    Reply
  • Yashveer Gurjar
    August 31, 2016 12:25 am

    Hi Pinal.As far as I Know.Primary key is a combination of NOt NULL and Unique .Then according to that concept it will not accept null values.

    In concern with UNIQUE constraint it will only accept 1 NULL value

    Reply
  • A primary key is not only a unique identifier but also a value that can satisfy a predicate such as WHERE = , used to look up a unique row (assuming a simple PK). A NULL cannot directly substitute for a non-NULL value in that role, even if it is unique, because either a different predicate would be required depending on whether or not the key value was NULL or else a non-SARGable expression would have to be used in place of the PK column name in the predicate. NULL is just not on an equal footing with non-NULL values when used to uniquely identify rows.

    So maybe it is a useful interview question after all?

    Reply

Leave a Reply

Menu