difference between primary key and unique key

Anuj picture Anuj · Mar 5, 2012 · Viewed 588.3k times · Source

I'm using mysql database. I have a confusion between primary key and unique key.

Please help me where should I create primary and unique key. I mean in which situation we create unique key or primary key .

Answer

Mr. KB picture Mr. KB · Nov 12, 2012

Primary Key:

  • There can only be one primary key in a table
  • In some DBMS it cannot be NULL - e.g. MySQL adds NOT NULL
  • Primary Key is a unique key identifier of the record

Unique Key:

  • Can be more than one unique key in one table
  • Unique key can have NULL values
  • It can be a candidate key
  • Unique key can be NULL ; multiple rows can have NULL values and therefore may not be considered "unique"