Interview Q&A / SQL & DBMS

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between a unique index and a primary key?

SHORT ANSWER

Both enforce uniqueness and create an index. A primary key also forbids NULL, there is only one per table, and it identifies each row. A table can have many unique indexes, and they usually allow NULLs.

Explain it simply

A roll number is the official identity of a student: everyone has exactly one and it can never be blank. A mobile number must be unique too, but one student may not have given any, and a student could have two numbers on file. The roll number is the primary key, the mobile number a unique index.

Example

sql
CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  email   VARCHAR(100),
  mobile  VARCHAR(15)
);

CREATE UNIQUE INDEX uq_email  ON students(email);
CREATE UNIQUE INDEX uq_mobile ON students(mobile);
PropertyPrimary keyUnique index
Count per tableOneMany
NULL allowedNoYes in MySQL and PostgreSQL, multiple NULLs; SQL Server allows one
PurposeRow identity, target of foreign keysPrevent duplicates in a column

Foreign keys normally reference the primary key, though they can reference a unique key.

In InnoDB (MySQL), the primary key is also the clustered index, so row data is stored in its order.

What to say in the interview

"A primary key is unique and not null, one per table, and defines row identity. A unique index only prevents duplicates and can be created many times. Both are backed by an index."

Common mistakes

  • Believing a unique column can never hold NULL.

#sql#index#primary-key#unique

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.