Advertisement

Google Ad Slot: content-top

MySQL CREATE INDEX


MySQL CREATE INDEX

In MySQL, an index is used to improve the performance of database queries by allowing faster data retrieval. It creates a structure that helps locate data efficiently without scanning the entire table.


Why Use Indexes?

  • Speeds up SELECT queries.
  • Enhances performance for JOIN operations.
  • Efficient for WHERE, ORDER BY, and GROUP BY operations.
  • Indexes are automatically used by the MySQL optimizer.


CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:


CREATE INDEX index_name
ON table_name (column1, column2);


MySQL CREATE INDEX Example

The SQL statement below creates an index named "idx_student_name" on the "name" column in the "students" table:

Example
CREATE INDEX idx_student_name
ON students(name);

If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:

Example
CREATE INDEX idx_student_city
ON students(student_name, city);

DROP INDEX Statement

The DROP INDEX statement is used to delete an index in a table.

Example
DROP INDEX idx_student_name ON students;