MySQL UNIQUE Constraint
The UNIQUE constraint in MySQL is used to ensure that all values in a column or a combination of columns are unique across all rows in the table. It prevents duplicate entries, making sure that each value in the column (or combination of columns) is distinct.
Syntax
The UNIQUE constraint can be defined in two ways:
1. When Creating a Table
2. Altering an Existing Table to Add a UNIQUE Constraint
column1, column2, ...: Columns on which theUNIQUEconstraint will be applied.constraint_name: A custom name for the constraint (optional when adding the constraint).
Examples
1. Creating a Table with a UNIQUE Constraint
- The
emailcolumn must contain unique values. No two employees can have the same email address.
2. Adding a UNIQUE Constraint to an Existing Table
If you have an existing table and want to ensure uniqueness on a column:
3. Using UNIQUE on Multiple Columns
You can also apply the UNIQUE constraint to a combination of columns. The combination of values across these columns must be unique:
In this case, no customer can order the same product more than once, even if they make multiple orders.
How It Works
- If you try to insert a row with a duplicate value in a column (or combination of columns) that has a
UNIQUEconstraint, MySQL will return an error, and the insert will fail.
Example: Inserting Duplicate Values
The second insert will fail because the email column must contain unique values.
Unique Index
When a UNIQUE constraint is created, MySQL automatically creates a unique index for the specified column(s). This index helps MySQL efficiently check for duplicates and maintain the uniqueness of the values.
Handling Duplicates with INSERT IGNORE
If you want to insert a row but ignore the insertion if a duplicate is found, you can use the INSERT IGNORE statement:
If the email already exists, the row will be ignored without causing an error.
Behavior with NULL
- The
UNIQUEconstraint does not treatNULLas a duplicate. This means that you can have multipleNULLvalues in a column with aUNIQUEconstraint, becauseNULLis not considered equal to any otherNULL.
Example:
Differences Between PRIMARY KEY and UNIQUE
- Primary Key:
- A
PRIMARY KEYconstraint uniquely identifies each row in a table. - It implicitly creates a unique index and does not allow
NULLvalues.
- A
- Unique Constraint:
- A
UNIQUEconstraint ensures that all values in a column or a set of columns are distinct. - It allows
NULLvalues (multipleNULLvalues are allowed).
- A
Key Considerations
Single vs. Multiple Columns:
- The
UNIQUEconstraint can be applied to a single column or a combination of columns. - When applied to multiple columns, the combination of values across those columns must be unique.
- The
NULL Handling:
- The
UNIQUEconstraint allows multipleNULLvalues, whereasPRIMARY KEYdoes not.
- The
Indexing:
- A
UNIQUEconstraint automatically creates a unique index on the specified columns, improving lookup performance but also consuming additional space.
- A
Conclusion
The UNIQUE constraint is essential for ensuring data integrity by preventing duplicate values in one or more columns. It is widely used to enforce uniqueness for fields like email addresses, usernames, or any other column where duplicate entries would be undesirable. By using the UNIQUE constraint, MySQL efficiently ensures that the data remains consistent and prevents unnecessary duplication.

