What is the difference between a primary key and a unique key, can a table have multiple unique keys, and can a unique key contain a NULL value?

Last updated 3 July 2026

  • A primary key uniquely identifies each row, implicitly forbids NULLs, and only one is allowed per table.

  • A unique key also enforces uniqueness, but a table can have many (e.g. unique email and unique username) and they permit NULLs.

  • NULLs don't violate uniqueness because NULL is not equal to NULL. PostgreSQL and Oracle allow multiple NULLs, while SQL Server allows only one per single-column unique index.

  • https://www.geeksforgeeks.org/dbms/difference-between-primary-key-and-unique-key/
  • https://www.postgresql.org/docs/current/glossary.html
  • https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-unique-indexes?view=sql-server-ver17
  • https://medium.com/@vishalbarvaliya/primary-key-vs-unique-key-in-sql-9e9d72d959eb
  • https://www.postgresql.org/docs/current/ddl-constraints.html
  • https://learnsql.com/blog/primary-key-vs-unique-key-sql/
  • https://neon.com/blog/remember-to-use-primary-keys-vs-unique-constraints-in-postgres
  • https://www.postgresql.org/docs/current/sql-createtable.html
All SQL interview questions