MySQL索引有哪些作用

40次阅读
没有评论

共计 16741 个字符,预计需要花费 42 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章给大家分享的是有关 MySQL 索引有哪些作用的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

一、索引简介
(1) 索引的含义和特定
(2)索引的分类
(3)索引的设计原则

二、创建索引
(1) 创建表的时候创建索引
(2)在已经存在的表上创建索引
(3)删除索引

一、索引简介

索引用于快速找出在某列中有一特定值的行。不使用索引,MySQL 必须从第 1 条记录开始读完整个表,知道找出相关的行。表越大,查询数据所花费的时间越多。如果表中查询的列有一个索引,MySQL 能快速到达某个位置去搜寻数据文件,而不必查看所有数据。

(1)索引的含义和特定

含义:索引是一个单独的、存储在磁盘上的数据库结构,包含着对数据表里所有记录的引用指针。用于快速找出在某个或多个列中有一特定值的行。

索引是在存储引擎中实现的,因此,每种存储引擎的索引都不一定完全相同,并且每种存储引擎也不一定支持所有索引类型。根据存储引擎定义每个表的最大索引数和最大索引长度。所有存储引擎支持每个表至少 16 个索引,总索引长度至少为 256 字节。大多数存储引擎有更高的限制。

MySQL 中索引的存储类型有两种:BTREE 和 HASH,具体和表的存储引擎相关;MyISAM 和 InnoDB 存储引擎只支持 BTREE 索引;MEMORY/HEAL 存储引擎可以支持 HASH 和 BTREE 索引。

索引的优点:
1. 通过创建唯一索引,可以保证数据库表中每一行数据的唯一性。
2. 可以大大加快数据的查询速度。(创建索引的最主要原因)
3. 在实现数据的参考完整性方面,可以加速表和表之间的连接。
4. 在使用分组和排序子句进行数据查询时,也可以显著减少查询中分组和排序的时间。

增加索引的缺点:
1. 创建索引和维护索引耗费时间,随着数据量的增加所耗费的时间也会增加。
2. 索引占用磁盘空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果有大量的索引,索引文件可能比数据文件更快达到最大文件尺寸。
3. 当对表中的数据进行增加、删除和修改时,索引也要动态维护,降低了数据的维护速度。

(2)索引的分类

1. 普通索引和唯一索引(unique)

普通索引是 MySQL 中的基本索引类型,允许在定义索引的列中插入重复值和空值。

唯一索引,索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。

主键索引是一种特殊的唯一索引,不允许有空值。

2. 单列索引和组合索引

单列所以你即一个索引只包含单个列,一个表可以有多个单列索引。

组合索引指在表的多个字段组合上创建的索引,只有在查询条件中使用了这些字段的左边字段时,索引才会被使用。

使用组合索引时遵循最左前缀集合。

3. 全文索引(fulltext)

全文索引类型为 FULLTEXT,在定义索引的列上支持值的全文查找,允许在这些索引列中插入重复值和空值,全文索引可以在 char、varchar 或 text 类型的列上创建。MySQL 中只有 MyISAM 存储引擎支持全文索引。

4. 空间索引(spatial)

空间索引是对空间数据类型的字段建立的索引,MySQL 中的空间数据类型有 4 种,分别为 geometry,point,linestring 和 polygon。MySQL 使用 spatial 关键字进行扩展,使得能够用于创建正规索引类似的语法创建空间索引。创建空间索引的列,必须将其声明为 not null,空间索引只能在存储引擎为 MySQL 的表中创建。

(3)索引的设计原则

索引设计不合理或者缺少索引都会对数据库和应用程序的性能造成障碍。高效的索引对于获得良好的性能非常重要,设计索引时,应该考虑以下准则:

1. 索引并非越多越好。

2. 避免对经常更新的表进行过多的索引,并且索引的列尽可能少。

3. 数据量小的表最好不要使用索引。

4. 在条件表达式中经常用到的不同值较多的列上建立索引,在不同值很少的列上不要建立索引。

5. 当唯一性是某种数据本身的特征时,指定唯一索引。

6. 在频繁进行排序或分组 (进行 group by 或 order by 操作) 的列上建立索引,如果待排序的列有多个,可以在这些列上建立组合索引。

二、创建索引

语法格式:

create table table_name [col_name date_type][unique|fulltext|spatial] [index|key] [index_name] (col_name [length]) [asc | desc]

unique,fulltext 和 spatial 为可选参数,分别表示唯一索引,全文索引和空间索引。

index 与 key 为同义词,两者作用相同,用来指定创建索引。

col_name 为需要创建索引的字段列,该列必须从数据表中定义的多个列中选择。

index_name 指定索引的名称,为可选参数,若不指定,MySQL 默认 col_name 为索引值。

length 为可选参数,表示索引的长度,只有字符串类型的字段才能指定索引长度。

asc 或 desc 指定升序或者降序的索引值存储。

(1)创建表的时候创建索引

①创建普通索引

普通索引是 最基本的索引类型,没有唯一性之类的限制,其作用只是加快对数据的访问速度。

【例 1】在 book 表中的 year_publication 字段上建立普通索引,SQL 语句如下:

mysql  create table book -  (
 -  bookid int not null,
 -  bookname varchar(255) not null,
 -  authors varchar(255) not null,
 -  info varchar(255) null,
 -  comment varchar(255) null,
 -  year_publication year not null,
 -  index(year_publication)
 -  );Query OK, 0 rows affected (0.21 sec)mysql  show create table book \G*************************** 1. row ***************************
 Table: bookCreate Table: CREATE TABLE `book` ( `bookid` int(11) NOT NULL,
 `bookname` varchar(255) NOT NULL,
 `authors` varchar(255) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 `comment` varchar(255) DEFAULT NULL,
 `year_publication` year(4) NOT NULL,
 KEY `year_publication` (`year_publication`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)mysql  explain select * from book where year_publication=1990 \G*************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: book
 partitions: NULL
 type: ref
possible_keys: year_publication key: year_publication
 key_len: 1
 ref: const rows: 1
 filtered: 100.00
 Extra: NULL1 row in set, 1 warning (0.00 sec)

explan 语句输出结果的各行解释如下:

select_type 行指定所使用的 select 查询类型,这里值为 simple,表示简单的 select,不使用 union 或子查询。其他可能的取值有 primary、union、subquery 等。

table 行指定数据库读取的数据表的名字,它们按被读取的先后顺序排列。

type 行指定了本数据库表与其他数据库表之间的关联关系,可能的取值有 system、const、eq_ref、ref、range、index 和 all。

possible_keys 行. 给出了 MySQL 在搜索数据记录时可选用的各个索引。

key 行是 MySQL 实际选用的索引。

key_len 行给出索引按字节计算的长度,key_len 数值越小,表示越快。

ref 行给出了关联关系中另一个数据表里数据列的名字。

rows 行是 MySQL 在执行这个查询时预计会从这个数据表里读出的数据行的个数。

extra 行提供了与关联操作有关的信息。

可以看到,possible_key 和 key 的值都为 year_publication,查询时使用了索引。

②创建唯一索引

创建唯一索引的主要原因是减少查询索引列操作的执行时间,尤其是对比较庞大的数据表。它与前面的普通索引类似,不同就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。

【例 2】创建一个表 t1,在表中的 id 字段上使用 unique 关键字创建唯一索引。

mysql  create table t1 -  (
 -  id int not null
 -  ,name char(30) not null,
 -  unique index uniqidx(id)
 -  );Query OK, 0 rows affected (0.27 sec)mysql  show create table t1 \G*************************** 1. row ***************************
 Table: t1Create Table: CREATE TABLE `t1` ( `id` int(11) NOT NULL,
 `name` char(30) NOT NULL,
 UNIQUE KEY `uniqidx` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)

③创建单列索引

单列索引是在数据表中的某一个字段上创建的索引,一个表中可以创建多个单列索引。

【例 3】创建一个表 t2,在表中的 name 字段上创建单列索引。

mysql  create table t2 -  (
 -  id int not null,
 -  name char(50) null,
 -  index singleidx(name(20))
 -  );Query OK, 0 rows affected (0.06 sec)mysql  show create table t2 \G*************************** 1. row ***************************
 Table: t2Create Table: CREATE TABLE `t2` ( `id` int(11) NOT NULL,
 `name` char(50) DEFAULT NULL,
 KEY `singleidx` (`name`(20))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.01 sec)

由结果可以看到,id 字段上已经成功建立了一个名为 SingleIdx 的单列索引,索引长度为 20。

④创建组合索引
组合索引就是在多个字段上创建一个索引。

【例 4】创建表 t3,在表中的 id、name 和 age 字段上建立组合索引,SQL 语句如下:

mysql  create table t3 -  (
 -  id int not null,
 -  name char(30) not null,
 -  age int not null,
 -  info varchar(255),
 -  index mulitiidx(id,name,age)
 -  );Query OK, 0 rows affected (0.07 sec)mysql  show create table t3 \G*************************** 1. row ***************************
 Table: t3Create Table: CREATE TABLE `t3` ( `id` int(11) NOT NULL,
 `name` char(30) NOT NULL,
 `age` int(11) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 KEY `mulitiidx` (`id`,`name`,`age`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)mysql  explain select * from t3 where id = 1 and name =  joe  \G*************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: t3
 partitions: NULL
 type: ref
possible_keys: mulitiidx key: mulitiidx
 key_len: 124
 ref: const,const rows: 1
 filtered: 100.00
 Extra: Using index condition1 row in set, 1 warning (0.06 sec)

组合索引起到几个索引的作用,但是使用时并不是随便查询哪个字段都可以使用索引,而是遵循从 最左前缀 : 利用索引中最左边的列集来匹配行,这样的列集称为最左前缀。

例如,这里由 id,name,age 三个字段构成的索引,索引行中按 id/nam/age 的顺序粗放,索引可以搜索字段组合:(id,name,age)、(id、name)或者 id。如果列不构成索引最左面的前缀,MySQL 不能使用局部索引,如 (age) 或(name,age)组合则不能使用索引查询。查询 id 和 name 字段时,使用了 multiidx 的索引,如果查询 (name,age) 组合或者单独查询 name 和 age 字段,索引为 null。

⑤创建全文索引

fulltext 全文索引可以用于全文搜索。只有 MyISAM 存储引擎支持 fulltext 索引,并且只为 char、varchar 和 text 列创建索引。索引总是对整列进行,不支持局部 (前缀) 索引。

【例 5】创建表 t4,在表中的 info 字段上建立全文索引,SQL 语句如下:

mysql  create table t4 -  (
 -  id int not null,
 -  name char(30) not null,
 -  age int not null,
 -  info varchar(255),
 -  fulltext index fulltxtidx(info)
 -  )engine=MyISAM;Query OK, 0 rows affected (0.08 sec)mysql  show create table t4 \G*************************** 1. row ***************************
 Table: t4Create Table: CREATE TABLE `t4` ( `id` int(11) NOT NULL,
 `name` char(30) NOT NULL,
 `age` int(11) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 FULLTEXT KEY `fulltxtidx` (`info`)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)

全文索引非常适合于大型数据集,对于小的数据集,它的用处比较小。

6. 创建空间索引

空间索引必须在 MyISAM 类型的表中创建,且空间类型的字段必须为非空。

【例 6】创建表 t5,在空间类型为 geometry 的字段上创建空间索引,SQL 语句如下:

mysql  create table t5 -  ( g geometry not null,spatial index spatidx(g) ) ENGINE=MyISAM;Query OK, 0 rows affected, 1 warning (0.07 sec)mysql  show create table t5 \G*************************** 1. row ***************************
 Table: t5Create Table: CREATE TABLE `t5` (
 `g` geometry NOT NULL,
 SPATIAL KEY `spatidx` (`g`)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.05 sec)

可以看到,t5 表的 g 字段上创建了名称为 spatIdx 的空间索引。创建时指定空间类型字段值的非空约束,并且表的存储引擎为 MyISAM。

(2)在已经存在的表上创建索引

在已经存在的表中创建索引,可以使用 alter table 语句或者 create index 语句。

1. 使用 alter table 语句创建索引

基本语法:

alter table table_name add [unique|fulltext|spatial] [index|key][index_name] (col_name[length],...) [asc |dec]

【例 7】在 book 表中的 bookname 字段上建立名为 BkNameIdx 的普通索引。

添加索引之前,show index 语句查看指定表中创建的索引:

mysql  show index from book \G*************************** 1. row ***************************
 Table: book
 Non_unique: 1
 Key_name: year_publication
 Seq_in_index: 1
 Column_name: year_publication
 Collation: A
 Cardinality: 0
 Sub_part: NULL
 Packed: NULL
 Null:
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL1 row in set (0.10 sec)

其中,各个主要参数的含义为;

table 表示创建索引的表。

Non_unique 表示索引非唯一,1 表示非唯一,0 表示唯一。

Key_name 表示索引的名称。

Seq_in_index 表示该字段在索引中的位置,单列索引该值为 1,组合索引为每个字段在索引定义中的顺序。

Column_name 表示定义索引的列字段。

Sub_part 表示索引的长度。

Null 表示该字段是否能为空值。

Index_type 表示索引类型。

可以看到 book 表中已经存在一个索引,即 year_publication 索引,该索引为非唯一索引,下面使用 alter table 在 bookname 字段上添加索引,SQL 语句如下:

mysql  alter table book add index bknameidx( bookname(30) );Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show index from book \G*************************** 1. row ***************************
 Table: book
 Non_unique: 1
 Key_name: year_publication
 Seq_in_index: 1
 Column_name: year_publication
 Collation: A
 Cardinality: 0
 Sub_part: NULL
 Packed: NULL
 Null:
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL*************************** 2. row ***************************
 Table: book
 Non_unique: 1
 Key_name: bknameidx
 Seq_in_index: 1
 Column_name: bookname
 Collation: A
 Cardinality: 0
 Sub_part: 30
 Packed: NULL
 Null:
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL2 rows in set (0.05 sec)

可以看到表中有了两个索引,另一个为通过 alter table 语句添加的名称为 bknameidx 的索引,该索引为非唯一索引,长度为 30。

【例 8】在 book 表的 bookid 字段上建立名称为 uniqididx 的唯一索引,SQL 语句如下:

mysql  alter table book add unique index uniqididx(bookid);Query OK, 0 rows affected (0.17 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show index from book \G1...2...*************************** 3. row ***************************
 Table: book
 Non_unique: 1
 Key_name: bknameidx
 Seq_in_index: 1
 Column_name: bookname
 Collation: A
 Cardinality: 0
 Sub_part: 30
 Packed: NULL
 Null:
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL3 rows in set (0.01 sec)

可以看到,Non_unique 的属性值为 0,表示名称为 Uniqididx 的索引为唯一索引,创建唯一索引成功。

【例 9】在 book 表的 comment 字段上建立单列索引,SQL 语句如下:

mysql  alter table book add index bkcmtidx ( comment(50) );Query OK, 0 rows affected (0.11 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show index from book \G1...2...3...*************************** 4. row ***************************
 Table: book
 Non_unique: 1
 Key_name: bkcmtidx
 Seq_in_index: 1
 Column_name: comment
 Collation: A
 Cardinality: 0
 Sub_part: 50
 Packed: NULL
 Null: YES
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL4 rows in set (0.01 sec)

可以看到,语句执行之后再 book 表的 comment 字段上建立了名称为 bkcmtidx 的索引,长度为 50,在查询时,只需要检索前 50 个字符。

【例 10】在 book 表的 authors 和 info 字段上建立组合索引,SQL 语句如下:

mysql  alter table book add index bkauandinfoidx (authors(30),info(50) );Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show index from book \G1...2...3...4...*************************** 5. row ***************************
 Table: book
 Non_unique: 1
 Key_name: bkauandinfoidx
 Seq_in_index: 1
 Column_name: authors
 Collation: A
 Cardinality: 0
 Sub_part: 30
 Packed: NULL
 Null:
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL*************************** 6. row ***************************
 Table: book
 Non_unique: 1
 Key_name: bkauandinfoidx
 Seq_in_index: 2
 Column_name: info
 Collation: A
 Cardinality: 0
 Sub_part: 50
 Packed: NULL
 Null: YES
 Index_type: BTREE
 Comment:
Index_comment:
 Visible: YES
 Expression: NULL6 rows in set (0.06 sec)

可以看到名称为 bkauandinfoidx 的索引由两个字段组成,authors 字段长度为 30,在组合索引中的序号为 1,该字段不允许空值 null;info 字段长度为 50,在组合索引中的序号为 2,该字段可以为空值 null。

【例 11】创建表 t6,在 t6 表上使用 alter table 创建全文索引,SQL 语句如下:

mysql  create table t6 -  (
 -  id int not null,
 -  info char(255)
 -  )ENGINE=MyISAM;Query OK, 0 rows affected (0.07 sec)mysql  alter table t6 add fulltext index infofiidx( info );Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show index from t6 \G*************************** 1. row ***************************
 Table: t6
 Non_unique: 1
 Key_name: infofiidx
 Seq_in_index: 1
 Column_name: info
 Collation: NULL
 Cardinality: NULL
 Sub_part: NULL
 Packed: NULL
 Null: YES
 Index_type: FULLTEXT Comment:
Index_comment:
 Visible: YES
 Expression: NULL1 row in set (0.05 sec)

可以看到,t6 表中已经创建了名称为 infoftidx 的索引,该索引在 info 字段上创建,类型为 fulltext,允许空值。

【例 12】创建表 t7,t7 的空间类型字段 g 上创建名称为 spatidx 的空间索引,SQL 语句如下:

mysql  create table t7(g geometry not null)ENGINE=MyISAM;Query OK, 0 rows affected (0.07 sec)mysql  alter table t7 add spatial index spatidx(g);Query OK, 0 rows affected, 1 warning (0.06 sec)Records: 0 Duplicates: 0 Warnings: 1mysql  show index from t7 \G*************************** 1. row ***************************
 Table: t7
 Non_unique: 1
 Key_name: spatidx
 Seq_in_index: 1
 Column_name: g
 Collation: A
 Cardinality: NULL
 Sub_part: 32
 Packed: NULL
 Null:
 Index_type: SPATIAL Comment:
Index_comment:
 Visible: YES
 Expression: NULL1 row in set (0.01 sec)

可以看到,t7 表的 g 字段上创建了名为 spatidx 的空间索引。

2. 使用 create index 创建索引

create index 语句可以在已经存在的表上添加索引,MySQL 中 create index 被映射到一个 alter table 语句上,基本语法为:

create [unique|fulltext|spatial] index index_nameon table_name (col_name[length],...) [asc|desc]

可以看到 create index 语句和 alter index 语句的语法基本一样,只是关键字不同,使用相同的表 book,假设该表中没有任何索引值,创建 book 表语句如下:

create table book(bookid int not null,bookname varchar(255) not null,authors varchar(255) not null,info varchar(255) null,comment varchar(255) null,year_publication year not null);

【例 13】在 book 表的 bookname 字段上建立名为 BkNameIdx 的普通索引,SQL 语句如下:

mysql  create index BkNameOdx on book(bookname);Query OK, 0 rows affected (0.10 sec)Records: 0 Duplicates: 0 Warnings: 0

【例 14】在 book 表的 bookid 字段上建立名为 UniqidIdx 的唯一索引,SQL 语句如下:

mysql  create unique index UniqiiIdx on book(bookid);Query OK, 0 rows affected (0.05 sec)Records: 0 Duplicates: 0 Warnings: 0

【例 15】在 book 表的 comment 字段上建立单列索引,SQL 语句如下:

mysql  create index BkcmtIdx on book(bookid);Query OK, 0 rows affected (0.08 sec)Records: 0 Duplicates: 0 Warnings: 0

【例 16】在 book 表的 authors 和 info 字段上建立组合索引,SQL 语句如下:

mysql  create index BkAuAndInfoIdx on book (authors(20),info(50));Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0

【例 17】删除表 t6,重新建立表 t6,在 t6 表中使用 create index 语句,在 char 类型的 info 字段上创建全文索引。

mysql  drop table t6;Query OK, 0 rows affected (0.02 sec)mysql  create table t6 -  (
 -  id int not null,
 -  info char(255)
 -  )ENGINE=MyISAM;Query OK, 0 rows affected (0.06 sec)mysql  create fulltext index infoftidx on t6(info);Query OK, 0 rows affected (0.07 sec)Records: 0 Duplicates: 0 Warnings: 0

【例 18】删除表 t7,重新创建表 t7,在 t7 表中使用 create index 语句,在空间数据类型字段 g 上创建名称为 spatIdx 的空间索引。

mysql  drop table t7;Query OK, 0 rows affected (0.06 sec)mysql  create table t7 (g geometry not null )ENGINE=MyISAM;Query OK, 0 rows affected (0.06 sec)mysql  create spatial index spatIdx on t7 (g);Query OK, 0 rows affected, 1 warning (0.07 sec)Records: 0 Duplicates: 0 Warnings: 1

(3)删除索引

MySQL 中删除索引使用 alter table 或者 drop index 语句,两者可实现相同的功能,drop index 语句在内部被映射到一个 alter table 语句中。

1. 使用 alter table 删除索引

alter table 删除索引的基本语法格式:

alter table table_name drop index index_name

【例 1】删除 book 表中的名称为 UniqidIdx 的唯一索引。

mysql  show create table book \G*************************** 1. row ***************************
 Table: bookCreate Table: CREATE TABLE `book` ( `bookid` int(11) NOT NULL,
 `bookname` varchar(255) NOT NULL,
 `authors` varchar(255) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 `comment` varchar(255) DEFAULT NULL,
 `year_publication` year(4) NOT NULL,
 UNIQUE KEY `UniqiIdx` (`bookid`),
 KEY `BkNameOdx` (`bookname`),
 KEY `BkcmtIdx` (`bookid`),
 KEY `BkAuAndInfoIdx` (`authors`(20),`info`(50))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)mysql  alter table book drop index UniqiIdx;Query OK, 0 rows affected (0.19 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show create table book \G*************************** 1. row ***************************
 Table: bookCreate Table: CREATE TABLE `book` ( `bookid` int(11) NOT NULL,
 `bookname` varchar(255) NOT NULL,
 `authors` varchar(255) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 `comment` varchar(255) DEFAULT NULL,
 `year_publication` year(4) NOT NULL,
 KEY `BkNameOdx` (`bookname`),
 KEY `BkcmtIdx` (`bookid`),
 KEY `BkAuAndInfoIdx` (`authors`(20),`info`(50))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)

可以看到,book 表中已经没有名称为 UniqidIdx 的唯一索引,删除索引成功。

注意:添加 auto_increment 约束字段的唯一索引不能被删除。

2. 使用 drop index 语句删除索引

drop index 语句删除索引的基本语法格式:

drop index inde _name on table_name

【例 2】删除 book 表中名称为 BkAuAndInfoIdx 的组合索引,SQL 语句如下:

mysql  drop index BkAuAndInfoIdx on book;Query OK, 0 rows affected (0.08 sec)Records: 0 Duplicates: 0 Warnings: 0mysql  show create table book \G*************************** 1. row ***************************
 Table: bookCreate Table: CREATE TABLE `book` ( `bookid` int(11) NOT NULL,
 `bookname` varchar(255) NOT NULL,
 `authors` varchar(255) NOT NULL,
 `info` varchar(255) DEFAULT NULL,
 `comment` varchar(255) DEFAULT NULL,
 `year_publication` year(4) NOT NULL,
 KEY `BkNameOdx` (`bookname`),
 KEY `BkcmtIdx` (`bookid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)

可以看到,book 表中已经没有名称为 BkAuAndInfoIdx 的组合索引,删除索引成功。

注意:删除表中的列时,如果要删除的列为索引的组成部分,则该部分也会从索引中删除。如果组成索引的所有列都被删除,那么整个索引将被删除。

感谢各位的阅读!关于“MySQL 索引有哪些作用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计16741字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)