#TCL/*
Transaction Control Language 事务控制语言
事务:
一个或一组sql语句组成一个执行单元,这个执行单元要么全部执行,要么全部不执行。
案例:转账
张三丰 1000
郭襄 1000
update 表 set 张三丰的余额=500 where name='张三丰'
意外
update 表 set 郭襄的余额=1500 where name='郭襄'
事务的特性:
ACID
原子性:一个事务不可再分割,要么都执行要么都不执行
一致性:一个事务执行会使数据从一个一致状态切换到另外一个一致状态
隔离性:一个事务的执行不受其他事务的干扰
持久性:一个事务一旦提交,则会永久的改变数据库的数据.
事务的创建
隐式事务:事务没有明显的开启和结束的标记
比如insert、update、delete语句
delete from 表 where id =1;
显式事务:事务具有明显的开启和结束的标记
前提:必须先设置自动提交功能为禁用
set autocommit=0;
步骤1:开启事务
set autocommit=0;
start transaction;可选的
步骤2:编写事务中的sql语句(select insert update delete)
语句1;
语句2;
...
步骤3:结束事务
commit;提交事务
rollback;回滚事务
savepoint 节点名;设置保存点
事务的隔离级别:
脏读 不可重复读 幻读
read uncommitted:√ √ √
read committed: × √ √
repeatable read: × × √
serializable × × ×
mysql中默认 第三个隔离级别 repeatable read
oracle中默认第二个隔离级别 read committed
查看隔离级别
select @@tx_isolation;
设置隔离级别
set session|global transaction isolation level 隔离级别;
开启事务的语句;
update 表 set 张三丰的余额=500 where name='张三丰'
update 表 set 郭襄的余额=1500 where name='郭襄'
结束事务的语句;
*/SHOW VARIABLES LIKE'autocommit';SHOW ENGINES;#1.演示事务的使用步骤#开启事务SET autocommit=0;STARTTRANSACTION;#编写一组事务的语句UPDATE account SET balance =1000WHERE username='张无忌';UPDATE account SET balance =1000WHERE username='赵敏';#结束事务ROLLBACK;#commit;SELECT*FROM account;#2.演示事务对于delete和truncate的处理的区别SET autocommit=0;STARTTRANSACTION;DELETEFROM account;ROLLBACK;#3.演示savepoint 的使用SET autocommit=0;STARTTRANSACTION;DELETEFROM account WHERE id=25;SAVEPOINT a;#设置保存点DELETEFROM account WHERE id=28;ROLLBACKTO a;#回滚到保存点SELECT*FROM account;
视图
#视图/*
含义:虚拟表,和普通表一样使用
mysql5.1版本出现的新特性,是通过表动态生成的数据
比如:舞蹈班和普通班级的对比
创建语法的关键字 是否实际占用物理空间 使用
视图 create view 只是保存了sql逻辑 增删改查,只是一般不能增删改
表 create table 保存了数据 增删改查
*/#案例:查询姓张的学生名和专业名SELECT stuname,majorname
FROM stuinfo s
INNERJOIN major m ON s.`majorid`= m.`id`WHERE s.`stuname`LIKE'张%';CREATEVIEW v1
ASSELECT stuname,majorname
FROM stuinfo s
INNERJOIN major m ON s.`majorid`= m.`id`;SELECT*FROM v1 WHERE stuname LIKE'张%';#一、创建视图/*
语法:
create view 视图名
as
查询语句;
*/USE myemployees;#1.查询姓名中包含a字符的员工名、部门名和工种信息#①创建CREATEVIEW myv1
ASSELECT last_name,department_name,job_title
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON j.job_id = e.job_id;#②使用SELECT*FROM myv1 WHERE last_name LIKE'%a%';#2.查询各部门的平均工资级别#①创建视图查看每个部门的平均工资CREATEVIEW myv2
ASSELECTAVG(salary) ag,department_id
FROM employees
GROUPBY department_id;#②使用SELECT myv2.`ag`,g.grade_level
FROM myv2
JOIN job_grades g
ON myv2.`ag`BETWEEN g.`lowest_sal`AND g.`highest_sal`;#3.查询平均工资最低的部门信息SELECT*FROM myv2 ORDERBY ag LIMIT1;#4.查询平均工资最低的部门名和工资CREATEVIEW myv3
ASSELECT*FROM myv2 ORDERBY ag LIMIT1;SELECT d.*,m.ag
FROM myv3 m
JOIN departments d
ON m.`department_id`=d.`department_id`;#二、视图的修改#方式一:/*
create or replace view 视图名
as
查询语句;
*/SELECT*FROM myv3
CREATEORREPLACEVIEW myv3
ASSELECTAVG(salary),job_id
FROM employees
GROUPBY job_id;#方式二:/*
语法:
alter view 视图名
as
查询语句;
*/ALTERVIEW myv3
ASSELECT*FROM employees;#三、删除视图/*
语法:drop view 视图名,视图名,...;
*/DROPVIEW emp_v1,emp_v2,myv3;#四、查看视图DESC myv3;SHOWCREATEVIEW myv3;#五、视图的更新CREATEORREPLACEVIEW myv1
ASSELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0))"annual salary"FROM employees;CREATEORREPLACEVIEW myv1
ASSELECT last_name,email
FROM employees;SELECT*FROM myv1;SELECT*FROM employees;#1.插入INSERTINTO myv1 VALUES('张飞','zf@qq.com');#2.修改UPDATE myv1 SET last_name ='张无忌'WHERE last_name='张飞';#3.删除DELETEFROM myv1 WHERE last_name ='张无忌';#具备以下特点的视图不允许更新#①包含以下关键字的sql语句:分组函数、distinct、group by、having、union或者union allCREATEORREPLACEVIEW myv1
ASSELECTMAX(salary) m,department_id
FROM employees
GROUPBY department_id;SELECT*FROM myv1;#更新UPDATE myv1 SET m=9000WHERE department_id=10;#②常量视图CREATEORREPLACEVIEW myv2
ASSELECT'john' NAME;SELECT*FROM myv2;#更新UPDATE myv2 SET NAME='lucy';#③Select中包含子查询CREATEORREPLACEVIEW myv3
ASSELECT department_id,(SELECTMAX(salary)FROM employees) 最高工资
FROM departments;#更新SELECT*FROM myv3;UPDATE myv3 SET 最高工资=100000;#④joinCREATEORREPLACEVIEW myv4
ASSELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;#更新SELECT*FROM myv4;UPDATE myv4 SET last_name ='张飞'WHERE last_name='Whalen';INSERTINTO myv4 VALUES('陈真','xxxx');#⑤from一个不能更新的视图CREATEORREPLACEVIEW myv5
ASSELECT*FROM myv3;#更新SELECT*FROM myv5;UPDATE myv5 SET 最高工资=10000WHERE department_id=60;#⑥where子句的子查询引用了from子句中的表CREATEORREPLACEVIEW myv6
ASSELECT last_name,email,salary
FROM employees
WHERE employee_id IN(SELECT manager_id
FROM employees
WHERE manager_id ISNOTNULL);#更新SELECT*FROM myv6;UPDATE myv6 SET salary=10000WHERE last_name ='k_ing';
#一、创建视图emp_v1,要求查询电话号码以‘011’开头的员工姓名和工资、邮箱CREATEORREPLACEVIEW emp_v1
ASSELECT last_name,salary,email
FROM employees
WHERE phone_number LIKE'011%';#二、创建视图emp_v2,要求查询部门的最高工资高于12000的部门信息CREATEORREPLACEVIEW emp_v2
ASSELECTMAX(salary) mx_dep,department_id
FROM employees
GROUPBY department_id
HAVINGMAX(salary)>12000;SELECT d.*,m.mx_dep
FROM departments d
JOIN emp_v2 m
ON m.department_id = d.`department_id`;