diff --git "a/49 \346\235\216\350\210\222\346\261\266/10.19\344\272\213\345\212\241.md" "b/49 \346\235\216\350\210\222\346\261\266/10.19\344\272\213\345\212\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..4fc3f0de7b61a36ae08b79f309d23dd0997c18f8 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/10.19\344\272\213\345\212\241.md" @@ -0,0 +1,178 @@ +## 事务 + +### 事务的特性 + +```sql +原子性、一致性、隔离性、持久性 +``` + +### 事务操作 + +```sql +mysql中事务默认是隐式事务,执行insert、update、delete操作的时候,数据库自动开启事务、提交或回滚事务 + +是否开启隐式事务是由变量autocommit控制的 + +所以事务分为**隐式事务**和**显式事务 +``` + +#### 隐式事务 + +```sql +事务自动开启、提交或回滚,比如insert、update、delete语句,事务的开启、提交或回滚由mysql内部自动控制的。 + +查看变量autocommit是否开启了自动提交 +``` + +#### 显式事务 + +事务需要手动开启、提交或回滚,由开发者自己控制。 + +```mysql +-- 显示所有的系统变量 +show variables; + +-- 查看autocommit状态 +show variables like 'autocommit'; + +-- 显式事务(手动提交,关闭系统自动) +set autocommit=off; +set autocommit=0; + +rollback; -- 撤销,可以保留数据 +commit; -- 提交数据 + +-- 隐式事务(自动提交,开启系统自动) +set autocommit=on; +set autocommit=1; + +rollback; -- 撤销,不保留数据,因为系统已经自动commit +``` + +```mysql +-- 开启事务 +start transaction; +-- 结束事务 +rollback / commit; +``` + +### savepoint关键字 + +可以将一大批操作分为几个部分,然后指定回滚某个部分。 + +```mysql +-- 在事务中途使用 +savepoint 保存名; + +-- 追溯回去 +rollback to 保存名; +``` + +### 只读事务 + +表示在事务中执行的是一些只读操作,如查询,但是不会做insert、update、delete操作,数据库内部对只读事务可能会有一些性能上的优化 + +```mysql +start transaction read only; +``` + +#### 脏读 + +读已提交可重复读 + +#### 幻读 + +### 隔离级别 + +隔离级别分为4种: + +```mysql +读未提交:READ-UNCOMMITTED +读已提交:READ-COMMITTED +可重复读:REPEATABLE-READ +串行:SERIALIZABLE +``` + + + +## 作业 + +```mysql +-- 部门表 +create table dept( + deptno int primary key auto_increment, -- 部门编号 + dname varchar(14) , -- 部门名字 + loc varchar(13) -- 地址 +) ; +-- 员工表 +create table emp( + empno int primary key auto_increment,-- 员工编号 + ename varchar(10), -- 员工姓名 - + job varchar(9), -- 岗位 + mgr int, -- 直接领导编号 + hiredate date, -- 雇佣日期,入职日期 + sal int, -- 薪水 + comm int, -- 提成 + deptno int not null, -- 部门编号 + foreign key (deptno) references dept(deptno) +); +insert into dept values(10,'财务部','北京'); +insert into dept values(20,'研发部','上海'); +insert into dept values(30,'销售部','广州'); +insert into dept values(40,'行政部','深圳'); +insert into emp values(7369,'刘一','职员',7902,'1980-12-17',800,null,20); +insert into emp values(7499,'陈二','推销员',7698,'1981-02-20',1600,300,30); +insert into emp values(7521,'张三','推销员',7698,'1981-02-22',1250,500,30); +insert into emp values(7566,'李四','经理',7839,'1981-04-02',2975,null,20); +insert into emp values(7654,'王五','推销员',7698,'1981-09-28',1250,1400,30); +insert into emp values(7698,'赵六','经理',7839,'1981-05-01',2850,null,30); +insert into emp values(7782,'孙七','经理',7839,'1981-06-09',2450,null,10); +insert into emp values(7788,'周八','分析师',7566,'1987-06-13',3000,null,20); +insert into emp values(7839,'吴九','总裁',null,'1981-11-17',5000,null,10); +insert into emp values(7844,'郑十','推销员',7698,'1981-09-08',1500,0,30); +insert into emp values(7876,'郭十一','职员',7788,'1987-06-13',1100,null,20); +insert into emp values(7900,'钱多多','职员',7698,'1981-12-03',950,null,30); +insert into emp values(7902,'大锦鲤','分析师',7566,'1981-12-03',3000,null,20); +insert into emp values(7934,'木有钱','职员',7782,'1983-01-23',1300,null,10); + + + +-- 1、列出最低薪金大于1500的各种工作。 +select job from emp group by job having min(sal)>1500; + +-- 2、列出在部门 "销售部" 工作的员工的姓名,假定不知道销售部的部门编号。 +select ename from emp,dept where dname='销售部'; + +-- 3、列出薪金高于公司平均薪金的所有员工。 +select ename,sal from emp where sal>(select avg(sal) from emp); + +-- 4、列出与"周八"从事相同工作的所有员工。 +select ename,job from emp where job=(select job from emp where ename='周八'); + +-- 5、列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。 +select ename,sal from emp where sal in (select distinct sal from emp where deptno=30); + +-- 6、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。 +select ename,sal from emp where sal > (select max(sal) from emp where deptno=30); + +-- 7、列出在每个部门工作的员工数量、平均工资、平均服务年限。 +select count(empno) 员工数量,avg(sal) 平均工资,avg(datediff(now(),hiredate)) div 365 平均服务年限 from emp group by deptno; + +-- 8、列出所有员工的姓名、部门名称和工资。 +select ename,dname,sal from emp e join dept d on e.deptno=d.deptno; + +-- 9、列出所有部门的详细信息和部门人数。 +select count(empno) 部门人数,d.* from emp e left join dept d on e.deptno=d.deptno group by deptno; + +-- 10、列出各种工作的最低工资。 +select job,min(sal) over (partition by job) 最低工资 from emp; + +select job,min(sal) from emp group by job; + +-- 11、列出各个部门的 经理 的最低薪金。 +select min(sal) 最低薪金 from emp where job='经理' group by deptno; + +-- 12、列出所有员工的年工资,按年薪从低到高排序。 +select ename,sal*12 from emp order by sal*12; + +``` diff --git "a/49 \346\235\216\350\210\222\346\261\266/\345\244\215\344\271\24050\351\242\230.md" "b/49 \346\235\216\350\210\222\346\261\266/\345\244\215\344\271\24050\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..01483ec05424d14f63c9f041f3ccf2764fabbe19 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/\345\244\215\344\271\24050\351\242\230.md" @@ -0,0 +1,464 @@ +```sql +# 学生表 +CREATE TABLE IF NOT EXISTS `student`( + `student_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '学号', + `student_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '姓名', + `birthday` DATETIME NOT NULL COMMENT '出生日期', + `gender` VARCHAR(10) NOT NULL DEFAULT '男' COMMENT '性别', + PRIMARY KEY(`student_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `student` VALUES +(1 , '赵雷' , '1990-01-01' , '男'), +(2 , '钱电' , '1990-12-21' , '男'), +(3 , '孙风' , '1990-12-20' , '男'), +(4 , '李云' , '1990-12-06' , '男'), +(5 , '周梅' , '1991-12-01' , '女'), +(6 , '吴兰' , '1992-01-01' , '女'), +(7 , '郑竹' , '1989-01-01' , '女'), +(8 , '张三' , '2017-12-20' , '女'), +(9 , '李四' , '2017-12-25' , '女'), +(10 , '李四' , '2012-06-06' , '女'), +(11 , '赵六' , '2013-06-13' , '女'), +(12 , '孙七' , '2014-06-01' , '女'); +-- 课程表 +CREATE TABLE IF NOT EXISTS `course`( + `course_id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '课程编号', + `course_name` VARCHAR(10) NOT NULL COMMENT '课程名', + `teacher_id` INT(10) NOT NULL COMMENT '任课教师工号', + PRIMARY KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `course` VALUES +(1, '语文', 2), +(2, '数学', 1), +(3, '英语', 3); +# 教师表 +CREATE TABLE IF NOT EXISTS `teacher`( + `teacher_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '教师工号', + `teacher_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '教师姓名', + PRIMARY KEY(`teacher_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `teacher` VALUES +(1, '高斯'), +(2, '李白'), +(3, 'Trump'); + +-- 成绩表 +CREATE TABLE IF NOT EXISTS `score`( + `student_id` INT(10) NOT NULL COMMENT '学号', + `course_id` INT(4) NOT NULL COMMENT '课程编号', + `score` DECIMAL(18,1) COMMENT '成绩', + KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `score` VALUES +(1 , 1 , 80), +(1 , 2 , 90), +(1 , 3 , 99), +(2 , 1 , 70), +(2 , 2 , 60), +(2 , 3 , 80), +(3 , 1 , 80), +(3 , 2 , 80), +(3 , 3 , 80), +(4 , 1 , 50), +(4 , 2 , 30), +(4 , 3 , 20), +(5 , 1 , 76), +(5 , 2 , 87), +(6 , 1 , 31), +(6 , 3 , 34), +(7 , 2 , 89), +(7 , 3 , 98); + + +``` + +## 题目: + +1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 + + ```mysql + select s.* from + student s, + (select * from score where course_id='1') a, + (select * from score where course_id='2') b + where a.score>b.score + and a.student_id=b.student_id + and b.student_id=s.student_id; + ``` + + ```mysql + select s1.*,s2.course_id,s2.score from student s1 left join score s2 on s1.student_id=s2.student_id where s2.course_id!=3 and s1.student_id in + (select a.student_id from + (select * from score where course_id=1) a, + (select * from score where course_id=2) b + where a.score>b.score and a.student_id=b.student_id); + ``` + +2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 + + ```mysql + select * from student where student_id in + (select a.student_id from + (select * from score where course_id=1) a, + (select * from score where course_id=2) b + where a.student_id=b.student_id); + ``` + +3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) + + ```mysql + select ifnull(s1.student_id,'null'),ifnull(s1.course_id,'null'),ifnull(s1.score,'null'),s2.* from + (select * from score where course_id=1) s1 + right join + (select * from score where course_id=2) s2 + on s1.student_id=s2.student_id; + ``` + +4. 查询不存在" 1 "课程但存在" 2 "课程的情况 + + ```mysql + select * from score where student_id not in (select student_id from score where course_id=1); + + select ifnull(s2.student_id,'null'),ifnull(s2.course_id,'null'),ifnull(s2.score,'null'),s1.* from + (select * from score where course_id=1) s1 + left join + (select * from score where course_id=2) s2 + on s1.student_id=s2.student_id; + ``` + +5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 + + ```mysql + select s1.student_id,s2.student_name,round(avg(score)) from score s1 left join student s2 on s1.student_id=s2.student_id group by student_id having avg(score)>=60; + ``` + +6. 查询在成绩表存在成绩的学生信息 + + ```mysql + select * from student where student_id in (select student_id from score); + ``` + +7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + + ```mysql + select s1.student_id,s1.student_name,count(s2.course_id),ifnull(sum(s2.score),'null') from student s1 left join score s2 on s1.student_id=s2.student_id group by s1.student_id; + ``` + +8. 查询「李」姓老师的数量 + + ```mysql + select count(teacher_id) from teacher where teacher_name like '李%'; + ``` + +9. 查询学过「李白」老师授课的同学的信息 + + ```mysql + select * from student where student_id in (select student_id from score where course_id in (select course_id from course where teacher_id in (select teacher_id from teacher where teacher_name='李白'))); + ``` + +10. 查询没有学全所有课程的同学的信息 + + ```mysql + select s2.*,count(s1.course_id) from score s1 right join student s2 on s1.student_id=s2.student_id group by s2.student_id having count(s1.course_id)<(select count(*) from course); + ``` + +11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 + + ```mysql + select * from student where student_id in (select student_id from score where course_id in (select course_id from score where student_id=1) and student_id!=1); + ``` + +12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + + ```mysql + select student_id,student_name,course_id from student s,(select + select * + from student + where student.student_id not in ( + select * + from + (select student.student_id,a.course_id + from student ,(select course_id from score where student_id='01') as a )as b + left join score on b.student_id=score.student_id and b.course_id=score.course_id + where score.course_id is null ) + and student.student_id !='01'; + ``` + + ```mysql + select * from student where student_id in (select student_id from score group by Student_id having count(course_id)=(select count(course_id) from score where student_id=1)) and student_id!=1; + ``` + + ```mysql + select s2.* from score s1 left join student s2 on s1.student_id=s2.student_id where s1.student_id!=1 group by s1.student_id having count(s1.course_id)=(select count(course_id) from score where student_id=1); + ``` + + ```mysql + select * from student where student_id in(select distinct student_id from score where student_id not in(select student_id from score where course_id not in(select course_id from score where student_id=1)) group by student_id having count(course_id)=(select count(course_id) from score where student_id=1) and student_id!=1); + ``` + +13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 + + ```mysql + select student_name from student where student_id not in (select student_id from score where course_id in (select course_id from course where teacher_id=(select teacher_id from teacher where teacher_name='李白')) group by student_id); + ``` + + ```mysql + select student_name from student + where student_id not in( + select student_id from score where course_id in( + select course_id from course,teacher + where course.teacher_id=teacher.teacher_id and teacher_name='李白')); + ``` + +14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + + ```mysql + select s1.student_id,s2.student_name,avg(s1.score) from score s1 left join student s2 on s1.student_id=s2.student_id where score<60 group by student_id having count(course_id)>=2; + ``` + +15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 + + ```mysql + select * from student where student_id in (select student_id from score where course_id=1 and score<60 order by score desc); + ``` + + ```mysql + select + st.*, + sc.score + from student st + left join score sc + on sc.student_id=st.student_id + where sc.course_id=1 and sc.score<60 + order by sc.score desc ; + ``` + +16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + ```mysql + select a.*,s1.course_id,s1.score from score s1 right join (select student_id,avg(score) from score group by student_id order by avg(score) desc) a on s1.student_id=a.student_id; + ``` + +17. 查询各科成绩最高分、最低分和平均分 + + ```mysql + select course_id,max(score),min(score),avg(score) from score group by course_id; + ``` + +18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + ```mysql + select a.course_id,max(a.score) 最高分 ,min(a.score) 最低分,avg(a.score) 平均分, + ((select count(student_id) from score where score>=60 and course_id=b.course_id )/(select count(student_id) from score where course_id=b.course_id)) 及格率 + from score a + inner join course b on a.course_id = b.course_id + group by b.course_id; + ``` + +19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + + ```mysql + select student_id,course_id,score,rank() over (partition by course_id order by score) from score; + ``` + +20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 + + ```mysql + select student_id,course_id,score,row_number() over (partition by course_id order by score) from score; + ``` + +21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + ```mysql + select a.student_id,rank() over (order by a.score desc) 排名 ,a.score from (select student_id,sum(score) score from score group by student_id) a; + ``` + +22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + ```mysql + select a.student_id,dense_rank() over (order by a.score desc) 排名 ,a.score from (select student_id,sum(score) score from score group by student_id) a; + ``` + +23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + + ```mysql + select c.course_id,c.course_name + ,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=100 and sc.score>80)/(select count(*) from score sc where sc.course_id=c.course_id )) "100-85" + ,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=85 and sc.score>70)/(select count(*) from score sc where sc.course_id=c.course_id )) "85-70" + ,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=70 and sc.score>60)/(select count(*) from score sc where sc.course_id=c.course_id )) "70-60" + ,((select count(*) from score sc where sc.course_id=c.course_id and sc.score<=60 and sc.score>=0)/(select count(*) from score sc where sc.course_id=c.course_id )) "60-0" + from course c order by c.course_id; + ``` + +24. 查询各科成绩前三名的记录 + + ```mysql + select * from + (select student_id,score,rank() over (partition by course_id order by score desc) ranks from score) s + where ranks<4; + ``` + +25. 查询每门课程被选修的学生数 + + ```mysql + select distinct s.course_id,c.course_name,count(s.student_id) over (partition by s.course_id) 学生数 from score s left join course c on s.course_id=c.course_id; + ``` + +26. 查询出只选修两门课程的学生学号和姓名 + + ```mysql + select s.student_name 姓名,a.学号 from (select distinct student_id 学号,count(course_id) over (partition by student_id) 选修数 from score) a left join student s on a.`学号`=s.student_id where 选修数=2; + ``` + +27. 查询男生、女生人数 + + ```mysql + select gender,count(student_id) from student group by gender; + ``` + +28. 查询名字中含有「风」字的学生信息 + + ```mysql + select * from student where student_name like '%风%'; + ``` + +29. 查询同名同性学生名单,并统计同名人数 + + ```mysql + select student_name from student group by student_name having count(student_id)>1; + ``` + +30. 查询 1990 年出生的学生名单 + + ```mysql + select * from student where year(birthday)='1990'; + ``` + +31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + ```mysql + select course_id,avg(score) from score group by course_id order by avg(score) desc,course_id asc; + ``` + +32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + ```mysql + select s.student_id,s.student_name,a.`平均成绩` from student s right join (select student_id,avg(score) 平均成绩 from score group by student_id having avg(score)>=85) a on s.student_id=a.student_id; + ``` + +33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + ```mysql + select student_name,score from student right join score on student.student_id=score.student_id left join course on score.course_id=course.course_id where course_name='数学' and score<60; + ``` + +34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + + ```mysql + select s2.student_name,s1.* from score s1 right join student s2 on s1.student_id=s2.student_id; + ``` + +35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + + ```mysql + select s1.student_name,c.course_name,s2.score from student s1 right join score s2 on s1.student_id=s2.student_id left join course c on s2.course_id=c.course_id where score>70; + ``` + +36. 查询不及格的课程 + + ```mysql + select distinct c.course_name from score s2 left join course c on s2.course_id=c.course_id where score<60; + ``` + +37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 + + ```mysql + select s1.student_id,s2.student_name from score s1,student s2 where course_id=1 and score>80 and s1.student_id=s2.student_id; + ``` + +38. 求每门课程的学生人数 + + ```mysql + select distinct course_id,count(student_id) over (partition by course_id) from score; + ``` + +39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + ```mysql + select distinct s1.* from score s1,score s2 where s1.score=s2.score and s1.course_id!=s2.course_id; + ``` + +42. 查询每门功成绩最好的前两名 + + ```mysql + select * from + (select student_id,score,rank() over (partition by course_id order by score desc) ranks from score) s + where ranks<=2; + ``` + +43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + + ```mysql + select course_id,count(student_id) from score group by course_id having count(student_id)>5; + ``` + +44. 检索至少选修两门课程的学生学号 + + ```mysql + select student_id + from score + group by student_id + having count(course_id)>=2; + ``` + +45. 查询选修了全部课程的学生信息 + + ```mysql + select s2.* from score s1 right join student s2 on s1.student_id=s2.student_id group by student_id having count(course_id)>=(select count(course_id) from course); + ``` + +46. 查询各学生的年龄,只按年份来算 + + ```mysql + select student_name,year(now())-year(birthday) from student; + ``` + +47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + + ```mysql + select birthday,(date_format(now(),'%y') - date_format(birthday,'%y') - + (case when date_format(now(),'%m%d') > date_format(birthday,'%m%d') then 0 else 1 end)) as age + from student; + ``` + +48. 查询本周过生日的学生 + + ```mysql + select * from student where day(birthday) between 16 and 22 and month(birthday)=month(now()); + ``` + +49. 查询下周过生日的学生 + + ```mysql + select * from student where day(birthday) between 23 and 29 and month(birthday)=month(now()); + ``` + +50. 查询本月过生日的学生 + + ```mysql + select * from student where month(birthday)=month(now()); + ``` + +51. 查询下月过生日的学生 + + ```mysql + select * from student where month(birthday)=month(now())+1; + ```