English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage and examples of the SQL INDEX keyword

SQL Keyword Reference

CREATE INDEX (Create Index)

The CREATE INDEX command is used to create an index in a table (allowing duplicate values).

Indexes are used to retrieve data from the database very quickly. Users do not see indexes; they are only used to speed up searches/Query.

The following SQL creates an index named 'idx_lastname' on the 'LastName' column of the 'Persons' table:

CREATE INDEX idx_lastname
ON Persons (LastName);

If you want to create an index on a column combination, you can list the column names within parentheses and separate them with commas:

CREATE INDEX idx_pname
ON Persons (LastName, FirstName);

Note:The syntax for creating indexes varies between different databases. Therefore: Check the syntax used to create indexes in the database.

Note:Updating a table using an index takes more time than updating a table without an index (because the index also needs to be updated). Therefore, only create indexes on columns that are frequently searched.

DROP INDEX (Delete Index)

The DROP INDEX command is used to delete an index from a table.

MS Access:

DROP INDEX index_name ON table_name;

SQL Server:

DROP INDEX table_name.index_name;

DB2 / Oracle:

DROP INDEX index_name;

MySQL:

ALTER TABLE DROP INDEX ;

SQL Keyword Reference