Unique Index (UNIQUE)

Last updated: 2026-11-04

Unique Index (UNIQUE)

CREATE UNIQUE INDEX enforces uniqueness on the indexed columns while building the index. It behaves like a UNIQUE constraint, but the two differ in the system catalogs: a UNIQUE constraint owns an underlying index (visible via conindid in pg_constraint), while CREATE UNIQUE INDEX creates only an index and no constraint record. Unique indexes are also the usual way to implement soft deletes — a WHERE clause restricts uniqueness to the rows that are still live.

Kindindex
Example LanguageSQL

📝 Syntax

CREATE UNIQUE INDEX <index_name> ON <table_name> ( <column_name> ) [ WHERE <condition> ]

⚙️ Parameters

index_nameName of the new index.
table_nameThe table to index.
column_nameThe indexed column(s); values must be unique (NULLs never conflict and may repeat).
where_conditionOptional partial-index predicate that restricts uniqueness to the matching rows.

Returns:No return value. On success the command tag is CREATE INDEX.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

CREATE TABLE
CREATE INDEX
INSERT 0 5000
ANALYZE

-- pg_indexes result: both the primary key index and the unique index are listed
      indexname       |                                      indexdef                                       
----------------------+-------------------------------------------------------------------------------------
 idx_demo_unique_pkey | CREATE UNIQUE INDEX idx_demo_unique_pkey ON public.idx_demo_unique USING btree (id)
 idx_unique_email     | CREATE UNIQUE INDEX idx_unique_email ON public.idx_demo_unique USING btree (email)
(2 rows)

-- EXPLAIN: the unique index also speeds up lookups
Index Scan using idx_unique_email on idx_demo_unique  (cost=0.29..8.30 rows=1 width=4)
  Index Cond: (email = 'user42@example.com'::text)
  Index Searches: 1
  Buffers: shared hit=1 read=2
Planning Time: 0.185 ms
Execution Time: 0.043 ms

(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QShould I use a unique constraint or a unique index?
AUse a UNIQUE constraint when a foreign key must reference it, when it should appear in pg_constraint, or when you want to manage it with ALTER TABLE ADD CONSTRAINT. Use a unique index when you want uniqueness over an expression or a subset of rows, such as lower(email).
QWhy can a unique index hold duplicate NULLs?
AThe SQL standard says NULL is not equal to NULL, so a uniqueness check cannot decide whether two NULLs conflict; PostgreSQL therefore allows any number of NULLs in a unique index. Add a NOT NULL constraint if you truly want to forbid them.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏