Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
in my recent interview I have been asked why primary key can't be null??
when primary key and unique key both used to uniquely identify a row and unique key can have null value once then why primary key can't.
expecting a details explanation on this.
Posted

What is the fundamental attribute required of a Primary Key?

Uniqueness.

So, how unique is NULL? Allowing a primary key to contain nulls would cause a potential conflict with the uniqueness constraint.
 
Share this answer
 
Technically, a primary key constraint is simply a combination of a
unique constraint and a not-null constraint.


A primary key indicates that a column or group of columns can be used as
a unique identifier for rows in the table. (This is a direct consequence
of the definition of a primary key. Note that a unique constraint does
not, by itself, provide a unique identifier because it does not exclude
null values.) This is useful both for documentation purposes and for
client applications.
 
Share this answer
 
It comes from SQL specification:
Quote:
A unique constraint is satisfied if and only if no two rows in
a table have the same non-null values in the unique columns. In
addition, if the unique constraint was defined with PRIMARY KEY,
then it requires that none of the values in the specified column or
columns be the null value.

For the full SQL92 - http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt[^]
 
Share this answer
 
Let me explain this practically
consider the following Situation
SQL
CREATE TABLE CnLanguage
(
	Languageid INT PRIMARY KEY, LanguageNAme VARCHAR(100)
)
GO
CREATE TABLE CnState
(
	Stateid INT PRIMARY KEY, StateName VARCHAR(100)
)
GO
CREATE TABLE Person
(
	id INT PRIMARY KEY, PNAME VARCHAR(100), Languageid INT FOREIGN KEY REFERENCES Cnlanguage(Languageid), 
	Stateid INT FOREIGN KEY REFERENCES Cnstate(Stateid)
)

INSERT INTO dbo.CnLanguage (Languageid, LanguageNAme) VALUES (1, 'English')
INSERT INTO dbo.CnState (Stateid, StateName) VALUES (1, 'Test State')


now if we want to insert a person name without provide the data for state and language,
it means he is not relate to any state or any language(just an assumption)
then how will you insert a record in that table person?
simple
SQL
INSERT INTO dbo.Person(id, PNAME, Languageid, Stateid)VALUES(1, 'Testperson', NULL, NULL)

it means that the person is not relate to any state or any lanugage

Now consider if we allow nulls in primary key
then how can you insert such a record in person table
you will not able to insert a record since there is no primary key found in the table
still if you try to insert it like this
(just an assumption )
SQL
INSERT INTO dbo.CnLanguage (Languageid, LanguageNAme) VALUES (NULL, 'Nothing')
INSERT INTO dbo.CnState (Stateid, StateName) VALUES (Null, 'Nothing')
INSERT INTO dbo.Person(id, PNAME, Languageid, Stateid)VALUES(1, 'Testperson', NULL, NULL)

then it means the person is relate to some language and some state
it is completely contradiction to our situation isn't it?
it's clear now why we are not use the null in primary keys isn't it?
 
Share this answer
 
v2
Primary is a combination of UNIQUE key and NOT NULL key .A primary key constraint does not imply theNOT NULLconstraint in practice. Because NULL is not an actual value (it represents the lack of a value), when two rows are compared, and both rows have NULL in a column, the column values are not considered to be equal. Thus, in order for a unique key to uniquely identify each row in a table, NULL values must not be used
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900