Difference between a primary key and unique key.

can anyone help me out to know the difference between primary and unique key.

Replies

  • Anoop Kumar
    Anoop Kumar
    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
    Which RDBMS are we discussing? or is this a general DB question?
  • aarthivg
    aarthivg
    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
    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
    #-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 allowed
  • sulochana anand
    sulochana anand
    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
    You can have many unique keys but primary key can only be one

You are reading an archived discussion.

Related Posts

Hi friends, I search after examples of researches and projects that involve or collaborate Computer Vision and one of this big subjects Robotics, Artificial Intelligent, Control, Mechanics and Communication.​
Recently I read a post from Biggi, for IE 10 being most energy saver. I was just wondering which browser is best per you, and what are its pros and...
Hey Folks, I am looking forward to waste my time in gaming but not money (No Single player please). I am fed up of WoW : Mists Of panderia :...
Reliance Communications may soon launch the iPhone CDMA in India - reports BGR claiming that several people who're aware of the matter have confirmed that Reliance and Apple have already...
Okay, we've a new leak, this time the photos of upcoming big boss Samsung Galaxy Note III have been leaked. Observe and tell if it looks awesomer than Note II...