SQL UNIQUE Constraint
A UNIQUE constraint guarantees that no two rows share the same value in a column, or the same combination of values in several columns. Use it for natural identifiers that are not the primary key: email addresses, usernames, product codes, “one review per user per product”.
- Make a column unique
- Make a combination of columns unique
- Understand how UNIQUE treats NULL
- Add and drop unique constraints, and compare with PRIMARY KEY
A unique column
CREATE TABLE users ( user_id INT PRIMARY KEY, username VARCHAR(30) NOT NULL UNIQUE, email VARCHAR(100) UNIQUE);INSERT INTO users VALUES (1, 'asha', 'asha@example.com'), (2, 'ravi', 'ravi@example.com');SELECT * FROM users;| user_id | username | |
|---|---|---|
| 1 | asha | asha@example.com |
| 2 | ravi | ravi@example.com |
A duplicate is rejected:
CREATE TABLE users (user_id INT PRIMARY KEY, username VARCHAR(30) NOT NULL UNIQUE, email VARCHAR(100) UNIQUE);INSERT INTO users VALUES (1, 'asha', 'asha@example.com');INSERT INTO users VALUES (2, 'asha', 'other@example.com');ERROR 1062 (23000): Duplicate entry 'asha' for key 'users.username'
- The first row is inserted.
- The second row has a different id but the same
username, so MySQL rejects it with error 1062 and names the key:users.username. - With the default case-insensitive collation,
'Asha'would also count as a duplicate of'asha'.
UNIQUE and NULL
A unique column may hold many NULLs, because NULL is not equal to anything, not even another NULL:
CREATE TABLE users (user_id INT PRIMARY KEY, username VARCHAR(30) NOT NULL UNIQUE, email VARCHAR(100) UNIQUE);INSERT INTO users VALUES (1, 'asha', NULL), (2, 'ravi', NULL), (3, 'mona', NULL);SELECT COUNT(*) AS users_without_email FROM users WHERE email IS NULL;| users_without_email |
|---|
| 3 |
Add NOT NULL if every row must have a distinct, real value.
Composite unique key
One review per customer per product: the pair must be unique, each column alone may repeat:
CREATE TABLE reviews ( review_id INT PRIMARY KEY, customer_id INT NOT NULL, product_id INT NOT NULL, rating INT NOT NULL, CONSTRAINT uq_review UNIQUE (customer_id, product_id));INSERT INTO reviews VALUES (1, 1, 2, 5), (2, 1, 4, 4), (3, 2, 2, 3);SELECT * FROM reviews;| review_id | customer_id | product_id | rating |
|---|---|---|---|
| 1 | 1 | 2 | 5 |
| 2 | 1 | 4 | 4 |
| 3 | 2 | 2 | 3 |
CREATE TABLE reviews (review_id INT PRIMARY KEY, customer_id INT NOT NULL, product_id INT NOT NULL, rating INT NOT NULL, CONSTRAINT uq_review UNIQUE (customer_id, product_id));INSERT INTO reviews VALUES (1, 1, 2, 5);INSERT INTO reviews VALUES (4, 1, 2, 1);ERROR 1062 (23000): Duplicate entry '1-2' for key 'reviews.uq_review'
- Customer 1 may review products 2 and 4, and customer 2 may also review product 2.
- A second review by customer 1 of product 2 repeats the pair
(1, 2)and is rejected.
Add and drop UNIQUE
ALTER TABLE customers ADD CONSTRAINT uq_customer_email UNIQUE (email);SHOW INDEX FROM customers WHERE Key_name = 'uq_customer_email';ALTER TABLE customers DROP INDEX uq_customer_email;| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| customers | 0 | uq_customer_email | 1 | A | 7 | NULL | NULL | YES | BTREE | YES | NULL |
- In MySQL a unique constraint is implemented as a unique index, which also speeds up lookups by email.
SHOW INDEXlists it;Non_unique = 0means unique.- It is dropped with
DROP INDEX(orDROP CONSTRAINTin MySQL 8.0.19+).
UNIQUE vs PRIMARY KEY
| PRIMARY KEY | UNIQUE | |
|---|---|---|
| Per table | one | many |
| NULL allowed | no | yes (many) |
| Purpose | the row’s identity, target of foreign keys | business rules on other columns |
| InnoDB storage | rows are stored in primary-key order | separate index |
Quick quiz
1. How many NULLs can a UNIQUE column hold in MySQL?
NULLs are never equal to each other, so they do not clash.
2. UNIQUE (customer_id, product_id) rejects…
Only the combination must be unique.
3. How many UNIQUE constraints can a table have?
Unlike the primary key, you can have several.
UNIQUE FAQ
What is a UNIQUE constraint in SQL?
A rule that no two rows may have the same value (or combination of values) in the constrained columns.
Can a UNIQUE column be NULL?
Yes. In MySQL a unique column can contain multiple NULLs; add NOT NULL to prevent them.
Is a UNIQUE constraint an index?
In MySQL, yes: each unique constraint creates a unique index, which also speeds up searches on those columns.