📜  mysql alter table add index - SQL (1)

📅  最后修改于: 2023-12-03 15:33:01.269000             🧑  作者: Mango

MySql ALTER TABLE ADD INDEX - SQL

MySQL is one of the most popular relational database management systems. ALTER TABLE command is used to add an index to the existing table. Indexes can be added to the table to make queries faster by speeding up the process of locating the rows.

Syntax

The syntax for adding a new index to the table is:

ALTER TABLE table_name ADD INDEX index_name (column1, column2, ...);

The ADD INDEX keyword is used to add an index. The index_name is the name of the index. The column1, column2, etc are the name of the columns that need to be indexed.

Example

Consider a users table with the following schema:

CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL
);

We can add an index for the email column using the following SQL command:

ALTER TABLE users ADD INDEX email_index (email);
Explanation

After running this command, a new index named email_index is created on the email column of the users table. An index improves the performance of queries that use the WHERE clause to filter data based on the value of the column.

Conclusion

In conclusion, ALTER TABLE ADD INDEX is used to add an index to a table to increase the performance of queries that use the indexed columns. It is an important command that every programmer should be familiar with when working with MySQL databases.