共計 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 問一下細節