Learn how to create, rename and drop indexes in SQL Server with syntax and examples.
An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed columns.
The syntax for creating an index in SQL Server (Transact-SQL) is:
Optional. Indicates that the combination of values in the indexed columns must be unique.
Optional. Indicates that the logical order determines the physical order of the rows in the table.
Optional. Indicates that the logical order does not determine the physical order of the rows in the table.
The name of the index to create.
The name of the table or view on which the index is to be created.
The columns to base the index.
The sort order for each of the columns.
Optional. The columns that are not key columns to add to the leaf level of the nonclustered index.
Optional. The condition to determine which rows to include in the index.
Optional. Indicates that the partition schema determines the filegroups in which the partitions will be mapped.
Optional. Indicates that the index will be created on the specified filegroup.
Optional. Indicates the default filegroup.
Optional. Indicates where to place the FILESTREAM data for a clustered index.
Let's look at an example of how to create an index in SQL Server (Transact-SQL).
For example:
In this example, we've created an index on the contacts table called contacts_idx. It consists of only one field - the last_name field.
We could also create an index with more than one field as in the example below:
In this example, we've created an index on the contacts table called contacts_idx but this time, it consists of the last_name and first_name fields.
Since we have not specified ASC | DESC to each of the columns, the index is created with each of the fields in ascending order. We could modify our example and change the sort orders to descending as follows:
This CREATE INDEX example will create the contacts_idx index with the last_name sorted in descending order and the first_name sorted in descending order.
Next, let's look at an example of how to create a unique index in SQL Server (Transact-SQL).
For example:
This example would create an index called contacts_uidx on that contacts table that consists of the last_name and first_name fields, but also ensures that the there are only unique combinations of the two fields.
You could modify this example further to make the unique index also clustered so that the physical order of the rows in the table is determined by the logical order of the index.
For example:
This example creates an index called contacts_uidx that is a unique index based on the last_name and first_name fields and the index is also clustered which changes the physical order of the rows in the table.
The syntax for renaming an index in SQL Server (Transact-SQL) is:
The name of the table where the index has been created.
The name of the index that you wish to rename.
The new name for the index.
In this example, we're renaming the index on the contacts table called contacts_idx to contacts_index_cname.
The syntax for dropping an index in SQL Server is:
The name of the table where the index has been created.
The name of the index to drop.
Let's look at example of how to drop an index in SQL Server (Transact-SQL).
For example:
In this example, we're dropping an index called supplier_idx.