What is the difference between Primary key and Unique key in SQL?

Main differences between Primary key and Unique key in SQL are:

  • Number: There can be only one Primary key in a table. There can be more than one Unique key in a table.
  • Null value: In some DBMS Primary key cannot be NULL. E.g. MySQL adds NOT NULL to Primary key. A Unique key can have null values.
  • Unique Identifier: Primary Key is a unique identifier of a record in database table. Unique key can be null and we may not be able to identify a record in a unique way by a unique key
  • Changes: It is not recommended to change a Primary key. A Unique key can be changed much easily.
  • Usage: Primary Key is used to identify a row in a table. A Unique key is used to prevent duplicate non-null values in a column.
Read the full book at www.amazon.com
Posted in SQL, SQL Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *

*