1. Home >
  2. Apps >
  3. Groups >

Difference between a primary key and unique key.

Question asked by aarthivg in #Coffee Room on Jun 10, 2013
aarthivg
aarthivg · Jun 10, 2013
Rank B1 - LEADER
can anyone help me out to know the difference between primary and unique key. Posted in: #Coffee Room
Anoop Kumar
Anoop Kumar · Jun 10, 2013
Rank A2 - PRO
You can define a unique key on columns that allow nulls.
That is at most one data from a column can be null.

Primary can't be defined on nullable colums.
Prasad Ajinkya
Prasad Ajinkya · Jun 10, 2013
Rank B1 - LEADER
Which RDBMS are we discussing? or is this a general DB question?
aarthivg
aarthivg · Jun 11, 2013
Rank B1 - LEADER
ianoop
You can define a unique key on columns that allow nulls.
That is at most data from a column can be null.

Primary can't be defined on nullable colums.

for an example, Can I say that, for a back account, account Id will be a primary key?
Anoop Kumar
Anoop Kumar · Jun 11, 2013
Rank A2 - PRO
aarthivg
for an example, Can I say that, for a back account, account Id will be a primary key?
Sure, as log as all the rows of table have unique value of account ID and there is no row with null account ID. It fulfills the primary key criteria.
Neeraj Sharma
Neeraj Sharma · Jun 11, 2013
Rank B2 - LEADER
aarthivg

Take it in this way:

We always go for design before we assign primary key or unique constraint to any column of a table.For example, if we own a store and a customer places and order to us, we are sure that every order has to be different from other and there can be no order that can go without an order id associated with it. So, if we have to make an Orders table, we can make order id as a primary key.

So, it boils down to this:

Unique key: All the row values for this column are unique and NULLS are allowed

Primary key: All the row values for the column are unique values and NULLS are not allowed
sulochana anand
sulochana anand · Jun 12, 2013
Rank D1 - MASTER
primary key and uniqe key both have some similarity.both conatain uniqe record not duplicate.but uniqe key can left null while primary key canot left null.only one pk will be defined in a table while unique key can be more than one.
its totaly depends upon database operator in using os primary key or uniqe key keyword.for eg if we book our ticket we get pnr number which is uiniq but status os ticket may be rac,confirm or waiting.pnr number is given as soon as ticket is booked but confirmation of seat can be given as seat is available.we can say that in primary key we are bound to fill record while in uniqe key we can leave for further.as soon as record is available we can fill it.
Manish Goyal
Manish Goyal · Jun 12, 2013
Rank A2 - PRO
You can have many unique keys but primary key can only be one

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up