UNIQUE INDEX is used to enforce the uniqueness of values in single or multiple columns. We can create more than one unique index in a single table. For creating a unique index, the user has to check the data in the column because the unique indexes are used when any column of the table has unique values. This indexing does not allow the field to have duplicate values if the column is unique indexed. A unique index can be applied automatically when a primary key is defined.
We can create it by using the following syntax:
CREATE UNIQUE INDEX index_name
ON table_name (index_column1, index_column2,...);
Example
CREATE TABLE Employee(
ID int AUTO_INCREMENT PRIMARY KEY,
Name varchar(45),
Phone varchar(15),
City varchar(25),
);
Suppose we want to make a Phone column as a unique index. We can do this like below:
CREATE UNIQUE INDEX index_name_phone ON Employee (Phone);