diff --git "a/18 \351\273\204\350\257\227\351\276\231/20231019\344\272\213\345\212\241.md" "b/18 \351\273\204\350\257\227\351\276\231/20231019\344\272\213\345\212\241.md" new file mode 100644 index 0000000000000000000000000000000000000000..59b1ed2a52cf83d48c7a4c2573ecf478c641f60c --- /dev/null +++ "b/18 \351\273\204\350\257\227\351\276\231/20231019\344\272\213\345\212\241.md" @@ -0,0 +1,128 @@ +# 1.笔记 + +```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; -- 设置一个存档点 +delete from employee where id=2; +savepoint t_2; +delete from employee where id=3; +savepoint t_3; +delete from employee where id=4; +savepoint t_4; +delete from employee where id=5; + +rollback to part1; -- 回滚到一个存档点 + +commit; -- 提交事务 +``` + +```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 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 ; +``` + diff --git "a/18 \351\273\204\350\257\227\351\276\231/20231022 50\351\242\230\347\273\203\344\271\240.sql" "b/18 \351\273\204\350\257\227\351\276\231/20231022 50\351\242\230\347\273\203\344\271\240.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b6abd1a6a37376e0675cc98047dae604f9e00ce2 --- /dev/null +++ "b/18 \351\273\204\350\257\227\351\276\231/20231022 50\351\242\230\347\273\203\344\271\240.sql" @@ -0,0 +1,260 @@ + + +```sql +日志:上午去敬老院参加志愿活动 + + + + + + +CREATE DATABASE tb_a charset utf8; +use tb_a; + +# 学生表 + +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); + + + + +## 题目: + +SELECT * from student; +#1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 + +#2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +select a.*,b.course_id from +(SELECT student.*,score.course_id from student,score where student.student_id=score.student_id) a +right join +(SELECT student.*,score.course_id from student,score where student.student_id=score.student_id) b +on a.student_id=b.student_id where a.course_id=1 and b.course_id=2; +#3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) + +#4. 查询不存在" 1 "课程但存在" 2 "课程的情况 + +#5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 + +#6. 查询在成绩表存在成绩的学生信息 +SELECT * +from student +where student_id +IN (SELECT DISTINCT(student_id)from score) +#7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +SELECT st.student_id,st.student_name,count(*),sum(sc.score) +from student st,score sc +where st.student_id=sc.course_id +GROUP BY sc.course_id; +#8. 查询「李」姓老师的数量 +SELECT count(*) +from teacher +where teacher_name +like '李%' +#9. 查询学过「李白」老师授课的同学的信息 +SELECT * from student st,teacher t,score sc,course co +where st.student_id=sc.student_id +AND sc.course_id=co.course_id +and co.teacher_id=t.teacher_id +and teacher_name='李白'; +#10. 查询没有学全所有课程的同学的信息 +SELECT * from student +left join score on score.student_id=student.student_id +left join course on course.course_id=score.course_id +where score is null; +#11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 + +#12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + +#13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +SELECT * from student st,teacher t,score sc,course co +where st.student_id=sc.student_id +AND sc.course_id=co.course_id +and co.teacher_id=t.teacher_id +and NOT teacher_name='李白'; +#14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +SELECT st.student_id,st.student_name,avg(sc.score) +from student st,score sc +where st.student_id=sc.student_id +AND sc.score <60 +GROUP BY sc.student_id +HAVING count(*)>1; +#15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 + +#16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + +#17. 查询各科成绩最高分、最低分和平均分 +SELECT max(score) '最高分',MIN(score) '最低分',AVG(score) '平均分' from score where course_id=1; +SELECT max(score) '最高分',MIN(score) '最低分',AVG(score) '平均分' from score where course_id=2; +SELECT max(score) '最高分',MIN(score) '最低分',AVG(score) '平均分' from score where course_id=3; + +#18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +#19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + +#20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 + +#21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + +#22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + +#23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +#24. 查询各科成绩前三名的记录 + +#25. 查询每门课程被选修的学生数 + +#26. 查询出只选修两门课程的学生学号和姓名 + +#27. 查询男生、女生人数 +SELECT count(*) '男生'from student where gender ='男'; +SELECT count(*) '女生'from student where gender ='女'; +#28. 查询名字中含有「风」字的学生信息 +SELECT * from student where student_name like '%风%' +#29. 查询同名同性学生名单,并统计同名人数 +SELECT count(*) from student where student_name like '李四'; +#30. 查询 1990 年出生的学生名单 +SELECT * from student where birthday like '1990%'; +#31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + +#32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +SELECT st.student_id,st.student_name,avg(sc.score) +from student st,score sc +where st.student_id=sc.student_id +AND sc.score >85 +GROUP BY sc.student_id +HAVING count(*)>1; +#33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +SELECT * from student st,score sc,course co +where st.student_id=sc.student_id +AND sc.course_id=co.course_id +and co.course_name='数学' +and sc.score<60 +#34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +SELECT st.student_id,st.student_name,sc.score 语文 +from student st +LEFT JOIN score sc +on st.student_id=sc.course_id +SELECT st.student_id,st.student_name,sc.score 数学 +from student st +LEFT JOIN score sc +on st.student_id=sc.course_id +SELECT st.student_id,st.student_name,sc.score 英文 +from student st +LEFT JOIN score sc +on st.student_id=sc.course_id +#35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +SELECT * from student st,score sc,course co +where st.student_id=sc.student_id +AND sc.course_id=co.course_id and score>70 +#36. 查询不及格的课程 +SELECT * from student st,score sc,course co +where st.student_id=sc.student_id +AND sc.course_id=co.course_id and score<60 +#37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +SELECT * +from student st,score sc,course co +where st.student_id=sc.student_id +and co.course_id=1 +AND sc.score>80 +#38. 求每门课程的学生人数 + +#39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +#40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +#41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +#42. 查询每门功成绩最好的前两名 + +#43. 统计每门课程的学生选修人数(超过 5 人的课程才统计) + +#44. 检索至少选修两门课程的学生学号 +SELECT student_id from score GROUP BY student_id HAVING COUNT(*) >=2; +#45. 查询选修了全部课程的学生信息 + + +#46. 查询各学生的年龄,只按年份来算 + +#47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + +#48. 查询本周过生日的学生 + +#49. 查询下周过生日的学生 + +#50. 查询本月过生日的学生 + +#51. 查询下月过生日的学生 +``` +