SQL UNIQUE
A UNIQUE constraint guarantees that no two rows have the same value in a column — or the same combination of values in a group of columns.
CREATE TABLE t (
col1 TEXT UNIQUE, -- one column
col2 INTEGER,
col3 INTEGER,
UNIQUE (col2, col3) -- combination
);Unique e-mail addresses
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL
);
INSERT INTO accounts (email, name) VALUES ('ava@example.com', 'Ava');
INSERT INTO accounts (email, name) VALUES ('liam@example.com', 'Liam');
SELECT * FROM accounts ORDER BY account_id;
INSERT INTO accounts (email, name) VALUES ('ava@example.com', 'Ava again');1 row affected.
1 row affected.
account_id | email | name
-----------+------------------+-----
1 | ava@example.com | Ava
2 | liam@example.com | Liam
(2 rows)
Error: UNIQUE constraint failed: accounts.email
Multi-column uniqueness
A customer may review many products, and a product may get many reviews — but each customer may review each product only once:
CREATE TABLE reviews (
customer_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
stars INTEGER NOT NULL,
UNIQUE (customer_id, product_id)
);
INSERT INTO reviews VALUES (2, 22, 5), (2, 21, 4), (6, 22, 3);
SELECT * FROM reviews ORDER BY customer_id, product_id;
INSERT INTO reviews VALUES (2, 22, 1);3 rows affected.
customer_id | product_id | stars
------------+------------+------
2 | 21 | 4
2 | 22 | 5
6 | 22 | 3
(3 rows)
Error: UNIQUE constraint failed: reviews.customer_id, reviews.product_id
Each customer id and each product id repeats, but the pair (2, 22) may appear only once.
UNIQUE and NULL
In SQLite, PostgreSQL, MySQL and Oracle, NULLs are not considered equal to each other, so a UNIQUE column can hold many NULLs. SQL Server is the exception — it allows only one NULL.
CREATE TABLE badges (employee_id INTEGER, badge_no TEXT UNIQUE);
INSERT INTO badges VALUES (1, 'B-001'), (2, NULL), (3, NULL);
SELECT * FROM badges ORDER BY employee_id;3 rows affected.
employee_id | badge_no
------------+---------
1 | B-001
2 | NULL
3 | NULL
(3 rows)
Handling duplicates gracefully
Instead of failing, SQLite can ignore a duplicate or turn the insert into an update ("upsert"):
CREATE TABLE stock_alerts (product_id INTEGER UNIQUE, threshold INTEGER);
INSERT INTO stock_alerts VALUES (4, 10), (14, 5);
INSERT OR IGNORE INTO stock_alerts VALUES (4, 99); -- silently skipped
INSERT INTO stock_alerts VALUES (14, 20)
ON CONFLICT (product_id) DO UPDATE SET threshold = excluded.threshold;
SELECT * FROM stock_alerts ORDER BY product_id;2 rows affected.
0 rows affected.
1 row affected.
product_id | threshold
-----------+----------
4 | 10
14 | 20
(2 rows)
excluded refers to the row that was being inserted. PostgreSQL uses the same ON CONFLICT syntax; MySQL writes ON DUPLICATE KEY UPDATE.
UNIQUE vs PRIMARY KEY
| PRIMARY KEY | UNIQUE | |
|---|---|---|
| How many per table | one | any number |
| NULLs allowed | no | yes (usually) |
| Purpose | identifies the row | prevents duplicates in other columns |
Both create an index behind the scenes, which also speeds up lookups by that column.
Test yourself
With UNIQUE (customer_id, product_id), which pair of rows is rejected?
- (1, 5) and (1, 6)
- (1, 5) and (2, 5)
- (1, 5) and (1, 5)
- None of these
How many UNIQUE constraints can a table have?
- Exactly one
- At most two
- Any number
- None if it has a primary key