SQL Foundations  «Prev  Next»

Lesson 6Create an index
ObjectiveCreate an Index on a Column in the Database Table.

Create Index on Column in Table

Using the BasicTable used earlier in this module as an example, a standard request would be to query this database by last name. If that's the case, you will want to consider creating an index on the last name so the SQL engine will automatically keep track of the last names and be able to retrieve them quickly. Creating an index is simple and straightforward. As with the CREATE TABLE statement from the prior lesson, you use the CREATE statement, but with the INDEX keyword:

CREATE INDEX index_name
ON table_name (column_name)

So, for the table you created above, with the first and last name fields and so on, if you want to create an index on the Lastname column, the statement would be as follows:
CREATE INDEX LastnameIndex
ON BasicTable (Lastname)

This creates the index, names it LastnameIndex, and sets it up on BasicTable. The information in the index is created initially, then maintained automatically as rows are inserted, deleted, or updated. Sometimes, the syntax you use to create an index can vary.

Syntax for SQL Indexes

Creating indexes will likely not be something that you do directly in your queries against the database. You will likely need to work with a developer, DBA, or other system administration personnel to implement a new index. Still, it is important to understand what indexes are and the fact that they are available in the first place as a line of defense.
The syntax, or how indexes are implemented, varies from database engine to database engine. For example, on some databases, the index may be stored separately from the data in your system. Other systems store the index as part of the data in your system. There may be considerations of disk space as well. Keep in mind that, if you start indexing every possible column, the disk space required to keep up with that index may be a problem on your servers.


The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading the whole table.

Table Index

An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches and queries.
Updating a table with indexes takes more time than updating a table without because the indexes also require an update. Therefore, you should only create indexes on columns and tables that will be frequently searched against.
In some cases, you can expect an increase in the timing of your queries. It is important to place the information in your database, and then determine how you are most likely to query it. Only then can you really determine what columns need to be indexed. Note that while indexes are helpful for performance in most cases, do not overdo it. By adding too many indexes, you can begin to impact the performance of your system negatively because the engine will be updating the indexes constantly as your data changes.

Creating Index - Exercise

Click on the Exercise link below to practice creating an index in SQL.
Creating Index - Exercise