CrazyEngineers
  • Difference between a primary key and unique key.

    aarthivg

    Member

    Updated: Oct 26, 2024
    Views: 1.2K
    can anyone help me out to know the difference between primary and unique key.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberJun 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.
    Cancel
  • Prasad Ajinkya

    MemberJun 10, 2013

    Which RDBMS are we discussing? or is this a general DB question?
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 10, 2013

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 10, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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 allowed
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberJun 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.
    Cancel
  • Manish Goyal

    MemberJun 12, 2013

    You can have many unique keys but primary key can only be one
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register