索引
一、索引的简介
1.索引的概念
是数据库对象,实现数据库快速查询。
2.为什么使用索引
实现数据库快速查询,提高查询速度。
3.索引的分类
a.普通索引
最基本的索引,对字段数据的类型和值没有任何限制,数据类型可以任意,字段的值可以空也可以重复。
b.主键索引
给主键字段添加的索引
主键特点:非空且唯一
c.唯一索引
给唯一字段添加的索引
唯一索引和主键索引的区别:
- 唯一索引:只有唯一,可以有空值
- 主键索引:非空且唯一
d.全文索引
适用于在一大串文本添加的索引,只可以给字符串数据类型添加。
字符串数据类型(char varchar text)
e.空间索引
给字段的数据类型只能是空间数据类型且该字段的值必须是非空 not null。
空间数据类型:geometry point linestring polygon
f.复合索引
给多个字段添加的索引。
注意: 如果添加了复合索引,查询条件中只有使用了第一个字段,该索引才会被触发。
例如:(id,name) 只有查询条件中使用了 id 字段,索引才会被使用
如果查询条件中只有 name 字段,则索引不会被触发。
二、创建索引
(1)自动创建索引
如果在创建表时,给表添加了主键约束和唯一约束,MySQL 数据库会自动为主键约束和唯一约束创建对应的主键索引和唯一索引。
例如:创建表 index_student,并为表添加主键和唯一约束
-- 创建表 index_student,并为表添加主键和唯一约束
create table index_student(
sno int(8) primary key auto_increment,
sname varchar(20) unique,
age int(2)
);
查询表中的索引
语法:show index from 表名;
例如:查询表 index_student 中的索引
-- 查询表 index_student 中的索引
show index from index_student;
运行结果:
(2)手动创建索引
a.创建表时创建索引
1.创建普通索引
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
index|key [索引名] [索引类型] (字段名[(长度)][asc | desc])
);
例如:创建表 index_student2 给 sno 添加索引
-- 创建表 index_student2,给 sno 添加索引
create table index_student2(
sno int(8),
sname varchar(20),
age int(2),
index(sno)
);
-- 查询表 index_student2 中的索引
show index from index_student2;
运行结果:
2.唯一索引的创建
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
unique [index|key] [索引名] [索引类型] (字段名[(长度)][asc | desc])
);
例如:创建表 index_student3 给 sname 添加唯一索引
-- 创建表 index_student3,给 sname 添加索引
create table index_student3(
sno int(8),
sname varchar(20),
age int(2),
unique index(sname)
);
-- 查询表 index_student3 中的索引
show index from index_student3;
运行结果:
3.主键索引的创建
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
primary key [index|key] [索引名] [索引类型] (字段名[(长度)][asc | desc])
);
例如:创建表 index_student4 给 sno 添加主键索引
-- 创建表 index_student4,给 sno 添加索引
create table index_student4(
sno int(8),
sname varchar(20),
age int(2),
primary key(sno)
);
-- 查询表 index_student4 中的索引
show index from index_student4;
运行结果:
4.全文索引的创建
注意: 只能给字符串数据类型添加
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
fulltext [index|key] [索引名] [索引类型] (字段名[(长度)][asc | desc])
);
例如:创建表 index_student5,给 sinfo 添加全文索引
-- 创建表 index_student5,给 sno 添加索引
create table index_student5(
sno int(8),
sname varchar(20),
sinfo varchar(200),
fulltext index(sinfo)
);
-- 查询表 index_student5 中的索引
show index from index_student5;
运行结果:
5.空间索引的创建
注意: 只能给空间数据类型添加,且该字段的值不能为空 not null
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
spatial [index|key] [索引名] [索引类型] (字段名[(长度)][asc | desc])
);
例如:创建表 index_student6,给 sloc 字段的类型是 point 添加空间索引
-- 创建表 index_student6,给 sloc 添加索引
create table index_student6(
sno int(8),
sname varchar(20),
age int(2),
sloc point not null,
spatial index(sloc)
);
-- 查询表 index_student6 中的索引
show index from index_student6;
运行结果:
6.复合索引的创建
语法:
create table 表名(
字段名1 字段类型1,
字段名2 字段类型2,
.....,
index|key [索引名] [索引类型] (字段名1[(长度)][asc | desc],字段名2[(长度)][asc | desc]...)
);
例如:创建表 index_student7,给 sno 和 sname 添加复合索引
-- 创建表 index_student7,给 sno 和 sname 添加索引
create table index_student7(
sno int(8),
sname varchar(20),
age int(2),
index(sno, sname)
);
-- 查询表 index_student7 中的索引
show index from index_student7;
运行结果:
索引名称与第一个索引字段名称相同
b.创建表后使用"create index" 创建索引
语法:
create [unique|fulltext|spatial] index 索引名称 [索引的类型]
on 表名(字段名1[(长度)][asc|desc],字段名2[(长度)][asc|desc]....);
注意: 使用 create index 这种创建索引的方式不能创建主键索引
1.创建普通索引
例如:创建表 index_student8,给表中 sno 添加普通索引
-- 创建表 index_student8
create table index_student8(
sno int(8),
sname varchar(20),
age int(2)
);
-- 给 sno 添加普通索引
create index index_student8_sno on index_student8(sno);
show index from index_student8;
运行结果:
2.创建唯一索引
例如:给表 index_student8 中 sname 添加唯一索引
-- 给表 index_student8 中 sname 添加唯一索引
create unique index index_student8_sname on index_student8(sname);
show index from index_student8;
运行结果:
3.创建全文索引 fulltext
例如:创建表 index_student9,其中给 sinfo 添加全文索引
-- 创建表 index_student9
create table index_student9(
sno int(8),
sname varchar(20),
sinfo varchar(200)
);
-- 给 sinfo 添加全文索引
create fulltext index index_student9_sinfo on index_student9(sinfo);
show index from index_student9;
运行结果:
4.创建空间索引 spatial
例如:创建表 index_student10,其中给 sloc 添加空间索引
-- 创建表 index_student10
create table index_student10(
sno int(8),
sname varchar(20),
age int(2),
sloc point not null
);
-- 给 sloc 添加空间索引
create spatial index index_student10_sloc on index_student10(sloc);
show index from index_student10;
运行结果:
5.创建复合索引
例如:创建表 index_student11,其中给 sno 和 sname 添加复合索引
-- 创建表 index_student11
create table index_student11(
sno int(8),
sname varchar(20),
age int(2)
);
-- 给 sno 和 sname 添加复合索引
create index index_student11_sno_sname on index_student11(sno, sname);
show index from index_student11;
运行结果:
c.给已有表添加索引 "alter table"
1.创建普通索引
语法: alter table 表名 add index|key [索引名] [索引类型] (字段名[长度][asc|desc]);
例如:创建表 index_student12,其中给 sno 添加普通索引
-- 创建表 index_student12,其中给 sno 添加普通索引
create table index_student12(
sno int(8),
sname varchar(20),
age int(2)
);
alter table index_student12 add index(sno);
show index from index_student12;
运行结果:
2.创建唯一索引
语法: alter table 表名 add unique [index|key] [索引名] [索引类型] (字段名[长度][asc|desc]);
例如:创建表 index_student13,其中给 sname 添加唯一索引
-- 创建表 index_student13,其中给 sname 添加唯一索引
create table index_student13(
sno int(8),
sname varchar(20),
age int(2)
);
alter table index_student13 add unique index(sname);
show index from index_student13;
运行结果:
3.创建主键索引
语法:alter table 表名 add primary key [index|key] [索引名] [索引类型] (字段名[长度][asc|desc]);
例如:创建表 index_student14,其中给 sno 添加主键索引
-- 创建表 index_student14,其中给 sno 添加主键索引
create table index_student14(
sno int(8),
sname varchar(20),
age int(2)
);
alter table index_student14 add primary key(sno);
show index from index_student14;
运行结果:
4.创建全文索引
语法: alter table 表名 add fulltext [index|key] [索引名] [索引类型] (字段名[长度][asc|desc]);
例如:创建表 index_student15,其中给 sinfo 添加全文索引
-- 创建表 index_student15,其中给 sinfo 添加全文索引
create table index_student15(
sno int(8),
sname varchar(20),
sinfo varchar(200)
);
alter table index_student15 add fulltext(sinfo);
show index from index_student15;
运行结果:
5.创建空间索引
语法:alter table 表名 add spatial [index|key] [索引名] [索引类型] (字段名[长度][asc|desc]);
例如:创建表 index_student16,其中给 sloc 添加空间索引
-- 创建表 index_student16,其中给 sloc 添加空间索引
create table index_student16(
sno int(8),
sname varchar(20),
sloc point not null
);
alter table index_student16 add spatial(sloc);
show index from index_student16;
运行结果:
6.创建复合索引
语法:alter table 表名 add index|key [索引名] [索引类型] (字段名1[长度][asc|desc],字段名2[长度][asc|desc]...);
例如:创建表 index_student17,其中给 sno 和 sname 添加复合索引
-- 创建表 index_student17,其中给 sno, sname 添加复合索引
create table index_student17(
sno int(8),
sname varchar(20),
age int(2)
);
alter table index_student17 add index(sno, sname);
show index from index_student17;
运行结果:
三、删除索引
1.使用 alter table 删除
语法:alter table 表名 drop index|key 索引名称;
例如:删除表 index_student17 中的索引
-- 删除表 index_student17 中的索引
alter table index_student17 drop index sno;
show index from index_student17;
运行结果:
2.使用 drop index 删除
语法:drop index 索引名称 on 表名;
例如:删除表 index_student16 中的索引
-- 删除表 index_student16 中的索引
drop index sloc on index_student16;
show index from index_student16;
运行结果:
注意:使用 alter table 方式删除索引不能删除主键索引,删除主键索引可以使用 drop index 进行删除:
例如:删除表 index_student14 中的主键索引
-- 首先查看表 index_student14 的主键索引
show index from index_student14;
-- 删除表 index_student14 的主键索引
alter table index_student14 drop primary key;
show index from index_student14;
运行结果:
四、使用图形化界面操作索引
打开 MySQL 图形化管理工具 Navicat,选择表名右键 --> 点击设计表 --> 选择索引项即可添加和删除索引: