UNIQUE Constraint

Last updated: 2026-11-04

UNIQUE Constraint

A unique constraint requires the values in a column (or column combination) to be distinct. Unlike a primary key, a table may carry any number of unique constraints, and the columns may be nullable. PostgreSQL treats two NULLs as distinct from each other, so multiple NULL rows are accepted — a subtlety that surprises many people.

Kindconstraint
Example LanguageSQL

📝 Syntax

Column level: `column_name type UNIQUE`
Table level: `UNIQUE (col1, col2)`
Named: `CONSTRAINT constraint_name UNIQUE (col)`

⚙️ Parameters

col1, col2, ...The columns whose values must be pairwise distinct. Several columns form a composite unique constraint.

Returns:A unique constraint has no return value. It creates a unique index automatically (named `

__key`) and rejects duplicates with `duplicate key value violates unique constraint "..."`.

▶ 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:

SELECT 'rows with NULL phone' AS step, (SELECT count(*)::TEXT FROM users WHERE phone IS NULL) AS result
UNION ALL SELECT 'duplicate email insert',
       (SELECT pg_try('INSERT INTO users VALUES (4,''a@example.com'',''222'')'))
UNION ALL SELECT 'total rows', (SELECT count(*)::TEXT FROM users)
 step                   | result
------------------------+------------------------------------------------------------------
 rows with NULL phone   | 2
 duplicate email insert | duplicate key value violates unique constraint "users_email_key"
 total rows             | 3
(3 rows)

📌 Key Points

  • NULLs take no part in uniqueness checks: the default is NULLS DISTINCT, so a column may hold many NULL rows.
  • To treat NULL as a duplicate, use `UNIQUE NULLS NOT DISTINCT` (PostgreSQL 15+).
  • A unique constraint brings its own unique index, so it can back `ON CONFLICT (col) DO NOTHING` directly.
  • Conditional uniqueness needs a unique index (`CREATE UNIQUE INDEX ... WHERE ...`); unique constraints have no WHERE clause.
More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy does UNIQUE still allow multiple NULL rows?
ASQL treats NULL as 'unknown', and two unknown values cannot be judged equal. PostgreSQL defaults to NULLS DISTINCT, which considers NULLs different from each other. If your business rule requires at most one NULL, use UNIQUE NULLS NOT DISTINCT.
QHow does a UNIQUE constraint differ from a unique index?
AA unique constraint is a schema object: it appears in pg_constraint and can be referenced by foreign keys. A unique index is only an index and survives dropping a constraint. Constraint indexes are named `
__key`, while a unique index can be named freely.
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%

🙏 帮我们做得更好

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

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