索引分類(lèi)
1.普通索引:不附加任何條件,可創(chuàng)建在任何數(shù)據(jù)類(lèi)型中
2.唯一性索引:使用unique參數(shù)可以設(shè)置索引為唯一性索引,在創(chuàng)建索引時(shí),該索引的值必須唯一,主鍵就是一種唯一性索引
3.全文索引:使用fulltext參數(shù)可以設(shè)置索引為全文索引。全文索引只能創(chuàng)建在char、varchar或text類(lèi)型的字段上。查詢數(shù)據(jù)量較大的字符串類(lèi)型字段時(shí),效果明顯。但只有MyISAM存儲(chǔ)引擎支持全文檢索
4.單列索引:在表中單個(gè)字段上創(chuàng)建的索引,單列索引可以是任何類(lèi)型,只要保證索引只對(duì)應(yīng)一個(gè)一個(gè)字段
5.多列索引:在表中多個(gè)字段上創(chuàng)建的索引,該索引指向創(chuàng)建時(shí)對(duì)應(yīng)的多個(gè)字段
6.空間索引:使用spatial參數(shù)可以設(shè)置索引為空間索引,空間索引只能建立在空間數(shù)據(jù)類(lèi)型上比如geometry,并且不能為空,目前只有MyISAM存儲(chǔ)引擎支持
(1)在創(chuàng)建表時(shí)創(chuàng)建索引
創(chuàng)建普通索引
mysql> create table index1(
-> id int,
-> name varchar(20),
-> sex boolean,
-> index(id)
-> );
Query OK, 0 rows affected (0.11 sec)
此處在id字段上創(chuàng)建索引,show create table可查看
創(chuàng)建唯一性索引
mysql> create table index2(
-> id int unique,
-> name varchar(20),
-> unique index index2_id(id ASC)
-> );
Query OK, 0 rows affected (0.12 sec)
此處使用id字段創(chuàng)建了一個(gè)名為index2_id的索引
這里的id字段可以不設(shè)置唯一性約束,但這樣一來(lái)索引就沒(méi)有作用
創(chuàng)建全文索引
mysql> create table index3(
-> id int,
-> info varchar(20),
-> fulltext index index3_info(info)
-> )engine=MyISAM;
Query OK, 0 rows affected (0.07 sec)
要注意創(chuàng)建全文索引時(shí)只能使用MyISAM存儲(chǔ)引擎
創(chuàng)建單列索引
mysql> create table index4(
-> id int,
-> subject varchar(30),
-> index index4_st(subject(10))
-> );
Query OK, 0 rows affected (0.12 sec)
此處subject字段長(zhǎng)度是30,而索引長(zhǎng)度則是10
這么做的目的在于提高查詢速度,對(duì)于字符型的數(shù)據(jù)不用查詢?nèi)啃畔?/p>
創(chuàng)建多列索引
mysql> create table index5(
-> id int,
-> name varchar(20),
-> sex char(4),
-> index index5_ns(name,sex)
-> );
Query OK, 0 rows affected (0.10 sec)
可以看出,這里使用了name字段和sex字段創(chuàng)建索引列
創(chuàng)建空間索引
mysql> create table index6(
-> id int,
-> space geometry not null,
-> spatial index index6_sp(space)
-> )engine=MyISAM;
Query OK, 0 rows affected (0.07 sec)
這里需要注意空間space字段不能為空,還有存儲(chǔ)引擎
(2)在已經(jīng)存在的表上創(chuàng)建索引
創(chuàng)建普通索引
mysql> create index index7_id on example0(id);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里在現(xiàn)有表的id字段上創(chuàng)建了一條名為index7_id的索引
創(chuàng)建唯一性索引
mysql> create unique index index8_id on example1(course_id);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
此處只需要在index關(guān)鍵字前面加上unique即可
至于表中的course_id字段,最要也設(shè)置唯一性約束條件
創(chuàng)建全文索引
mysql> create fulltext index index9_info on example2(info);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
fulltext關(guān)鍵字用來(lái)設(shè)置全文引擎,此處的表必須是MyISAM存儲(chǔ)引擎
創(chuàng)建單列索引
mysql> create index index10_addr on example3(address(4));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
此表中address字段的長(zhǎng)度是20,這里只查詢4字節(jié),不需要全部查詢
創(chuàng)建多列索引
mysql> create index index11_na on example4(name,address);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
索引創(chuàng)建好之后,查詢中必須有name字段才能使用
創(chuàng)建空間索引
mysql> create spatial index index12_line on example5(space);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里需要注意存儲(chǔ)引擎是MyISAM,還有空間數(shù)據(jù)類(lèi)型
(3)用alter table語(yǔ)句來(lái)創(chuàng)建索引
創(chuàng)建普通索引
mysql> alter table example6 add index index13_n(name(20));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建唯一性索引
mysql> alter table example8 add fulltext index index15_info(info);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建單列索引
mysql> alter table example9 add index index16_addr(address(4));
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建多列索引
mysql> alter table example10 add index index17_in(id,name);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
創(chuàng)建空間索引
mysql> alter table example11 add spatial index index18_space(space);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
到此,三種操作方式,每種索引類(lèi)別的建立就都列舉了
對(duì)于索引,重要的是理解索引的概念,明白索引的種類(lèi)
更多的是自己的使用經(jīng)驗(yàn)
最后來(lái)看看索引的刪除
(4)刪除索引
mysql> drop index index18_space on example11;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
這里是剛剛創(chuàng)建的一條索引
其中index18_space是索引名,example11是表名
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com