WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL CREATE INDEX Statement



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

Indexes enable database applications to look up data faster without reading the entire table.


Index

You can create indexes on tables to query data more quickly and efficiently.

Users cannot see the indexes, they can only be used to speed up searches /queries.

Note: Updating a table containing an index takes more time than updating a table without an index, because the index itself also needs to be updated. Therefore, it is ideal to create indexes only on columns (and tables) that are often searched.

SQL CREATE INDEX syntax

Create a simple index on the table. Allow duplicate values:

CREATE INDEX index_name
ON table_name (column_name)

SQL CREATE UNIQUE INDEX syntax

Create a unique index on the 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 used to create an index is different in different databases. So check the syntax of creating indexes in your database.


CREATE INDEX instance

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 to index more than one column, you can list the names of those columns in parentheses, separated by commas:

CREATE INDEX PIndex
ON Persons (LastName, FirstName)