What is the difference between a PRIMARY KEY and UNIQUE KEY?

A column in a table (an attribute in an entity) is called a PRIMARY KEY if it uniquely identifies a specific row in a table of multiple rows. A UNIQUE key is a constraint applied on the data of a column. Technically, both are constraints that a column data must satisfy to be added in a table.

  • A UNIQUE key means that the column must not contain any duplicate values, meaning all the values in the column must be UNIQUE.
  • A column with UNIQUE key can accept NULL as a valid value (for only once in the column)
  • A PRIMARY key means that the column must be UNIQUE and no NULL values are allowed (even once)

SQL •  Added one month ago

Join the Newsletter

Subscribe to get our latest content by email.
    We won't send you spam. Unsubscribe at any time.
    We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept