-
can anyone help me out to know the difference between primary and unique key.0
-
Member • Jun 9, 2013
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.Are you sure? This action cannot be undone. -
Member • Jun 10, 2013
Which RDBMS are we discussing? or is this a general DB question?Are you sure? This action cannot be undone. -
Member • Jun 10, 2013
ianoopYou 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?Are you sure? This action cannot be undone. -
Member • Jun 10, 2013
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.aarthivgfor an example, Can I say that, for a back account, account Id will be a primary key?Are you sure? This action cannot be undone. -
Member • Jun 11, 2013
#-Link-Snipped-#
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 allowedAre you sure? This action cannot be undone. -
Member • Jun 11, 2013
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.Are you sure? This action cannot be undone. -
Member • Jun 12, 2013
You can have many unique keys but primary key can only be oneAre you sure? This action cannot be undone.