Search code examples
mysqlsqldatabaseprimary-keyunique-key

Difference between primary key and unique key


I'm using a MySQL database.

In which situations should I create a unique key or a primary key?


Solution

  • Primary Key:

    • There can only be one primary key constraint 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"