From 5b3b91150a31c108cc3c8d0e946d7cd23921eac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=82=96=E9=92=9F=E5=87=AF=E9=9F=A9=E2=80=9D?= <“3175644391@qq.com”> Date: Tue, 21 Mar 2023 00:06:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\345\255\246\347\224\237\347\211\210.md" | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 "22 \350\202\226\351\222\237\345\207\257\351\237\251/20230319 \345\244\215\344\271\240\344\275\234\344\270\232\345\255\246\347\224\237\347\211\210.md" diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230319 \345\244\215\344\271\240\344\275\234\344\270\232\345\255\246\347\224\237\347\211\210.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230319 \345\244\215\344\271\240\344\275\234\344\270\232\345\255\246\347\224\237\347\211\210.md" new file mode 100644 index 0000000..9407395 --- /dev/null +++ "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230319 \345\244\215\344\271\240\344\275\234\344\270\232\345\255\246\347\224\237\347\211\210.md" @@ -0,0 +1,192 @@ +# 作业 + +````mysql +create database mtyk charset utf8; -- 建数据库 +use mtyk; -- 进入数据库 +create table J_TEACHER( -- 新建表J_TEACHER + Tno int primary key auto_increment comment '教师编号', + Tname varchar(20) unique comment '教师姓名' +); +insert into J_TEACHER values -- 新建数据 +(1,'张老师'), +(2,'王老师'), +(3,'李老师'), +(4,'赵老师'), +(5,'刘老师'), +(6,'向老师'), +(7,'李文静'), +(8,'叶平'); + +create table J_STUDENT( -- 新建表J_STUDENT + Sno int primary key auto_increment comment '学生编号', + Sname varchar(20) unique comment '学生姓名', + Sage datetime comment '学生年龄', + Ssex enum('男','女') comment '性别' +); + +insert into J_STUDENT values -- 新建数据 +(1,'张三','1980-1-23 00:00:00','男'), +(2,'李四','1982-12-12 00:00:00','男'), +(3,'张飒','1981-9-9 00:00:00','男'), +(4,'莉莉','1983-3-23 00:00:00','女'), +(5,'王粥','1982-6-21 00:00:00','男'), +(6,'王丽','1984-10-10 00:00:00','女'), +(7,'刘香','1980-12-22 00:00:00','女'); + +create table J_COURSE( -- 新建表J_COURSE + Cno int primary key auto_increment comment '课程编号', + Cname varchar(20) unique comment '课程名称', + Tno int comment '授课教师编号', + foreign key (Tno) references J_TEACHER(Tno) +); + +insert into J_COURSE values -- 新建数据 +(1,'企业管理',3), +(2,'马克思',1), +(3,'美团盈开',2), +(4,'数据库',5), +(5,'物理',8); + +create table J_SCORE( -- 新建表J_SCORE + Sno int comment '学生编号', + Cno int comment '课程编号', + Score int comment '成绩', + foreign key (Sno) references J_STUDENT(Sno), + foreign key (Cno) references J_COURSE (Cno) +); + +insert into J_SCORE values -- 新建数据 +(1,1,80), +(1,2,86), +(1,3,83), +(1,4,89), +(2,1,50), +(2,2,36), +(2,3,43), +(2,4,59), +(3,1,50), +(3,2,96), +(3,3,73), +(3,4,69), +(4,1,90), +(4,2,36), +(4,3,88), +(4,4,99), +(5,1,90), +(5,2,96), +(5,3,98), +(5,4,99), +(6,1,70), +(6,2,66), +(6,3,58), +(6,4,79), +(7,1,80), +(7,2,76), +(7,3,68), +(7,4,59), +(7,5,89); +``` + +1、 修改刘香的美团盈开课程成绩为100分 +update J_SCORE set score=100 where sno=(select sno from J_STUDENT where sname='刘香') and cno=(select cno from J_COURSE where cname='美团盈开'); + +2、 将学生表中的sage段放在ssex段后面 +alter table J_STUDENT modify Sage datetime after Ssex; + +3、 教师表添加一个用于统计状态的列 名字为now 默认值为有课 +alter table J_TEACHER add now varchar(20) default '有课'; + +4、 将没有上课的老师的now列值设置为无课 +update J_TEACHER set now='无课' where tno not in (select tno from J_COURSE); + +5、 修改课程表中的企业管理为比遛旺 +update J_COURSE set cname='比遛旺' where cname='企业管理'; + +6、 查询课程1的成绩比课程2的成绩 高 的所有学生的学号。 + +select s.sno from J_SCORE s join J_SCORE c on s.sno=c.sno where s.cno=1 and c.cno=2 and s.score>c.score; + +select a1.sno from (select * from J_SCORE where cno=1) a1 left join (select * from J_SCORE where cno=2) a2 on a1.sno=a2.sno where a1.score>a2.score; + +7、 查询平均成绩大于60分的同学的学号和平均成绩。 + +select sno,avg(score) from J_SCORE group by sno having avg(score)>60; + +8、 查询所有同学的学号、姓名、选课数、总成绩。 +select jst.sno,sname,count(cno),sum(score) from J_STUDENT jst left join J_SCORE jsc on jst.sno=jsc.sno group by jsc.sno; + +9、 查询姓“李”的学生的个数。 +select count(sname) from J_STUDENT where sname like '李%'; + +10、 查询没学过“叶平”老师课的同学的学号、姓名。 +select j_score.sno,sname from j_student left join j_score on j_student.sno=j_score.sno where j_score.sno in (select sno from j_score where sno!=(select sno from j_score where cno = (select cno from j_course where tno=(select tno from j_teacher where tname='叶平'))) GROUP BY sno) GROUP BY sno; + +11、 查询同时学过课程1和课程2的同学的学号、姓名。 +select jst.sno,jst.sname from J_STUDENT jst left join (select * from J_SCORE where cno=1) a1 on jst.sno=a1.sno left join (select * from J_SCORE where cno=2) a2 on a1.sno=a2.sno; + +SELECT a.sno,a.sname from j_student a where a.sno in ( select b.sno from j_score b JOIN j_score c on b.sno=c.sno where b.cno=1 and c.cno=2); + +12、 查询学过“叶平”老师所教所有课程的所有同学的学号、姓名。 +select j_score.sno,sname from j_student left join j_score on j_student.sno=j_score.sno where j_score.sno in (select sno from j_score where sno=(select sno from j_score where cno = (select cno from j_course where tno=(select tno from j_teacher where tname='叶平'))) GROUP BY sno) GROUP BY sno; + +13、 查询 课程编号1的成绩 比 课程编号2的成绩 高的所有同学的学号、姓名。 +select jst.sno,jst.sname from J_STUDENT jst right join J_SCORE a on jst.sno=a.sno join J_SCORE b on a.sno=b.sno where a.cno=1 and b.cno=2 and a.score>b.score; + +14、 查询所有课程成绩小于60分的同学的学号、姓名。 +select a.sno,a.sname from j_student as a where a.sno in ( select b.sno from j_score as b group by b.sno having max(b.score)<60); + +15、 查询所有课程成绩大于60分的同学的学号、姓名。 +select a.sno,a.sname from j_student as a where a.sno in ( select b.sno from j_score as b group by b.sno having max(b.score)>60); + + +16、 查询没有学全所有课的同学的学号、姓名 +select J_SCORE.sno,sname from J_SCORE left join J_STUDENT on J_STUDENT.sno = J_SCORE.sno group by J_SCORE.sno having not count(J_SCORE.sno)=(select count(cno) from J_COURSE); + + +17、 查询至少有一门课程 与 学号为1的同学所学课程 相同的同学的学号和姓名 +select sno,sname from J_STUDENT where sno in (select sno from J_SCORE where cno in (select cno from J_SCORE where sno=1) and sno!=1); + +18、 查询和2号同学学习的课程完全相同的其他同学学号和姓名。 +select sno,sname from J_STUDENT where sno in (select sno from J_SCORE where cno in (select cno from J_SCORE where sno=2) and sno!=2); + +19、 查询各科成绩最高分和最低分。 +select cno,max(score),min(score) from J_SCORE group by cno; + +20、 查询每门课程被选修的学生数。 +select cno,count(sno) from J_SCORE group by cno; + + +21、 查询出只选修了一门课程的全部学生的学号和姓名。 +select sno,sname from J_STUDENT where sno in (select sno from J_SCORE group by sno having count(cno)=1); + +22、 查询同名同性学生名单,并统计同名人数。 + +select sname,ssex,count(sname) from (select a.sname,a.ssex from J_STUDENT a join J_STUDENT b on a.sname=b.sname where a.sname=b.sname and a.sno!=b.sno and a.ssex=b.ssex) c group by sname,ssex; + +23、 查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩。 + +select max(score),sname from J_STUDENT jst left join J_SCORE jsc on jst.sno=jsc.sno right join J_COURSE jco on jsc.cno=jco.cno left join J_TEACHER jte on jco.tno=jte.tno where tname='叶平'; + +24、 查询不同课程成绩相同的学生的学号、课程号、学生成绩。 + +select a.sno,a.cno,a.score from J_SCORE a join J_SCORE b on a.sno=b.sno where a.cno!=b.cno and a.score=b.score; + +25、 查询每门课程成绩最好的前两名的学生ID +select a.sno,a.cno,a.score from j_score a where (select count(*) from j_score b where a.cno=b.cno and a.score<=b.score)<=2; + +26、 检索至少选修了5门课程的学生学号。 +select sno from J_SCORE group by sno having count(sno)>=5; + +27、 查询没学过“叶平”老师讲授的任一门课程的学生姓名。 +select jst.sno,jst.sname from J_STUDENT jst left join J_SCORE jsc on jst.sno=jsc.sno left join J_COURSE jco on jsc.cno=jco.cno left join J_TEACHER jte on jco.tno=jte.tno where tname !='叶平' group by jst.sname; + +28、 查询两门以上不及格课程的同学的学号及其平均成绩。 +select sno,avg(score) from J_SCORE where score<60 group by sno having count(sno)>2; + +29、 查询最受欢迎的老师(选修学生最多的老师)。 +select tname from J_SCORE a,J_COURSE,J_TEACHER where a.cno=J_COURSE.cno and J_TEACHER.tno=J_COURSE.tno group by a.cno having count(a.cno)=(select max(c.b) from (select count(cno) b from J_SCORE group by cno) c); + +30、 修改课程表名为给乱给要酒要 +alter table J_COURSE rename to 乱给要酒要; + +```` \ No newline at end of file -- Gitee