Difference between primary key and unique key



Unique Key: It's a column or a group of columns that can identify uniqueness in a row.
Primary Key: It's also a column or group of columns that can identify uniqueness in a row.
The Primary key is just another name for the unique key, but the default implementation in SQL 
The server is different for Primary and Unique Key.


Primary Key:


  • There can only be one primary key in a table.
  • In some DBMS it cannot be NULL.
  • Primary Key is a unique key identifier of the record.
Unique Key:


  • It can be more than one unique key in one table.
  • The unique key can have valuesNULL.
  • It can be a candidate key.
  • The unique key can be andNULL may not be unique.

By Default:
  1. Primary Key creates a Clustered index and the Unique key creates a Non-Clustered Index.
  2. PK is not null, but Unique Key allows nulls (Note: By Default)
  3. There can only be one and only one PK on a table, but there can be multiple UK's
  4. You can override the default implementation depending upon your needs.
It really depends on what is your aim when deciding whether to create a UK or PK. It follows an analogy like "If there is a team of three people, so all of them are peers, but there will be one of them
who will be a pair of peers: Primary Key and Unique Key have a similar relation.". I would suggest reading this article

: The example given by the author may not seem suitable, but try to get an overall idea.

1 comment:

  1. Great article, Thanks for sharing helpful information. It will helpful for me.

    Regards,
    PHP web developer

    ReplyDelete