1. Home
  2. Interview Questions
  3. SQL
  4. What is the difference between a primary key and a unique ke…

Try answering first

In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.

1 Answer

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best answer ·

Short answer: A primary key uniquely identifies each row, can never be NULL, and a table can have only one. A unique key also enforces uniqueness, but a table can have many, and most databases allow NULLs in unique columns, with rules that vary by product.

Detailed explanation

Both are constraints that stop duplicate values, and both are normally enforced with a unique index behind the scenes. The differences lie in their purpose and in how they treat NULLs.

  • Purpose: The primary key is the main identity of the row. Other tables point to it, ORMs use it, and it should be stable. A unique key enforces a business rule, such as no two users sharing an email address.
  • Count: Only one primary key per table. It may be composite, meaning it spans several columns. You can define as many unique constraints as you need.
  • NULLs: Primary key columns are implicitly NOT NULL. Unique columns can hold NULL, but the rules depend on the database:
    • PostgreSQL, MySQL (InnoDB) and Oracle allow multiple NULLs, because NULL is not considered equal to NULL.
    • SQL Server allows only one NULL in a unique constraint. If you need several, use a filtered unique index with WHERE col IS NOT NULL.
    • PostgreSQL 15 and later lets you write UNIQUE NULLS NOT DISTINCT to treat NULLs as equal.
  • Physical storage: In MySQL InnoDB, the table is physically organised (clustered) by the primary key. In SQL Server, the primary key becomes the clustered index by default if no clustered index exists yet. Unique constraints usually create non-clustered indexes. PostgreSQL has no clustered storage by default, so both are ordinary B-tree indexes.
  • Foreign keys: A foreign key can reference a primary key or any unique key. Referencing the primary key is the usual convention.

A common design is a surrogate primary key, such as an auto-increment integer or a UUID, together with unique constraints on natural keys like email, PAN number or employee code. You get a compact, stable identifier, and the business rules are still enforced.

Example

CREATE TABLE users (
    user_id    INT          PRIMARY KEY,
    email      VARCHAR(255) NOT NULL UNIQUE,
    phone      VARCHAR(15)  UNIQUE,         -- optional, may be NULL
    username   VARCHAR(50)  NOT NULL,
    CONSTRAINT uq_users_username UNIQUE (username)
);

INSERT INTO users VALUES (1, '[email protected]', NULL, 'asha');
INSERT INTO users VALUES (2, '[email protected]', NULL, 'ravi');
-- Works in PostgreSQL/MySQL/Oracle (two NULL phones)
-- Fails in SQL Server: only one NULL allowed in a UNIQUE constraint

INSERT INTO users VALUES (NULL, '[email protected]', '98', 'meena');
-- Fails everywhere: primary key cannot be NULL

-- SQL Server workaround for many NULLs:
-- CREATE UNIQUE INDEX uq_phone ON users(phone) WHERE phone IS NOT NULL;

Common mistakes

  • Saying a unique key allows exactly one NULL as a universal rule. That is SQL Server behaviour, not standard SQL.
  • Thinking a table can have two primary keys. A composite key on two columns is still one primary key.
  • Choosing a mutable business value, such as an email or phone number, as the primary key. When it changes, every referencing foreign key must change too.
  • Forgetting to add NOT NULL to unique columns that must always have a value. UNIQUE alone does not prevent NULLs.

Follow-up questions

  • What is the difference between a surrogate key and a natural key, and which would you choose as the primary key?
  • Can a foreign key reference a unique key instead of a primary key?
  • What is a candidate key and an alternate key?

Interview tip: Mention that NULL handling in unique constraints differs between SQL Server and other databases. It shows real hands-on experience rather than textbook recall.

Reviewed by the Interview Questions editorial team · Last updated

How well did you know this?

Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.

Your Answer

More easy SQL questions

All easy questions →

Related Questions

Q1 of 10 · SQL
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA