diff --git "a/07 \351\231\210\346\235\260/20231019 \344\272\213\345\212\241.md" "b/07 \351\231\210\346\235\260/20231019 \344\272\213\345\212\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..27571151e168f530ba10d0c7b05a97f695b08868 --- /dev/null +++ "b/07 \351\231\210\346\235\260/20231019 \344\272\213\345\212\241.md" @@ -0,0 +1,102 @@ +# 笔记 + + + +### 事务 + +是指对数据库执行一批操作,这些操作要么全部成功,要么全部失败,这是**原子性** + +一致性:一个事务必须使数据库从一个一致性状态变换到另一个一致性状态 + +举例:A转账给B,A的钱如果少了而B却没有增加,那么此时的数据处于不一致的状态 + +隔离性:一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。 + +隔离级别:读未提交 读已提交 可重复读 串行化 + +持久性:一个事务一旦提交,他对数据库中的改变是永久的。当事务提交后,数据库会持久化到硬盘,修改是永久性的 + +```MySQL +-- 在mysql中事务是默认开启状态 + +select * from employee; + +show variables like 'autocommit'; -- 查看事务状态 + +set autocommit =0; -- 事务关闭 +set autocommit =off; -- 事务关闭 + +set autocommit =1; -- 事务开启 +set autocommit =on; -- 事务开启 + +select * from employee; + +delete from employee ; + +rollback; -- 回滚 + +start transaction; -- 开始一次临时事务,将在第一次回滚的时候失效 + +delete from employee where id=1; +savepoint t_1; -- 设置一个存档点 + +rollback to part1; -- 回滚到一个存档点 + +commit; -- 提交事务 +``` + + + +```mysql + +-- 完成以下练习题 + +-- 1、列出最低薪金大于1500的各种工作。 + +select distinct job,salary from (select job,min(sal) over(partition by job) salary from emp) li where salary>1500; + +-- 2、列出在部门 "销售部" 工作的员工的姓名,假定不知道销售部的部门编号。 + +select ename from emp where mgr=(select empno from emp,dept where dept.deptno=emp.deptno and dname='销售部' and job='经理'); + +-- 3、列出薪金高于公司平均薪金的所有员工。 + +select * from emp where sal>(select avg(sal) from emp); + +-- 4、列出与"周八"从事相同工作的所有员工。 + +select * from emp where job=(select job from emp where ename='周八'); + +-- 5、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。 + +select * from emp where sal in (select sal from emp where deptno = 30); + +-- 6、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。 + +select * from emp where sal > (select max(sal) from emp where deptno = 30); + +-- 7、列出在每个部门工作的员工数量、平均工资、平均服务年限。 + +select distinct dname,count(*) over(partition by emp.deptno) 员工数量,avg(sal) over(partition by emp.deptno) from emp,dept where emp.deptno=dept.deptno; + +-- 8、列出所有员工的姓名、部门名称和工资。 + +select ename,dname,sal from emp,dept where emp.deptno=dept.deptno; + +-- 9、列出所有部门的详细信息和部门人数。 + +select distinct dept.*,count(ename) over(partition by dname) from emp,dept where emp.deptno=dept.deptno; + +-- 10、列出各种工作的最低工资。 + +select distinct job,min(sal) over(partition by job) from emp; + +-- 11、列出各个部门的 经理 的最低薪金。 + +select distinct ename,dname,job,min(sal) over(partition by emp.deptno) from emp,dept where emp.deptno=dept.deptno and job='经理'; + + +-- 12、列出所有员工的年工资,按年薪从低到高排序。 + +select ename,(sal*12)+(ifnull(comm,0)*12) salary from emp order by salary ; +``` \ No newline at end of file