数据库学习4
来源:互联网 发布:ubuntu 启动器 图标 编辑:程序博客网 时间:2024/06/07 23:39
刘江 11511010019
叶公正 11511010024
创建索引
1. 创建表时创建索引
基本形式:
create table 表名(
属性名 数据类型 [完整性约束条件],
属性名 数据类型 [完整性约束条件]
...
[unique|fulltext|spatial] index|key [别名](属性名1 [(长度)] [asc|desc])
)
示例:
create table index1(id int,name varchar(5),index(id));
2. 在已经存在表中创建索引
基本形式:
create [unique|fulltext|spatial] index 索引名 on 表名 (属性名 [(长度)] [asc|desc]);
单字段索引
create index c2_idx on student(sno);
show index from su;
explain select * from su a,(select c2 from su where id=10) b where a.c2=b.c2;
多字段索引(联合索引) 最左前缀原则
create index idx_c3_c4 on su(c3,c4);
show index from su;
explain select * from su where c3=21;
explain select * from su where c4=21;
explain select * from su where c3=21 order by c4;
explain select * from su where c4=21 order by c3;
只能使用一个索引
explain select * from su where c3=21 or c4=21;
总结,尽量少用联合索引。
create index c3_index on su(c3);
create index c4_index on su(c4);
explain select * from su where c3=21 or c4=21;
explain select * from su where c3=21 union all select * from su where c4=21;
** union all既不排序、也不去重;union排序、去重 **
前缀索引
create index name_idx on table_name(name(6));
3. 用alter table语句创建索引
基本形式:
alter table 表名 add [unique|fulltext|spatial] index 索引名 (属性名 [(长度)] [asc|desc]);
示例:
alter table tb add index index_name(id);
删除索引
alter table 表名 drop index 索引名;
drop index 索引名 on 表名;
alter table su drop index idex_c3_
1. 创建表时创建索引
基本形式:
create table 表名(
属性名 数据类型 [完整性约束条件],
属性名 数据类型 [完整性约束条件]
...
[unique|fulltext|spatial] index|key [别名](属性名1 [(长度)] [asc|desc])
)
示例:
create table index1(id int,name varchar(5),index(id));
2. 在已经存在表中创建索引
基本形式:
create [unique|fulltext|spatial] index 索引名 on 表名 (属性名 [(长度)] [asc|desc]);
单字段索引
create index c2_idx on student(sno);
show index from su;
explain select * from su a,(select c2 from su where id=10) b where a.c2=b.c2;
多字段索引(联合索引) 最左前缀原则
create index idx_c3_c4 on su(c3,c4);
show index from su;
explain select * from su where c3=21;
explain select * from su where c4=21;
explain select * from su where c3=21 order by c4;
explain select * from su where c4=21 order by c3;
只能使用一个索引
explain select * from su where c3=21 or c4=21;
总结,尽量少用联合索引。
create index c3_index on su(c3);
create index c4_index on su(c4);
explain select * from su where c3=21 or c4=21;
explain select * from su where c3=21 union all select * from su where c4=21;
** union all既不排序、也不去重;union排序、去重 **
前缀索引
create index name_idx on table_name(name(6));
3. 用alter table语句创建索引
基本形式:
alter table 表名 add [unique|fulltext|spatial] index 索引名 (属性名 [(长度)] [asc|desc]);
示例:
alter table tb add index index_name(id);
删除索引
alter table 表名 drop index 索引名;
drop index 索引名 on 表名;
alter table su drop index idex_c3_
阅读全文
0 0
- JAVA学习数据库4
- 数据库学习---4
- 数据库学习4
- 数据库学习4 计算字段
- Django学习4:操作数据库
- 学习数据库
- 学习数据库
- 数据库学习
- 数据库学习
- 数据库学习
- 学习数据库
- 数据库学习
- 数据库学习
- 数据库学习
- 数据库学习
- 数据库学习
- 数据库学习
- 数据库学习
- [模板]ST表
- 【云星数据---mesos实战系列002】:mesos全分布式部署实战002--Java准备
- CSDN应该改成网购网站
- 在js中仿java的枚举类型设计实例
- 实验三:内部模块化的命令行菜单小程序V2.0
- 数据库学习4
- Android列表下拉刷新操作
- pytorch setup
- PHP和Redis实现在高并发下的抢购及秒杀功能
- MAC泛洪攻击
- 实验四:用可重用的链表模块来实现命令行菜单小程序V2.5
- 【Java 并发】易混淆的KeyWords
- Android学习之Socket多个客户端即时通信聊天
- 实验五:用callback增强链表模块来实现命令行菜单小程序V2.8