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.
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)
8 Comments. Leave new
What about unique key. How unique key maintain only one null value.
What about unique key. How it maintains only one null values.
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
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.
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.
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
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?
I’ve never been able to understand why a SQL NULL doesn’t behave logically, like null in other programming languages. NULL is the absence of a value. There is nothing ‘unknown’ about it. And, as others have stated, SQL is terribly inconsistent with it, with a UNIQUE constraint only allowing a single NULL value, despite it being unequal to NULL, and therefore, according to the rules, never qualifying for uniqueness.