MYSQL多表查询与事务

多表查询

避免笛卡尔积

 

select * from emp,dept where emp.`dept_id` = dept.`id`;#设置过滤条件(隐式内连接)

select emp.`name`, dept.`name` from emp,dept where emp.`dept_id` = dept.`id`;#查询姓名和部门

select * from emp e inner join dept d on e.`dept_id` = d.`id`;#显式内连接

select * from emp e inner join dept d on e.`dept_id` = d.`id` where e.`name`='唐僧';#添加过滤条件

select e.`id`,e.`name`,e.`gender`,e.`salary`,d.`name` from emp e inner join dept d on e.`dept_id` = d.`id` where e.`name`='唐僧';显式内连接并显示指定列

select e.`id` 编号,e.`name` 姓名,e.`gender` 性别,e.`salary` 工资,d.`name` 部门名字 from emp e inner join dept d on e.`dept_id` = d.`id` where e.`name`='唐僧';#修正表头

select * from dept d left join emp e on d.`id` = e.`dept_id`;#左外连接查询。

 

select * from dept right join emp on dept.`id` = emp.`dept_id`;#右外连接查询

 

嵌套查询

select * from emp where dept_id = (select id from dept where name='市场部');#使用子查询

子查询只有一个值的时候

select * from emp where salary = (select max(salary) from emp);#查询最高薪水对应的姓名

select * from emp where salary < (select avg(salary) from emp);#小于平均工资的员工

子查询有多个值的时候使用in

select name from dept where id in (select dept_id from emp where salary > 5000);#薪水大于5k的部门

select * from emp where dept_id in (select id from dept where name in('开发部','财务部'));#查询部门人员

子查询是多行多列的时候使用FROM后面作为表进行二次查询

select * from dept d, (select * from emp where join_date >='2011-1-1') e where d.`id`= e.dept_id ;#子查询作为表需要取别名,否则这张表没有名称则无法访问表中的字段

或:

select * from emp inner join dept on emp.`dept_id` = dept.`id` where join_date >='2011-1-1';

select * from emp inner join dept on emp.`dept_id` = dept.`id` and join_date >='2011-1-1';

事务

 

手动提交事务

start transaction;

update account set balance = balance - 500 where name='张三';

update account set balance = balance + 500 where name='李四';

commit;#执行完commit数据才真正发生改变

start transaction;

update account set balance = balance - 500 where name='张三';

rollback;#执行过程中使用rollback表示执行错误并回滚到原始状态

自动提交事务

 

查看是否自动提交:

select @@autocommit;# @@表示全局变量,结果为1表示开启,0表示关闭。

set @@autocommit=0;#关闭自动提交

update account set balance=balance+500 where id=2;

commit;#手动提交

 

回滚点

 

在某些成功的操作完成之后,后续的操作有可能成功有可能失败,但是不管成功还是失败,前面操作都已经成功,可以在当前成功的位置设置一个回滚点。可以供后续失败操作返回到该位置,而不是返回所有操作,这个点称之为回滚点。

事务与隔离

 

 

 

set global transaction isolation level read uncommitted;#设置事务隔离级别为读未提交

读未提交时,当A对数据进行操作但未提交时,B读取的信息是未提交信息,可能不一致。

读已提交时,当A对数据进行操作但未提交时,B读取的信息是原始信息,不出现脏读。

读已提交时,当A对数据进行操作已提交时,B读取的信息在A提交前后两次读取不一致,出现不可重复读。

使用serializable隔离级别,一个事务没有执行完,其他事务的SQL执行不了,可以挡住幻读

 

 DCL语句

mysqld是MySQL的主程序,服务器端。mysql是MySQL的命令行工具,客户端。

创建用户user1密码123只能在本机使用:

create user 'user1'@'localhost' identified by '123';

创建用户user1密码123可以在任何计算机使用:

create user 'user2'@'%' identified by '123';

grant create,alter,insert,update,select on test.* to 'user1'@'localhost';#给user1分配到test数据库的部分权限

grant all on *.* to 'user2'@'%';#给user2分配到所有数据库的所有表权限

revoke all on test.* from 'user1'@'localhost';#撤销user1用户对test数据库所有表的操作权限

show grants for 'user1'@'localhost';#查询用户权限,usage是指登录权限

drop user 'user1'@'localhost';#删除用户user1

mysqladmin -uroot -p password 1#修改管理员的用户密码为1(登出状态使用)

set password for 'user2'@'%'=password('1');#修改普通用户user1的密码为1

原文地址:https://www.cnblogs.com/bai2018/p/11469425.html