The SQL CREATE INDEX
statement is used to create an index on a table in a database. Indexes are used to improve the speed of data retrieval operations on a table, such as SELECT
queries, by providing faster access to rows based on the indexed columns.
Here's the basic syntax of the CREATE INDEX
statement:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
index_name
: The name of the index to be created. This should be unique within the database.table_name
: The name of the table on which the index is being created.(column1, column2, ...)
: The column or columns on which the index is being created. You can specify one or more columns here.Example:
CREATE INDEX idx_lastname ON employees (last_name);
This statement creates an index named idx_lastname
on the last_name
column of the employees
table. This index will help to speed up queries that involve filtering or sorting by the last_name
column.
It's important to note that while indexes can improve query performance, they also consume additional storage space and can impact the performance of data modification operations (e.g., INSERT
, UPDATE
, DELETE
), as the index needs to be updated along with the underlying data. So, it's essential to carefully consider the columns on which to create indexes based on the