Day 65 – SQL CREATE UNIQUE INDEX Keyword
SQL CREATE UNIQUE INDEX keyword is used to create a new unique index on one or more columns of a table. A unique index is similar to a regular index, but it enforces a constraint that ensures that each value in the indexed column(s) is unique, i.e., it cannot be duplicated.
The basic syntax for using the CREATE UNIQUE INDEX keyword is as follows:
CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
Here, index_name
is the name of the new unique index, and table_name
is the name of the table on which the index will be created. The column1
, column2
, etc. are the names of the columns that will be included in the index.
For example, to create a unique index named “idx_students_email” on the “Students” table, based on the “Email” column, the SQL statement would be:
CREATE UNIQUE INDEX idx_students_email
ON Students (Email);
This would create a new unique index named “idx_students_email” on the “Students” table, based on the “Email” column.
Unlike regular indexes, unique indexes cannot be used to enforce a foreign key constraint, as they only ensure that the values in the indexed column(s) are unique within the same table. However, unique indexes can be used to improve the performance of SQL queries that involve the indexed column(s), as they allow the database to quickly retrieve data based on the values in the indexed column(s).