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 theUNIQUE
constraint 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
email
column 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
UNIQUE
constraint, 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
UNIQUE
constraint does not treatNULL
as a duplicate. This means that you can have multipleNULL
values in a column with aUNIQUE
constraint, becauseNULL
is not considered equal to any otherNULL
.
Example:
Differences Between PRIMARY KEY
and UNIQUE
- Primary Key:
- A
PRIMARY KEY
constraint uniquely identifies each row in a table. - It implicitly creates a unique index and does not allow
NULL
values.
- A
- Unique Constraint:
- A
UNIQUE
constraint ensures that all values in a column or a set of columns are distinct. - It allows
NULL
values (multipleNULL
values are allowed).
- A
Key Considerations
Single vs. Multiple Columns:
- The
UNIQUE
constraint 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
UNIQUE
constraint allows multipleNULL
values, whereasPRIMARY KEY
does not.
- The
Indexing:
- A
UNIQUE
constraint 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.