mysql之索引 应用于事物 内连接、左(外)连接、右(外)连接

什么是索引 

索引就像是一本书的目录一样,能够快速找到所需要的内容

索引的作用

加快查询速率,降低IO成本加快表与表之间的连接,减少分组和排序时间

索引类型

普通索引:没有唯一性的基本索引

唯一索引:有唯一性,但可以为空

主键索引:有唯一性且不能够为空

全文索引:

单列索引与多列索引

创建索引的依据

表的主键,外键必须有索引

数量超过300行的必须有索引

经常与其他表连接大表,在连接字段应该有索引

唯一性差,频繁更新的不适合建索引

将常出现在where字段的应该建索引

索引应该建在选择性高的小字段上

创建查看删除索引

1.普通

create index  索引名字 on 表名(把谁设为索引);
create index  sy on wk(年龄);

2.查看索引

show index from 库名.表名G;  查看并以竖列形式显示出来
show keys from 库名.表名G;

3.唯一索引

create unique index 索引名字 on 表名(把谁设为唯一索引);

 create unique index wysy on wk(姓名);

 4.主键索引

1.在create建表时创建 

2.若忘记在建表时创建

alter table 表名 add primary key(把谁建为主键索引);

5.删除索引

非主键

drop index 索引名字 on 表名;
alter tables 表名 drop index 索引名字;

主键索引

alter table 表名 drop primary key;

事物处理控制命令

begin   开始一个事务

commit     提交一个事务

rollback  回滚一个事物


set 命令进行控制

set autocommit=0;        #禁止自动提交
set autocommit=0;        #开启自动提交

MySQL 内连接、左(外)连接、右(外)连接

内连接
就是找两个表之间的交集

关键字:inner join on

select * from 表名1 自定义的表1的别名 inner join 表名2 自定义的表2的别名 on 表一的别名.a_id = 表2的别名.b_id;
select * from a_table a inner join b_table b on a.a_id = b.b_id;
查看表内所有 表a_table 定义别名a 两个表之间的交集 表b_table 定义别名b 通过格式上的b_id作对比找出b_id的并集

左(外)连接

以左边表为基准,找右边表里和左边一样的列出来

关键字:left join on 或 left outer join on  #两种写法都行,没区别,前者是后者的简写

 select * from a_table a left join b_table b on a.a_id = b.b_id;   

右(外)连接

跟左连接差不多,以右边表为基准,右边的都列出来,找左边表里和右边一样的列出来

关键字:right join on 或 right outer join on 

 select * from a_table a right join b_table b on a.a_id = b.b_id;

原文地址:https://www.cnblogs.com/ywrj/p/9412931.html