Unique Constraints

Difficulty: Beginner

Question

Is a unique constraint the same as a unique index? What are the practical differences?

Answer

They enforce the same rule, and in most databases a unique constraint is implemented by automatically creating a unique index, so duplicate checks perform identically. The difference is intent and features: a constraint is part of the logical schema and supports things like deferrable checking, while an index is a physical structure that supports options a constraint cannot express, such as partial (WHERE) or expression-based uniqueness. Use a constraint to declare a business rule, and a unique index when you need those extra options.

How they relate under the hood

  • PostgreSQL: ALTER TABLE users ADD CONSTRAINT uq_email UNIQUE (email) creates a unique B-tree index named uq_email. You will see it in pg_indexes and in pg_constraint. A CREATE UNIQUE INDEX appears only in pg_indexes.
  • MySQL InnoDB: UNIQUE KEY and a unique constraint are essentially the same thing; the constraint is the index.
  • SQL Server: a unique constraint creates a unique index behind the scenes; you can also create unique indexes directly.

Because the enforcement mechanism is the same index, insert speed and lookup speed are the same either way.

Where they differ

FeatureUnique constraintUnique index
Part of logical schema / standard SQLYesNo, physical object
Partial uniqueness (WHERE deleted_at IS NULL)NoYes (PostgreSQL, SQL Server filtered index)
Expression uniqueness (lower(email))NoYes (PostgreSQL; MySQL 8.0.13+ functional key parts)
DEFERRABLE checking at commitYes (PostgreSQL)No
Used for ON CONFLICT ON CONSTRAINT nameYesUse column list instead
Can be created without blocking writesBuild index CONCURRENTLY first, then attach with USING INDEXCREATE UNIQUE INDEX CONCURRENTLY

NULL handling, a common gotcha

By SQL standard behaviour, NULL is not equal to NULL, so a unique column can hold many NULLs:

  • PostgreSQL and MySQL allow multiple NULLs by default. PostgreSQL 15 added UNIQUE NULLS NOT DISTINCT to treat NULLs as equal.
  • SQL Server allows only one NULL in a unique constraint; use a filtered index WHERE col IS NOT NULL to allow many.

So if phone is optional but must be unique when present, the default PostgreSQL behaviour is exactly what you want.

Which one to use

  • Use a constraint for plain business rules like "email is unique". It documents intent, shows up in schema tools and ERDs, and gives clearer error messages.
  • Use a unique index when you need case-insensitive uniqueness, uniqueness only among active rows, or uniqueness on an expression.
  • On a large live table in PostgreSQL, create the unique index CONCURRENTLY first, then ALTER TABLE ... ADD CONSTRAINT ... UNIQUE USING INDEX to promote it without a long lock.

A related distinction: a primary key is a unique constraint plus NOT NULL, and a table can have only one.

Code examples

Constraint vs index in PostgreSQL

-- Constraint: plain business rule, creates index uq_users_email
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE (email);

-- Index: case-insensitive uniqueness (a constraint cannot do this)
CREATE UNIQUE INDEX uq_users_email_ci ON users (lower(email));

-- Index: unique only among active rows
CREATE UNIQUE INDEX uq_handle_active ON users (handle) WHERE deleted_at IS NULL;

-- PostgreSQL 15+: treat NULLs as equal
ALTER TABLE devices ADD CONSTRAINT uq_imei UNIQUE NULLS NOT DISTINCT (imei);

-- Zero-downtime: build index first, then promote it
CREATE UNIQUE INDEX CONCURRENTLY uq_orders_ref_idx ON orders (payment_ref);
ALTER TABLE orders ADD CONSTRAINT uq_orders_ref UNIQUE USING INDEX uq_orders_ref_idx;

The first and last statements produce real constraints; the middle ones are unique indexes using features constraints do not support.

Key points

  • A unique constraint is usually implemented by an automatically created unique index
  • Performance of duplicate checking is the same either way
  • Only unique indexes support partial (WHERE) and expression-based uniqueness
  • Only constraints can be DEFERRABLE in PostgreSQL
  • Most databases allow multiple NULLs in a unique column; SQL Server allows one

Common follow-up questions

  • How would you enforce case-insensitive unique emails?
  • What does DEFERRABLE INITIALLY DEFERRED do and when is it useful?
  • How does INSERT ... ON CONFLICT use unique constraints?
  • What is the difference between a primary key and a unique constraint?

Interview tips

  • Start with 'same enforcement, different features' and then list the differences
  • Bring up NULL behaviour; it is a frequent trick follow-up
  • Mention partial unique indexes for soft deletes to connect it to real design work

Concepts covered

Unique Constraint, Unique Index, Partial Index, NULL Handling, Deferrable Constraints

More Database Design questions