Description

The SQL keyword CREATE UNIQUE INDEX is used to create a unique index on a table, which doesn't allow duplicates.

  • It speeds up the fetch or search operations on table data.
  • It speeds up the fetch operation on a table, but it slows down the update operations as they need to update the index as well along with the update.
  • Always choose the columns that are frequently fetched to create indexes.

The below SQL statement creates a unique index on the table, using a single column.

Run this on IDE

CREATE UNIQUE INDEX uidx_pid
ON users (id);

The below SQL statement creates a unique index on the table, using multiple columns.

CREATE UNIQUE INDEX uidx_user
ON users (id, name);

Related Links