How to implement addition, deletion, modification and query of mysql index?
青灯夜游
Release: 2020-10-14 16:45:56
Original
3282 people have browsed it
In mysql, you can use the "CREATE INDEX" statement to add an index; use the "SHOW INDEX" statement to view the index; use the "DROP INDEX" statement to delete the index; and modify the index by deleting the original index. Then create an index with the same name as needed to modify the index.
Creating an index refers to establishing an index on one or more columns of a table, which can improve the access speed to the table. Creating indexes is important for the efficient operation of a MySQL database.
Basic syntax
MySQL provides three methods of creating indexes:
1) Use the CREATE INDEX statement
You can use the CREATE INDEX statement specifically for creating indexes to create an index on an existing table, but this statement cannot create a primary key.
Syntax format:
CREATE <索引名> ON <表名> (<列名> [<长度>] [ ASC | DESC])
Copy after login
The syntax description is as follows:
: Specify the index name. A table can create multiple indexes, but each index has a unique name in the table.
: Specify the name of the table to create an index.
: Specify the column name to create an index. You can usually consider using columns that frequently appear in the JOIN clause and WHERE clause in the query statement as index columns.
: Optional. Specifies that the length characters preceding the column are used to create the index. Creating an index using part of a column can help reduce the size of the index file and save the space occupied by the index columns. In some cases, only the prefix of a column can be indexed. The length of an index column has a maximum limit of 255 bytes (1000 bytes for MyISAM and InnoDB tables). If the length of an index column exceeds this limit, it can only be indexed using the column's prefix. In addition, columns of type BLOB or TEXT must also use prefix indexes.
ASC|DESC: Optional. ASC specifies that the index is sorted in ascending order, DESC specifies that the index is sorted in descending order, and the default is ASC.
2) Use the CREATE TABLE statement
The index can also be created while creating the table (CREATE TABLE). Add the following statements to the CREATE TABLE statement. Syntax format:
CONSTRAINT PRIMARY KEY [索引类型] (<列名>,…)
Copy after login
Add this statement in the CREATE TABLE statement to create the primary key of the table when creating a new table.
Syntax format:
KEY | INDEX [<索引名>] [<索引类型>] (<列名>,…)
Copy after login
Add this statement in the CREATE TABLE statement to create an index for the table while creating a new table.
Syntax format:
UNIQUE [ INDEX | KEY] [<索引名>] [<索引类型>] (<列名>,…)
Copy after login
Add this statement in the CREATE TABLE statement to create a unique index for the table while creating a new table.
Syntax format:
FOREIGN KEY <索引名> <列名>
Copy after login
Add this statement in the CREATE TABLE statement to create a foreign key to the table while creating a new table.
When using the CREATE TABLE statement to define column options, you can create a primary key by adding PRIMARY KEY directly after a column definition. When the primary key is a multi-column index composed of multiple columns, this method cannot be used. It can only be implemented by adding a PRIMARY KRY (,...) clause at the end of the statement. .
3) Use the ALTER TABLE statement
The CREATE INDEX statement can create an index on an existing table, and the ALTER TABLE statement can also create an index on an existing table. Create index. You can add indexes to an existing table while modifying the table using the ALTER TABLE statement. The specific method is to add one or more of the following syntax components to the ALTER TABLE statement.
Syntax format:
ADD INDEX [<索引名>] [<索引类型>] (<列名>,…)
Copy after login
Add this syntax component in the ALTER TABLE statement to add an index to the table while modifying the table.
Syntax format:
ADD PRIMARY KEY [<索引类型>] (<列名>,…)
Copy after login
Add this syntax component in the ALTER TABLE statement to add a primary key to the table while modifying the table.
Syntax format:
ADD UNIQUE [ INDEX | KEY] [<索引名>] [<索引类型>] (<列名>,…)
Copy after login
Add this syntax component in the ALTER TABLE statement to add a unique index to the table while modifying the table.
Syntax format:
ADD FOREIGN KEY [<索引名>] (<列名>,…)
Copy after login
Add this syntax component in the ALTER TABLE statement to add a foreign key to the table while modifying the table.
View mysql index
After the index creation is completed, you can use SQL statements to view the existing index. In MySQL, you can use the SHOW INDEX statement to view the indexes created on a table.
The syntax format for viewing the index is as follows:
SHOW INDEX FROM <表名> [ FROM <数据库名>]
Copy after login
The syntax description is as follows:
##
: Specify the data table to be viewed for the index name.
: Specify the database where the data table that needs to be viewed index is located, which can be omitted. For example, the SHOW INDEX FROM student FROM test; statement means to view the index of the student data table in the test database.
ExampleSQL statements and running results are as follows.
mysql> SHOW INDEX FROM tb_stu_info2\G
*************************** 1. row ***************************
Table: tb_stu_info2
Non_unique: 0
Key_name: height
Seq_in_index: 1
Column_name: height
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.03 sec)
mysql> DROP INDEX height
-> ON tb_stu_info;
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE tb_stu_info\G
*************************** 1. row ***************************
Table: tb_stu_info
Create Table: CREATE TABLE `tb_stu_info` (
`id` int(11) NOT NULL,
`name` char(45) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.00 sec)
Copy after login
【实例 2】删除表 tb_stu_info2 中名称为 id 的索引,输入的 SQL 语句和执行结果如下所示。
mysql> ALTER TABLE tb_stu_info2
-> DROP INDEX height;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE tb_stu_info2\G
*************************** 1. row ***************************
Table: tb_stu_info2
Create Table: CREATE TABLE `tb_stu_info2` (
`id` int(11) NOT NULL,
`name` char(45) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`height` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.00 sec)
Copy after login
The above is the detailed content of How to implement addition, deletion, modification and query of mysql index?. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn