// dialect comparison

CREATE UNIQUE INDEX in every SQL dialect

A unique index both speeds up lookups and rejects duplicate values. Syntax is universal; NULL handling is where engines disagree.

Filed under Indexes

The verdict

Same statement everywhere. One-NULL-only semantics are the difference: SQL Server enforces them by default, PostgreSQL 15+ offers NULLS NOT DISTINCT, and MySQL, SQLite and Oracle have no switch for it. To allow many NULLs in SQL Server, add WHERE email IS NOT NULL to the index.

Where they differ

All five engines support CREATE UNIQUE INDEX with the same syntax. The subtle part is NULLs: most engines treat NULLs as distinct from each other, so multiple NULL rows are allowed.

How each engine writes it

SQL Server

CREATE UNIQUE INDEX idx_users_email ON users (email);

NULLs are treated as equal, so a unique index allows only one NULL. Filtered indexes can scope uniqueness: WHERE email IS NOT NULL.

PostgreSQL

CREATE UNIQUE INDEX idx_users_email ON users (email);

Multiple NULLs are allowed (they are not equal). PostgreSQL 15+ adds NULLS NOT DISTINCT to reject them.

MySQL

CREATE UNIQUE INDEX idx_users_email ON users (email);

Multiple NULLs are allowed; a unique index on a nullable column does not enforce a single NULL.

SQLite

CREATE UNIQUE INDEX idx_users_email ON users (email);

Multiple NULLs are allowed, matching the SQL standard.

Oracle

CREATE UNIQUE INDEX idx_users_email ON users (email);

Multiple NULLs are allowed; rows where every indexed column is NULL do not even enter the index.

Questions that come up

Unique index or UNIQUE constraint?

A UNIQUE constraint creates a unique index under the hood. Prefer the constraint in DDL so the intent is visible in metadata; the index form is useful when adding uniqueness to an existing busy table.

Can I make a case-insensitive unique index?

Yes: index LOWER(email) instead of email (an expression index), or use a case-insensitive collation so duplicate spellings collide.

What error do duplicate rows raise?

SQL Server error 2601/2627, PostgreSQL error 23505, MySQL error 1062 (ER_DUP_ENTRY), SQLite constraint SQLITE_CONSTRAINT_UNIQUE, Oracle ORA-00001.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.