diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230301 MySQL\345\207\275\346\225\260\345\222\214\350\201\224\345\220\210\346\237\245\350\257\242\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/23 \351\273\204\346\265\251\344\270\234/20230301 MySQL\345\207\275\346\225\260\345\222\214\350\201\224\345\220\210\346\237\245\350\257\242\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..2647ef0a2397421678630c4f3600f7121d7d3acc --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230301 MySQL\345\207\275\346\225\260\345\222\214\350\201\224\345\220\210\346\237\245\350\257\242\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,238 @@ +# 作业 + +### 1 建表 + +```mysql +#建表 +REATE table Student (Sno varchar(20) PRIMARY KEY,Sname varchar(20) not null, Ssex varchar(20) not null,Sbirthday datetime,Class varchar(20)); +create table Course(Cno varchar(20) PRIMARY KEY, Cname varchar(20) not null,Tno varchar(20) not null); +create table Score(Sno varchar(20) not null,Cno varchar(20) not null,Degree Decimal(4,1),PRIMARY KEY (Sno,Cno)); +create table Teacher(Tno varchar(20) PRIMARY KEY,Tname varchar(20) not null,Tsex varchar(20) not null,Tbirthday datetime,Prof varchar(20),Depart varchar(20) not null); +``` + +```mysql +#建外键 +alter table Course add foreign key (Tno) REFERENCES Teacher (Tno); +alter table Score add foreign key (Sno) REFERENCES Student (Sno); +alter table Score add foreign key (Cno) REFERENCES Course (Cno); +``` + +-- 3. 数据库中的数据: +1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息 + +```mysql +Select a.*,c.Cname,c.Cno from Student a left join score b on a.Sno = b.Sno left join course c on b.Cno = c.Cno +``` + + 2,查询没有学生的教师的所有信息 + +```mysql +select a.*,b.* from teacher a LEFT JOIN course b on a.tno = b.tno left JOIN score c on b.Cno= c.Cno where b.Cno not in (select Cno from score); +``` + +### 2 插入数据 + +-- 表(一)Student +-- Sno Sname Ssex Sbirthday class +-- 108 曾华 男 1977-9-1 95033 +-- 105 匡明 男 1975-10-2 95031 +-- 107 王丽 女 1976-1-23 95033 +-- 101 李军 男 1976-2-20 95033 +-- 109 王芳 女 1975-2-10 95031 +-- 103 陆君 男 1974-6-3 95031 + +```mysql +insert into Student values (108,'曾华','男','1977-9-1',95033),(105,'匡明','男','1975-10-2','95031'),(107,'王丽','女','1976-1-23',95033),(101,'李军','男','1976-2-20',95033),(109,'王芳','女','1975-2-10',95031),(103,'陆君','男','1974-6-3',95031); +#第一个添加 +``` + +-- 表(二)Course +-- Cno Cname Tno +-- 3-105 计算机导论 825 +-- 3-245 操作系统 804 +-- 6-166 数字电路 856 +-- 9-888 高等数学 831 + +```mysql +insert into Course values('3-105','计算机导论',825),('3-245','操作系统',804),('6-166','数字电路',856),('9-888','高等数学',831); +``` + +-- 表(三)Score +-- Sno Cno Degree +-- 103 3-245 86 +-- 105 3-245 75 +-- 109 3-245 68 +-- 103 3-105 92 +-- 105 3-105 88 +-- 109 3-105 76 +-- 101 3-105 64 +-- 107 3-105 91 +-- 108 3-105 78 +-- 101 6-166 85 +-- 107 6-166 79 +-- 108 6-166 81 + +```mysql +insert into Score values(103,'3-245',86),(105,'3-245',75),(109,'3-245',68),(103,'3-105',92),(105,'3-105',88),(109,'3-105',76),(101,'3-105',64),(107,'3-105',91),(108,'3-105',78),(101,'6-166',85),(107,'6-166',79),(108,'6-166',81); +``` + +-- 表(四)Teacher +-- Tno Tname Tsex Tbirthday Prof Depart +-- 804 李诚 男 1958-12-2 副教授 计算机系 +-- 856 张旭 男 1969-3-12 讲师 电子工程系 +-- 825 王萍 女 1972-5-5 助教 计算机系 +-- 831 刘冰 女 1977-8-14 助教 电子工程系 + +```mysql +insert into Teacher values (804,'李诚','男','1958-12-2','副教授','计算机系'),(856,'张旭','男','1969-3-12','讲师','电子工程系'),(825,'王萍','女','1972-5-5','助教','计算机系'),(831,'刘冰','女','1977-8-14','助教','电子工程系');#第二个添加 +``` + +### 3 查询 + +-- ① 查询Score表中的最高分的学生学号和课程号。 + +```mysql +select Cno,Sno from Score where Degree=(select MAX(Degree) from Score); +``` + +-- ② 查询所有学生的Sname、Cno和Degree列。 + +```mysql +select Sname,Cno,Degree from Student a LEFT JOIN Score b on a.Sno=b.Sno; +``` + + + +-- ③ 查询所有学生的Sno、Cname和Degree列。 + +```mysql +select Sno,Cname,Degree from Score a LEFT JOIN Course b on a.Cno=b.Cno; +``` + +-- ④ 查询所有学生的Sname、Cname和Degree列。 + +```mysql +select b.Sname,c.Cname,Degree from Score a LEFT JOIN Student b on a.Sno=b.Sno left join Course c on a.Cno=c.Cno; +``` + +-- ⑤ 查询“95033”班学生的平均分。 + +```mysql +select avg(Degree) from score a left JOIN student b on a.Sno = b.Sno where b.Class = 95033; +``` + +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 + +```mysql +select * from score a left join student b on a.Sno = b.Sno where a.Cno = '3-105' and degree>(select degree from score where Cno = '3-105'and Sno = 109) ; +``` + +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 + +```mysql +select * from score a WHERE a.degree not in (SELECT max(b.Degree) from score b where a.Cno = b.cno); +``` + +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 + +```mysql +select * from score where Cno = '3-105' and degree > (select degree from score where Sno = 109 and Cno = '3-105'); +``` + +#分组函数不能用于where语句 + +```mysql +select * from score a left JOIN student b on a.Sno = b.Sno where degree>(select Degree from score where Sno=109 and Cno = '3-105'); +``` + +-- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 + +```mysql +select Sno,Sname,Sbirthday from student where YEAR(Sbirthday) = (select YEAR(Sbirthday) from student where Sno = 108); +``` + +-- ⑩ 查询“张旭“教师任课的学生成绩。 + +```mysql +select Tname,degree from score a LEFT JOIN course b on a.Cno = b.Cno LEFT JOIN teacher c on b.Tno = c.Tno where c.Tname = (select Tname from teacher where Tname = '张旭'); +``` + +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 + +```mysql +# HAVING语句用于分组后筛选 +SELECT Tname FROM teacher WHERE Tno = ( SELECT tno FROM course WHERE cno =( SELECT cno FROM score GROUP BY cno HAVING COUNT ( Cno )> 5 )); +``` + +-- 12 查询出“计算机系“教师所教课程的成绩表。 + +```mysql +SELECT Tname,a.Tno,b.Cno,c.Sno,c.Degree FROM teacher a LEFT JOIN course b ON a.Tno = b.Tno LEFT JOIN score c ON b.Cno = c.Cno WHERE a.depart = '计算机系'; +``` + +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 + +```mysql +SELECT Tname,Prof FROM teacher WHERE prof not in (select prof from teacher where Depart = '电子工程系') AND Depart = '计算机系'; +``` + +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 + +```mysql +select Cno,Sno,Degree from score where Cno = '3-105' AND Degree> ANY (select degree from score where Cno = '3-245') ORDER BY Degree; +``` + +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. + +```mysql +select Cno,Sno,Degree from score where Cno = '3-105' and Degree > all (select degree from score where Cno = '3-245'); +``` + +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 + +```mysql +select * FROM score a where Degree < (select avg(degree)from score b where a.Cno = b.Cno); +``` + +-- 17 查询所有任课教师的Tname和Depart. + +```mysql +select Tname,Depart from teacher; +``` + +-- 18 查询所有未讲课的教师的Tname和Depart. + +```mysql +select Tname,Depart from teacher a LEFT JOIN course b on a.tno = b.tno left JOIN score c on b.Cno= c.Cno where b.Cno not in (select Cno from score); +``` + +-- 19 查询“男”教师及其所上的课程。 + +```mysql +select b.Cname,a.Tsex from teacher a LEFT JOIN course b on a.Tno=b.Tno where a.Tsex='男' ; +``` + +-- 20 查询最高分同学的Sno、Cno和Degree列。 + +```mysql +select a.Sno,a.Cno,a.Degree from score a where a.degree = (select MAX(b.degree) from score b where a.Cno=b.Cno); +``` + +-- 21 查询和“李军”同性别的所有同学的Sname. + +```mysql +select Sname,Ssex from student where Ssex = (select Ssex from student where Sname = '李军'); +``` + +-- 22 查询和“李军”同性别并同班的同学Sname. + +```mysql +select Sname,Ssex,Class from student where Ssex = (select Ssex from student where Sname = '李军') and Class = (select Class from student where Sname = '李军'); +``` + +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 + +```mysql +select c.Cname,Sname,b.Ssex,a.Degree FROM score a LEFT JOIN student b on a.Sno = b.Sno left JOIN course c on a.Cno=c.Cno where b.Ssex = '男' and c.Cname = '计算机导论' +``` + diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230307 MySQL\346\237\245\350\257\242\347\273\274\345\220\210\344\275\234\344\270\232.md" "b/23 \351\273\204\346\265\251\344\270\234/20230307 MySQL\346\237\245\350\257\242\347\273\274\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..6e06a54b37afda754bfb36992565f73ddedc0ac9 --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230307 MySQL\346\237\245\350\257\242\347\273\274\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,61 @@ +## 作业 + +### 1建表 + +```MySQL +create database homework charset utf8; +use homework; +CREATE table stuinfo(stuNO char(5),stuName varchar(30) not NULL,stuSex char,stuAge char(2),stuAddress varchar(20),stuSeat int, primary key (stuNO),unique key (stuSeat)); +CREATE table stuExam (examNO int not NULL,stuNO char(5),writtenExam int not null default 0,labExam int not null default 0,primary key (stuNO),UNIQUE key (examNO)); +create table stuMarks (examNO int not null,stuID char(5) not null,score int not null DEFAULT 0 , UNIQUE key (examNo)); +``` + +### 2查询 + +```MySQL +insert into stuinfo values ('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',18,'湖南长沙',3),('s2504','欧阳俊雄','女',21,'湖北武汉',4),('s2505','梅超风','男',20,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',6); +INSERT into stuExam values (1,'s2501',50,70),(2,'s2502',60,65),(3,'s2503',86,70),(4,'s2504',40,80),(5,'s2505',70,85),(6,'s2506',85,90); +insert into stuMarks values(1,'s2501',88),(2,'s2501',92),(3,'s2501',53),(4,'s2502',60),(5,'s2502',99),(6,'s2503',82); +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuAge > (select avg(stuAge) from stuinfo ) +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +-- sutNO,stuName,stuSex,MAX(score) +select a.stuNO,a.stuName,a.stuSex,b.score from stuinfo a LEFT JOIN (select stuID,score from stuMarks c where score = (select max(score) from stuMarks d where c.stuID = d.stuID))b on a.stuNO = b.stuID where b.score is not null; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +-- stuNO,stuName,stuSex,avg(writeemExame+labExame) +select a.stuNO,a.stuName,a.stuSex,b.`avg` from stuinfo a +left join +(select c.stuNO,((c.writtenExam+c.labExam)/2) as `avg` from stuexam c) b +on a.stuNO = b.stuNO +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuAge >= 20 and stuSex = '男'; +select * from stuinfo where stuNO = any(select stuNO from stuinfo where stuAge >= 20 and stuSex = '男'); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select *from stuinfo where stuSex = '女' and stuAge > (select max(stuAge) from stuinfo where stuSex ='男'); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a left join stumarks b on a.stuNO=b.stuID where b.score >= 60; + +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a left join stumarks b on a.stuNO = b.stuID where score is not NULL; +select * from stuinfo where stuNO in (select stuID from stumarks where score is not null); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a left join stumarks b on a.stuNO = b.stuID where score is NULL; +select * from stuinfo where stuNO not in (select stuID from stumarks where score is not null); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.* from stuinfo a left join stumarks b on a.stuNO = b.stuID where b.score>90; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo a LEFT JOIN +(select stuID,AVG(score) c from stuMarks GROUP BY stuID having AVG(score)>80)b +on a.stuNO = b.stuID where b.c is not null; + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select DISTINCT a.* from stuinfo a left JOIN stumarks b on a.stuNo=b.stuID where b.score > all(SELECT score from stumarks c LEFT JOIN stuinfo d ON c.stuID = d.stuNO where d.`stuName`='张秋利' ) and stuName != '张秋利' +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select DISTINCT a.* from stumarks b RIGHT JOIN stuinfo a on a.stuNo=b.stuID where b.score > any(SELECT score from stumarks c LEFT JOIN stuinfo d ON c.stuID = d.stuNO where d.`stuName`='张秋利' ) and a.stuName != '张秋利' + +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select *from stuinfo where stuSex = '女' and stuAge > (select max(stuAge) from stuinfo where stuSex ='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 +select *from stuinfo where stuSex = '女' and stuAge > any(select stuAge from stuinfo where stuSex ='男'); +``` +