Create, view, delete, modify indexes in Mysql Create, view, delete, modify indexes in Mysql

From: Create, view, delete, modify indexes in Mysql

 

create index

The syntax for creating an index in MySQL is as follows:

1
2
3
CREATE  [ UNIQUE |FULLTEXT|SPATIAL] INDEX  index_name
[USING index_type]
ON  table_name (index_col_name,...)

The corresponding syntax variable information is as follows:

The three keywords in brackets in [UNIQUE|FULLTEXT|SPATIAL]
indicate the index type to be created, and they respectively indicate three different index types: unique index, full-text index, and spatial index. If we don't specify any keyword, it defaults to normal index.
index_name
index_name indicates the name of the index, which is defined by the user to facilitate future management operations such as modification of the index.
index_type
index_type indicates the specific implementation of the index. In MySQL, there are two different forms of index - BTREE index and HASH index. Only BTREE can be used in tables whose storage engine is MyISAM and InnoDB, and its default value is BTREE; in tables whose storage engine is MEMORY or HEAP, two types of indexes, HASH and BTREE, can be used, and the default value is HASH.
index_col_name
index_col_name indicates the name of the field that needs to be indexed. We can also create a composite index for multiple fields by separating multiple field names with commas.
In addition, for fields of CHAR or VARCHAR type, we can also use only the first part of the field content to create an index, just add a command like (length) after the corresponding field name, indicating that only the field content needs to be used. the first length characters to create the index. Here, we take the username field of the User table (of type VARCHAR(50)) as an example, and use the 6-character prefix of the username field to create an index.

1
CREATE  INDEX  idx_user_username ON  user  (username(6));

Since the first 6 characters of most fields are usually different, this index is not much slower than an index created with the entire contents of the field. In addition, creating an index using a portion of a field can make the index file significantly smaller, saving a lot of disk space, potentially increasing the speed of INSERT operations.

In MySQL, the maximum prefix length is 255 bytes. For tables whose storage engine is MyISAM or InnoDB, the prefix can be up to 1000 bytes long.

It must be noted that in MySQL, for fields of large data types such as TEXT and BLOB, a prefix length (length) must be given to successfully create an index.

Note 1: The above syntax for creating an index also has the following variants:

1
2
ALTER  TABLE  table_name
ADD  [ UNIQUE |FULLTEXT|SPATIAL] INDEX  index_name (index_col_name,...) [USING index_type]

Note 2: In MySQL, you can add indexes to columns with NULL values ​​or columns with data type TEXT or BLOB only when the storage engine of the data table is MyISAM, InnoDB or BDB.

drop index

The method of dropping an index in MySQL is very simple, and its full syntax is as follows:

1
2
3
--删除指定表中指定名称的索引
ALTER  TABLE  table_name
DROP  INDEX  index_name;

Here, we write an SQL statement to delete the index idx_user_username in the above example of creating an index. The code details are as follows:

1
2
3
--删除名称为idx_user_username的索引
ALTER  TABLE  user
DROP  INDEX  idx_user_username;

modify index

There is no direct instruction to modify the index in MySQL. In general, we need to delete the original index first, and then create an index with the same name as needed, so as to implement the index modification operation in disguise.

1
2
3
4
5
--先删除
ALTER  TABLE  user
DROP  INDEX  idx_user_username;
--再以修改后的内容创建同名索引
CREATE  INDEX  idx_user_username ON  user  (username(8));

view index

In MySQL, it is also very simple to view the indexes in a database table, just use either of the following two commands.

1
2
3
4
--如果查看索引前,没有使用user db_name等命令指定具体的数据库,则必须加上FROM db_name
SHOW INDEX  FROM  table_name [ FROM  db_name]
--如果查看索引前,没有使用user db_name等命令指定具体的数据库,则必须加上db_name.前缀
SHOW INDEX  FROM  [db_name.]table_name
 
 

From: Create, view, delete, modify indexes in Mysql

 

create index

The syntax for creating an index in MySQL is as follows:

1
2
3
CREATE  [ UNIQUE |FULLTEXT|SPATIAL] INDEX  index_name
[USING index_type]
ON  table_name (index_col_name,...)

The corresponding syntax variable information is as follows:

The three keywords in brackets in [UNIQUE|FULLTEXT|SPATIAL]
indicate the index type to be created, and they respectively indicate three different index types: unique index, full-text index, and spatial index. If we don't specify any keyword, it defaults to normal index.
index_name
index_name indicates the name of the index, which is defined by the user to facilitate future management operations such as modification of the index.
index_type
index_type indicates the specific implementation of the index. In MySQL, there are two different forms of index - BTREE index and HASH index. Only BTREE can be used in tables whose storage engine is MyISAM and InnoDB, and its default value is BTREE; in tables whose storage engine is MEMORY or HEAP, two types of indexes, HASH and BTREE, can be used, and the default value is HASH.
index_col_name
index_col_name indicates the name of the field that needs to be indexed. We can also create a composite index for multiple fields by separating multiple field names with commas.
In addition, for fields of CHAR or VARCHAR type, we can also use only the first part of the field content to create an index, just add a command like (length) after the corresponding field name, indicating that only the field content needs to be used. the first length characters to create the index. Here, we take the username field of the User table (of type VARCHAR(50)) as an example, and use the 6-character prefix of the username field to create an index.

1
CREATE  INDEX  idx_user_username ON  user  (username(6));

Since the first 6 characters of most fields are usually different, this index is not much slower than an index created with the entire contents of the field. In addition, creating an index using a portion of a field can make the index file significantly smaller, saving a lot of disk space, potentially increasing the speed of INSERT operations.

In MySQL, the maximum prefix length is 255 bytes. For tables whose storage engine is MyISAM or InnoDB, the prefix can be up to 1000 bytes long.

It must be noted that in MySQL, for fields of large data types such as TEXT and BLOB, a prefix length (length) must be given to successfully create an index.

Note 1: The above syntax for creating an index also has the following variants:

1
2
ALTER  TABLE  table_name
ADD  [ UNIQUE |FULLTEXT|SPATIAL] INDEX  index_name (index_col_name,...) [USING index_type]

Note 2: In MySQL, you can add indexes to columns with NULL values ​​or columns with data type TEXT or BLOB only when the storage engine of the data table is MyISAM, InnoDB or BDB.

drop index

The method of dropping an index in MySQL is very simple, and its full syntax is as follows:

1
2
3
--删除指定表中指定名称的索引
ALTER  TABLE  table_name
DROP  INDEX  index_name;

Here, we write an SQL statement to delete the index idx_user_username in the above example of creating an index. The code details are as follows:

1
2
3
--删除名称为idx_user_username的索引
ALTER  TABLE  user
DROP  INDEX  idx_user_username;

modify index

There is no direct instruction to modify the index in MySQL. In general, we need to delete the original index first, and then create an index with the same name as needed, so as to implement the index modification operation in disguise.

1
2
3
4
5
--先删除
ALTER  TABLE  user
DROP  INDEX  idx_user_username;
--再以修改后的内容创建同名索引
CREATE  INDEX  idx_user_username ON  user  (username(8));

view index

In MySQL, it is also very simple to view the indexes in a database table, just use either of the following two commands.

1
2
3
4
--如果查看索引前,没有使用user db_name等命令指定具体的数据库,则必须加上FROM db_name
SHOW INDEX  FROM  table_name [ FROM  db_name]
--如果查看索引前,没有使用user db_name等命令指定具体的数据库,则必须加上db_name.前缀
SHOW INDEX  FROM  [db_name.]table_name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966825&siteId=291194637