Junior From PDF SQL SQL & Databases

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

Short answer: Unique Index: Ensures that the values in the indexed columns are unique.

Explain a bit more

Can be created on any column and allows NULL values (in most DBMS). A table can have multiple unique indexes. Primary Key Index: Enforces the uniqueness of the column(s) and also guarantees NOT NULL constraint on the column(s). A table can only have one primary key. Automatically creates a unique index. In short, both enforce uniqueness, but the primary key also guarantees that the column(s) cannot be NULL.

Real-world example (ShopNest)

ShopNest adds an index on Orders(CustomerId, CreatedAt) because “my recent orders” is queried constantly.

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details