diff --git "a/47\346\250\212\345\260\217\351\203\255/20230319 SQL\347\232\204\345\255\246\347\224\237\350\257\225\351\242\230\344\275\234\344\270\232.md" "b/47\346\250\212\345\260\217\351\203\255/20230319 SQL\347\232\204\345\255\246\347\224\237\350\257\225\351\242\230\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..e26164b61a0c50dcb22a51acbb31ecd7836fe4c1 --- /dev/null +++ "b/47\346\250\212\345\260\217\351\203\255/20230319 SQL\347\232\204\345\255\246\347\224\237\350\257\225\351\242\230\344\275\234\344\270\232.md" @@ -0,0 +1,313 @@ +某某学校为了让学生学习mysql,安排你建立一个用于统计成绩和课程的数据库,数据库要求如下: + +数据库库名: mtyk + +创建四个表: + +教师表 (J_TEACHER) + +Tno 主键 自增 备注为教师编号 + +Tname 不可重复 备注为教师姓名 + +学生表(J_STUDENT) + +Sno 主键 自增 备注为学生编号 + +Sname 不可重复 备注为学生姓名 + +Sage 备注为学生年龄 + +Ssex 男or女 备注为性别 + +课程表(J_COURSE) + +Cno 主键 自增 备注为课程编号 + +Cname 不可重复 备注为课程名称 + +Tno 外键 备注为授课教师编号 关联J_TEACHER表主键 + +成绩表(J_SCORE) + +Sno 外键 备注为学生编号 关联J_STUDENT表主键 + +Cno 外键 备注为课程编号 关联J_COURSE 表主键 + +Score 备注为成绩 + +![img]() + +```mysql +create database mtyk charset utf8; + +use mtyk; + +create table 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( + 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( + 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( + 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分 + +```mysql +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段后面 + +```mysql +alter table J_STUDENT modify Sage datetime after Ssex; +``` + +3、 教师表添加一个用于统计状态的列 名字为now 默认值为有课 + +```mysql +alter table J_TEACHER add now varchar(20) default '有课'; +``` + +4、 将没有上课的老师的now列值设置为无课 + +```mysql +update J_TEACHER set now='无课' where tno not in (select tno from J_COURSE); +``` + +5、 修改课程表中的企业管理为比遛旺 + +```mysql +update J_COURSE set cname='比遛旺' where cname='企业管理'; +``` + +6、 查询课程1的成绩比课程2的成绩 高 的所有学生的学号。 + +```mysql +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分的同学的学号和平均成绩。 + +```mysql +select sno,avg(score) from J_SCORE group by sno having avg(score)>60; +``` + +8、 查询所有同学的学号、姓名、选课数、总成绩。 + +```mysql +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、 查询姓“李”的学生的个数。 + +```mysql +select count(sname) from J_STUDENT where sname like '李%'; +``` + +10、 查询没学过“叶平”老师课的同学的学号、姓名。 + +```mysql +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的同学的学号、姓名。 + +```mysql +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、 查询学过“叶平”老师所教所有课程的所有同学的学号、姓名。 + +```mysql +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的成绩 高的所有同学的学号、姓名。 + +```mysql +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分的同学的学号、姓名。 + +```mysql +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分的同学的学号、姓名。 + +```mysql +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、 查询没有学全所有课的同学的学号、姓名 + +```mysql +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的同学所学课程 相同的同学的学号和姓名 + +```mysql +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号同学学习的课程完全相同的其他同学学号和姓名。 + +```mysql +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、 查询各科成绩最高分和最低分。 + +```mysql +select cno,max(score),min(score) from J_SCORE group by cno; +``` + +20、 查询每门课程被选修的学生数。 + +```mysql +select cno,count(sno) from J_SCORE group by cno; +``` + +21、 查询出只选修了一门课程的全部学生的学号和姓名。 + +```mysql +select sno,sname from J_STUDENT where sno in (select sno from J_SCORE group by sno having count(cno)=1); +``` + +22、 查询同名同性学生名单,并统计同名人数。 + +```mysql +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、 查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩。 + +```mysql +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、 查询不同课程成绩相同的学生的学号、课程号、学生成绩。 + +```mysql +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 + +```mysql +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门课程的学生学号。 + +```mysql +select sno from J_SCORE group by sno having count(sno)>=5; +``` + +27、 查询没学过“叶平”老师讲授的任一门课程的学生姓名。 + +```mysql +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、 查询两门以上不及格课程的同学的学号及其平均成绩。 + +```mysql +select sno,avg(score) from J_SCORE where score<60 group by sno having count(sno)>2; +``` + +29、 查询最受欢迎的老师(选修学生最多的老师)。 + +```mysql +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、 修改课程表名为给乱给要酒要 + +```mysql +alter table J_COURSE rename to 乱给要酒要; +``` \ No newline at end of file