The SQL INDEX TABLE statement is the SQL command that adds a new index to an existing table to an SQL database. Indexed enable faster and more efficient retrieval of information from SQL databases.
SQL indexes are used because they can provide the following benefits / functions:
Correct use of indexes can make the difference between a top performing database with high customer satisfaction and a non-performing database with low customer satisfaction.
The SQL CREATE INDEX command is used as follows.
SQL CREATE INDEX Statement Syntax
CREATE INDEX <index_type> <index_name> ON <table_name> (
CREATE UNIQUE INDEX <index_type> <index_name> ON <table_name> (
The number of characters that can make up SQL names for tables, columns and indexes varies by DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a number or special character. The name of a new index can not duplicate the name of an existing index for the same table and should not be the same as a SQL reserved word. The underscore character can be used to improve readability. List elements are seperated by commas.
SQL CREATE INDEX Statement Example 1 - Rapid Access
The following example creates an non-unique index named IDX_PERSON_NAME on table PERSON with columns named person_name and gender_code in ascending sequence..
Here are the contents of the table:
This SQL CREATE VIEW Statement is executed:
CREATE INDEX IDX_PERSON_NAME ON PERSON
(person_name ASC, gender_code ASC)
SQL CREATE INDEX Statement Example 2 - Enforce Uniqueness
The following example creates a unique index named IDX_PERSON_SSN on table PERSON with the column named social_security_nnbr. This assures that two rows can not be added to the PERSON table with the same social security number.
This SQL CREATE TABLE Statement is executed:
CREATE UNIQUE INDEX IDX_PERSON_SSN ON PERSON