MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

A Complete Guide to the SQL CREATE INDEX Statement

intro

Let’s learn everything you need to know about the SQL CREATE INDEX statement to master the art of database indexing.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Indexes are a vital component of databases, speeding up queries and significantly improving data retrieval times. If you ever worked with indexes, you know that everything starts with an SQL CREATE INDEX statement.

In this guide, you will learn more about the CREATE INDEX SQL statement, how it works, and when it comes in handy.

Let's dive in!

What Is an Index in SQL?

An index, in the most basic terms, is a data structure. It’s a data structure that acts like an index in a book—it is a reference to data stored in a table. An index speeds up read-based (SELECT) queries while slowing down UPDATEs, INSERTs, and DELETEs. That’s the case because index structures also store data: data that has to be inserted, updated, selected (read through), and sometimes deleted.

Indexes are also home to certain specific terms such as index cardinality (the number of unique values stored in a specific index), index definition (adding an index), and the like.

Indexes are of interest to everyone working with any database management system, no matter that DBMS is of a relational model or not. To get a general introduction to indexes, their types, and how to use them, read our guide on index creation in SQL, and after you’re done, create one yourself.

What Is CREATE INDEX in SQL?

Indexes can be created by using the SQL CREATE INDEX statement.

While the SQL CREATE INDEX statement is widely supported across many database management systems, it is not part of the ANSI/ISO SQL standard. In particular, there is no ANSI standard for the SQL syntax used to create, alter, or manage indexes. Thus, INDEX is not even a reserved keyword according to the standard.

How to Use the CREATE INDEX SQL Statement?

Time to see how to use CREATE INDEX in the most popular databases.

MySQL

The simplified MySQL CREATE INDEX syntax is:

Copy
        
1 CREATE INDEX index_name ON table_name(column_name_1 [, ..., columnn_name_n]);

For the complete syntax, all available options, and index types, see the official documentation.

Key aspects:

  • Normally, indexes are specified when creating a table with CREATE TABLE.
  • CREATE INDEX cannot be used to create a PRIMARY KEY; use ALTER TABLE instead.
  • An index with the form (column_name_1, column_name_2, ...) creates a multi-column index, where index key values are formed by concatenating the values of the specified columns (e.g., (col_1, col_2, col_3)).
  • A column_name_X specification can end with ASC or DESC to indicate whether index values are stored in ascending or descending order. By default, values are stored in ascending order if no specifier is given.
  • ASC and DESC are not supported for HASH indexes, multi-valued indexes, or SPATIAL indexes.

PostgreSQL

The simplified PostgreSQL CREATE INDEX syntax is:

Copy
        
1 CREATE INDEX index_name ON table_name(column_name_1 [, ..., columnn_name_n]);

For the complete syntax, refer to the official documentation.

Key aspects:

  • table_name can refer to either a table or a materialized view.
  • The key field(s) for the index are specified as column names or expressions within parentheses. Multiple columns can be indexed if the index method supports multicolumn indexes.
  • PostgreSQL supports the creation of partial indexes, which target a portion of a table by specifying a WHERE clause in the SQL CREATE INDEX query.
  • All functions and operators used in an index definition must be "immutable," meaning their results must depend solely on the arguments and not on external factors (e.g., the contents of another table or the current time).
  • Postgres provides the index methods B-tree, hash, GiST (Generalized Inverted Search Tree), SP-GiST (Space Partitioned GiST), GIN (Generalized Inverted Index), and the Block Range Index — BRIN.

SQL Server

The simplified CREATE INDEX SQL Server syntax is:

Copy
        
1 CREATE INDEX [CLUSTERED | NONCLUSTERED] ON table_name(column_name_1 [, ..., columnn_name_n]);

For details, read the official documentation.

Key aspects:

  • The CLUSTERED keyword creates an index where the logical order of the key values determines the physical order of the rows in the table.
  • If CLUSTERED is not specified, a non-clustered index is created by default.
  • A table can have up to 999 non-clustered indexes.
  • The CREATE INDEX statement can include a WHERE clause to create a filtered index, specifying which rows to include in the index. A filtered index must be non-clustered.

Oracle

Key aspects:

  • Oracle supports several types of indexes, including normal indexes, bitmap indexes, partitioned indexes, function-based indexes, and domain indexes.
  • To create an index, you must have the INDEX object privilege on the table being indexed and the CREATE ANY INDEX system privilege.

For a more in-depth explanation, refer to the documentation for the complete syntax of the Oracle CREATE INDEX query.

How to Create an Index in SQL with a Database Client

Launching a CREATE INDEX query is practical, but when working with different databases, you must remember the specific syntax to avoid errors. Wouldn't it be better to create indexes through a visual interface? That is exactly the experience offered by DbVisualizer!

DbVisualizer is a powerful, multi-database client that allows you to create indexes visually. Simply download the tool, install it, connect to the database, and navigate to the table you want to index:

Selecting the target table to add an index to using DbVisualizer
Selecting the target table to add an index to using DbVisualizer

Right-click the table and select the "Create Index..." option:

Select the Create Index… option
Select the Create Indexu2026 option

The modal will appear, letting you define the index with just a few clicks:

Visually creating an index in DbVisualizer
Visually creating an index in DbVisualizer

When you are done, click "Execute" to create the index or copy the generated SQL CREATE INDEX query and run it manually.

Wonderful! Creating an index in SQL using DbVisualizer has never been easier.

Best Practices for the SQL CREATE INDEX Statement

When using the CREATE INDEX in SQL, following best practices guarantees efficient indexing and optimized query performance.

Here are some key tips to keep in mind:

  • Index only when necessary: Indexing improves query performance, but excessive indexing can slow down data modification operations. Try to define indexes only on frequently queried columns.
  • Use appropriate index types: Choose the right index type (e.g., clustered, non-clustered, unique) based on query needs.
  • Consider composite indexes: Use multi-column indexes for queries involving multiple columns.
  • Maintain index health: Regularly rebuild or reorganize indexes to optimize performance.

Conclusion

In this guide, you learned what indexes are and what the SQL CREATE INDEX command is, then learned how to use it in MySQL, PostgreSQL, SQL Server, and Oracle. You now know that each database offers a different and unique implementation of the statement.

As shown here, creating indexes becomes much easier with a tool like DbVisualizer. This database client simplifies query execution, data exploration, and table discovery. It also offers advanced features such as query optimization, SQL formatting, and ERD-like schema generation. Try DbVisualizer for free today!

FAQ

What is the CREATE INDEX SQL Server syntax?

The CREATE INDEX SQL Server syntax is consistent across SQL Server, Azure SQL Database, and Azure SQL Managed Instance. Still, the syntax may vary depending on the server version in use. So, it is recommended to refer to the official documentation for the most accurate and up-to-date information.

What are some scenarios where creating an index in SQL is useful?

Creating an index in SQL can make all the difference in scenarios involving:

In general, indexes are most beneficial when applied to columns frequently used in queries. For more information, refer to our guide on how and why to add an index in SQL.

How to create a non clustered index in SQL?

A non-clustered index is a type of index that maintains a sorted copy of specified columns separately from the table's actual data. Unlike clustered indexes, it does not alter the physical order of rows in the table. The syntax for creating a non-clustered index varies by DBMS. For example, in SQL Server it is:

Copy
        
1 CREATE NONCLUSTERED INDEX idx_name ON table_name(column_name_1, ..., column_name_n);

By default, most database systems create a clustered index on the primary key and allow you to specify non-clustered indexes for general optimization.

Is it possible to create an index on a temporary table in SQL?

Yes, it is usually possible to create an index on a temporary table in SQL. Temporary tables are often used for intermediate data storage during complex queries, and indexing them can improve query performance. In detail, teh SQL CREATE INDEX statement can generally also be applied to temporary tables.

What are the benefits of creating an index with a database client?

Creating an index with a database client offers several benefits. It equips you with everything you need to efficiently manage and optimize indexing tasks directly from a graphical interface or command-line tool. Database clients make it easier to create, modify, and drop indexes, while reducing the risk of errors compared to manual SQL scripting.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

MySQL ALTER TABLE Statement: Definitive Guide

author Antonello Zanini tags ALTER TABLE MySQL 11 min 2025-03-06
title

A Complete Guide to the Order of Execution in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-03-03
title

MySQL NOW Function: Get the Current Date and Time

author Antonello Zanini tags DATETIME MySQL 5 min 2025-02-27
title

A Complete Guide to the MySQL CTE Mechanism

author Antonello Zanini tags MySQL 10 min 2025-02-26
title

SQL Operators: A Complete Walkthrough

author Lukas Vileikis tags DbVisualizer MySQL SQL 4 min 2025-02-25
title

How to Truncate Many Tables In SQL at Once

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-02-24
title

PostgreSQL Truncate Table Reset Sequence Guide

author Antonello Zanini tags POSTGRESQL 6 min 2025-02-17
title

Clustered vs Non-Clustered Index: Complete SQL Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-02-13
title

Queries and Subqueries in SQL: The Definitive Guide

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-02-12
title

How to Optimize a Clustered Index Scan in SQL

author Antonello Zanini tags MySQL SQL SQL SERVER 7 min 2025-02-11

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.