SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL CREATE INDEX



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

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


Index

You can create an index on a table to query data more quickly and efficiently.

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

Note: Updating a table that contains 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 frequently searched.

SQL CREATE INDEX Syntax

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

CREATE INDEX index_name
ON table_name (column_name)

SQL CREATE UNIQUE INDEX Syntax

Create on table A unique index. 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)

Comments:The syntax used to create indexes is different in different 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 wish to index more than one column, you can list the column names in parentheses, separated by commas:

CREATE INDEX PIndex
ON Persons (LastName, FirstName)

php.cn