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)

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)
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

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