Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL CREATE INDEX Statement

SQL CREATE INDEX Statement


The CREATE INDEX statement is used to create an index in a table.

Indexes allow database applications to find data faster without reading the entire table.


Index

You can create indexes in a table to query data more quickly and efficiently.

Indexes cannot be seen by users; they can only be used to speed up searches/queries.

**Note:** Updating a table with indexes takes more time than updating a table without indexes, because the indexes themselves also need to be updated. Therefore, the ideal approach is to create indexes only on columns (and tables) that are frequently searched.

SQL CREATE INDEX syntax

Create a simple index on a table. Duplicate values are allowed:

CREATE INDEX index_name

ON table_name (column_name)

SQL CREATE UNIQUE INDEX syntax

Create a unique index on a table. Duplicate values are not allowed: A unique index means that two rows cannot have the same index value. Creates a unique index on a table. Duplicate values are not allowed:

CREATE UNIQUE INDEX index_name

ON table_name (column_name)

**Note:** The syntax for creating indexes differs across databases. Therefore, check the syntax for creating indexes in your database.


CREATE INDEX example

The following SQL statement creates an index named "PIndex" on the "LastName" column of the "Persons" table:

CREATE INDEX PIndex

ON Persons (LastName)

If you want an index on more than one column, you can list the column names in parentheses, separated by commas:

CREATE INDEX PIndex

ON Persons (LastName, FirstName)