Difficulty: Beginner
Is a unique constraint the same as a unique index? What are the practical differences?
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.
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.UNIQUE KEY and a unique constraint are essentially the same thing; the constraint is the index.Because the enforcement mechanism is the same index, insert speed and lookup speed are the same either way.
| Feature | Unique constraint | Unique index |
|---|---|---|
| Part of logical schema / standard SQL | Yes | No, physical object |
Partial uniqueness (WHERE deleted_at IS NULL) | No | Yes (PostgreSQL, SQL Server filtered index) |
Expression uniqueness (lower(email)) | No | Yes (PostgreSQL; MySQL 8.0.13+ functional key parts) |
DEFERRABLE checking at commit | Yes (PostgreSQL) | No |
Used for ON CONFLICT ON CONSTRAINT name | Yes | Use column list instead |
| Can be created without blocking writes | Build index CONCURRENTLY first, then attach with USING INDEX | CREATE UNIQUE INDEX CONCURRENTLY |
By SQL standard behaviour, NULL is not equal to NULL, so a unique column can hold many NULLs:
UNIQUE NULLS NOT DISTINCT to treat NULLs as equal.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.
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.
-- 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.
Unique Constraint, Unique Index, Partial Index, NULL Handling, Deferrable Constraints