索引
20 分钟阅读
•
1468 字
+
2446 词
四、索引(==重要==)
1、概念
2、B+树的特征
3、索引的类型
4、索引的创建与删除
4.1、创建主键索引
mysql
#查看索引
show index from member;
#在创建表的时候,创建出主键
create table test1 (id int, age int, math float, primary key(id));
#在表创建出来之后,再去创建主键
alter table test2 add primary key(id);
4.2、创建唯一索引
mysql
#在创建表的时候,创建出唯一索引
create table test3 (id int, age int, math float, unique math_idx(math));
#在表创建出来之后,再去创建唯一索引
alter table test4 add unique index math_idx(math);
create unique index age_idx on test4(age);
4.3、创建普通索引
mysql
#在创建表的时候,创建出普通索引
create table test3 (id int, age int, math float, math_idx(math));
#在表创建出来之后,再去创建普通索引
alter table test4 add index math_idx(math);
create index age_idx on test4(age);
4.4、创建组合索引
mysql
#在创建表的时候,创建出组合索引
create table test3 (id int, age int, math float, math_age_idx(math,age));
#在表创建出来之后,再去创建组合索引
alter table test4 add index math_age_idx(math,age);
create index age_math_idx on test4(age,math);
4.5、索引的删除
mysql
alter table test4 drop index math_idx;
drop index age_idx on test4;
5、最左前缀(==重点==)
6、索引的优缺点
1、InnoDB使用主键创建索引
2、InnoDB使用非主键创建索引
mysql
//id = primary key name = index
select age from tableName where id = 18;
select age from tableName where name = 'Alice';
select id,name from tableName where name = 'Alice';
#没有索引只能全盘扫描
select id,name from tableName where age = 77;
3、MyISAM使用主键创建索引
4、MyISAM使用非主键创建索引