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.
A unique index both speeds up lookups and rejects duplicate values. Syntax is universal; NULL handling is where engines disagree.
Filed under Index
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.
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.
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.
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.
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.
CREATE UNIQUE INDEX idx_users_email ON users (email);
Multiple NULLs are allowed, matching the SQL standard.
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.
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.
Yes: index LOWER(email) instead of email (an expression index), or use a case-insensitive collation so duplicate spellings collide.
SQL Server error 2601/2627, PostgreSQL error 23505, MySQL error 1062 (ER_DUP_ENTRY), SQLite constraint SQLITE_CONSTRAINT_UNIQUE, Oracle ORA-00001.
The bread-and-butter index statement: nearly identical everywhere, with a few differences in naming and locking.
An expression index stores a function result instead of the raw column, which makes WHERE LOWER(email) = ? use an index.
A partial index covers only rows matching a WHERE clause, keeping it small and fast when you always filter the same way.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.