ALTER TABLE ADD UNIQUE
Forbids duplicate values in a column or column list. Four engines take one clause and back it with a unique index; SQLite goes straight to the index or rebuilds.
- 카테고리
- 테이블
- In this category
- 12 of 16
구문
ALTER TABLE t ADD CONSTRAINT uq_name UNIQUE (col [, ...]) -- SQL Server, PostgreSQL, Oracle
ALTER TABLE t ADD UNIQUE [KEY] (col) -- MySQL: a unique constraint is a unique index
CREATE UNIQUE INDEX uq_name ON t (col) -- SQLite: the practical equivalent, no table rebuild
ALTER TABLE t ADD CONSTRAINT uq_name UNIQUE USING INDEX idx_name -- PostgreSQL: promote an existing unique index
Each block is a separate form. The support table and the examples show which engine takes which.
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | 예 | creates a unique index to enforce it; the constraint and its index drop as a unit; NULL counts as a value, so only one NULL fits (a filtered unique index WHERE col IS NOT NULL allows many) |
| PostgreSQL | 예 | also UNIQUE USING INDEX over an index built CONCURRENTLY; NULLS NOT DISTINCT from 15 |
| MySQL | 예 | the constraint is literally a unique index: same name, same SHOW INDEX row, dropped with DROP INDEX |
| SQLite | Different syntax | no ADD CONSTRAINT: a unique index enforces the same rule without a rebuild |
| Oracle | 예 | backs the constraint with a unique index; USING INDEX reuses one you built |
예시
PostgreSQL
ALTER TABLE orders ADD CONSTRAINT uq_orders_ref UNIQUE (order_ref);
PostgreSQL
-- index built without blocking writes, promoted without a re-scan CREATE UNIQUE INDEX CONCURRENTLY idx_orders_ref ON orders (order_ref); ALTER TABLE orders ADD CONSTRAINT uq_orders_ref UNIQUE USING INDEX idx_orders_ref;
SQL Server
ALTER TABLE dbo.orders ADD CONSTRAINT uq_orders_ref UNIQUE (order_ref);
MySQL
-- the constraint and the index are the same object ALTER TABLE orders ADD UNIQUE KEY uq_orders_ref (order_ref);
SQLite
-- no ALTER form, no rebuild needed: CREATE UNIQUE INDEX uq_orders_ref ON orders (order_ref);
Oracle
CREATE UNIQUE INDEX idx_orders_ref ON orders (order_ref); ALTER TABLE orders ADD CONSTRAINT uq_orders_ref UNIQUE (order_ref) USING INDEX idx_orders_ref;
알아 두면 좋은 정보
- NULL handling splits the engines. PostgreSQL, MySQL, SQLite and Oracle treat NULLs as distinct, so a nullable column holds any number of NULLs under a unique constraint; PostgreSQL 15+ can close that with UNIQUE NULLS NOT DISTINCT, and elsewhere you add NOT NULL to the column. SQL Server goes the other way: NULL counts as a value, so a second NULL is a duplicate (error 2627); a filtered unique index (CREATE UNIQUE INDEX ... WHERE col IS NOT NULL) gives you many NULLs.
- On MySQL the constraint is an index by another name: it appears in SHOW INDEX and DROP INDEX removes it (DROP CONSTRAINT works too from 8.0.19). information_schema.TABLE_CONSTRAINTS lists it as UNIQUE, but a plain CREATE UNIQUE INDEX shows up there the same way, because there is no separate constraint object.
- Adding the constraint checks the whole table for duplicates, a full scan on a big table. PostgreSQL's UNIQUE USING INDEX promotes an index you built with CONCURRENTLY, and Oracle's USING INDEX reuses one you already built, so the scan happens once, when you choose.
- For a composite list the whole tuple is the unit: (7, 3) twice collides, (7, 3) next to (7, 4) does not, and rows where every listed column is NULL stay allowed everywhere except SQL Server, which rejects a second (NULL, NULL), and PostgreSQL with NULLS NOT DISTINCT.
Questions about ADD UNIQUE
Does a unique constraint allow NULLs?
On PostgreSQL, MySQL, SQLite and Oracle, yes, and any number of them: NULL never equals NULL in SQL comparisons. PostgreSQL 15+ offers NULLS NOT DISTINCT to treat them as duplicates; on the others, declare the column NOT NULL alongside the constraint. SQL Server is the exception: it allows exactly one NULL, and a filtered unique index (WHERE col IS NOT NULL) is how you allow more.
Unique constraint or unique index?
They enforce the same rule at the same cost. The named constraint is the portable form and reads well in catalogs (MySQL excepted, where they are one object). The bare index is what you use on SQLite, and what you build CONCURRENTLY on PostgreSQL before promoting it.
Why did my ADD UNIQUE fail?
Existing duplicates. Find them with SELECT order_ref, COUNT(*) FROM orders GROUP BY order_ref HAVING COUNT(*) > 1, merge or archive the extras, then run the ALTER again. The duplicate check runs before the change commits, so the table is untouched after a failure.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.