Index
A database index is a data structure that improves the speed of data retrieval in the database. Indexes on a table consist of one or more columns of ordered data with links to specific rows in a table.
By matching the values in the index, the database management system can quickly retrieve the corresponding row without having to search every row in the table.
Tables are indexed on their primary key columns, and many database systems require an index on a foreign key column as well. It is also common practice to place indexes on columns that are likely to be queried often.
SQL Example
Index creation can vary from database to database, but in standard SQL it consists of:
- Using the
CREATE INDEXstatement, followed by a name for the index. - Applying the
ONclause to the table name, followed by a list of the columns to be indexed.
In the example below, adding an index on the region field of a sales table looks like this:
CREATE INDEX sales_by_regionON sales (region);
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn General on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.17 hours
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours