Get the App
SLTechnology News&Howtos  ›  Database  › 

What are the methods of creating indexes in mysql

Shulou Source: shulou.com Published: 2022-06-01 04:30:50 09月26日 Update

Editor to share with you the methods of mysql to create an index, I hope you will learn a lot after reading this article, let's discuss it together!

You can create an index when you execute a CREATE TABLE statement, or you can add an index to a table using CREATE INDEX or ALTER TABLE alone.

1.ALTER TABLE

ALTER TABLE is used to create normal, UNIQUE, or PRIMARY KEY indexes.

ALTER TABLE table_name ADD INDEX index_name (column_list) ALTER TABLE table_name ADD UNIQUE (column_list) ALTER TABLE table_name ADD PRIMARY KEY (column_list)

Where table_name is the name of the table to be indexed, column_list indicates which columns are indexed, and multiple columns are separated by commas. The index name index_name is optional, and by default, MySQL assigns a name based on the first index column. In addition, ALTER TABLE allows multiple tables to be changed in a single statement, so multiple indexes can be created at the same time.

2.CREATE INDEX

CREATE INDEX can add a normal index or an UNIQUE index to a table.

CREATE INDEX index_name ON table_name (column_list) CREATE UNIQUE INDEX index_name ON table_name (column_list)

Table_name, index_name, and column_list have the same meaning as in the ALTER TABLE statement, and the index name is not optional. In addition, PRIMARY KEY indexes cannot be created with CREATE INDEX statements.

After reading this article, I believe you have a certain understanding of mysql's method of creating an index. I would like to know more about it. Welcome to follow the industry information channel. Thank you for your reading!

Tags: Indexes statements methods ordinary multiple articles same between single at the same time name meaning done more knowledge industry information information channels commas channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Apple Shulou Tech Info Redmi