The UNIQUE constraint in MySQL ensures that all values in a specified column or combination of columns are distinct. This is useful for ensuring data integrity and preventing duplicate values in columns that require unique values.
- The
UNIQUEconstraint ensures that no two rows in the table can have the same value for the specified column(s). - While the
PRIMARY KEYconstraint also enforces uniqueness, aPRIMARY KEYcolumn cannot acceptNULLvalues, while a column with aUNIQUEconstraint can.
Key Differences Between UNIQUE and PRIMARY KEY:
- Uniqueness: Both constraints enforce uniqueness, but:
- A table can have only one
PRIMARY KEY, whereas it can have multipleUNIQUEconstraints. - A
PRIMARY KEYautomatically has aUNIQUEconstraint. - A
UNIQUEconstraint allowsNULLvalues (but only oneNULLis allowed in a column with aUNIQUEconstraint).
- A table can have only one
- NULL Values: A column with a
UNIQUEconstraint can containNULLvalues, but a column with aPRIMARY KEYconstraint cannot containNULLvalues.
Using the UNIQUE Constraint
1. UNIQUE Constraint on Table Creation
You can apply the UNIQUE constraint while creating a table. This guarantees that all the values in the specified column(s) are unique.
Example: Create Table with UNIQUE Constraint on a Single Column
CREATE TABLE Persons (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT,
UNIQUE (ID)
);
In this example:
- The
IDcolumn is constrained to have unique values across all records. - No two rows can have the same
ID.
Example: Create Table with UNIQUE Constraint on Multiple Columns
CREATE TABLE Persons (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT,
CONSTRAINT UC_Person UNIQUE (ID, LastName)
);
In this example:
- A composite unique constraint is applied to the combination of
IDandLastName, ensuring that the combination of these two values must be unique across all records in the table. - The constraint is named
UC_Person.
2. Adding UNIQUE Constraint to an Existing Table
You can add a UNIQUE constraint to an existing column using the ALTER TABLE statement.
Example: Add UNIQUE Constraint on an Existing Column
ALTER TABLE Persons
ADD UNIQUE (ID);
In this example:
- A
UNIQUEconstraint is added to theIDcolumn in thePersonstable, ensuring that all values in theIDcolumn are distinct.
Example: Add UNIQUE Constraint on Multiple Columns
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID, LastName);
In this example:
- A composite
UNIQUEconstraint is added to ensure that the combination ofIDandLastNameis unique in thePersonstable.
3. Dropping a UNIQUE Constraint
To remove a UNIQUE constraint from a table, you use the DROP INDEX statement, which drops the index associated with the UNIQUE constraint.
Example: Drop a UNIQUE Constraint
ALTER TABLE Persons
DROP INDEX UC_Person;
In this example:
- The
UNIQUEconstraint namedUC_Personis dropped from thePersonstable.
Important Notes:
- Multiple UNIQUE Constraints: A table can have multiple
UNIQUEconstraints. However, it can have only onePRIMARY KEYconstraint. - NULL Values: A
UNIQUEconstraint allowsNULLvalues, but only oneNULLvalue is allowed per column with aUNIQUEconstraint. If multipleNULLvalues are inserted into the same column, the constraint will not raise an error becauseNULLis treated as a distinct value. - Naming Constraints: You can name a
UNIQUEconstraint when you want to reference it easily later (e.g., to drop or modify it). This is especially useful when applying the constraint to multiple columns.
Summary of UNIQUE Constraint Usage:
- On Table Creation: The
UNIQUEconstraint ensures that all values in a specified column or combination of columns are unique. - On Table Modification: You can add a
UNIQUEconstraint to an existing table using theALTER TABLEstatement. - Dropping Constraints: The
UNIQUEconstraint can be removed using theDROP INDEXstatement.
The UNIQUE constraint helps ensure data integrity by preventing duplicate values in a column, making it essential for fields like email addresses, usernames, or product IDs.