diff --git "a/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" "b/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..5873cebcfc70d01740d8aa33ccbccc566982d6a1 --- /dev/null +++ "b/10 \347\253\240\347\254\221\345\256\271/20230227 \345\207\275\346\225\260\344\270\216\345\244\232\350\241\250\346\237\245\350\257\242.md" @@ -0,0 +1,250 @@ +# 1、笔记 + +```sql +-- 计算平均值 +avg(x) + select avg(字段名) from 表名; +-- 计算和 +sum(x) + #简单求和 + select sum(字段名) from 表名; + #计算表达式求和 + select sum(字段名*(1+ifnull(字段名,代替值))); +-- 计算最大值 +max(x) + select max(字段名) from 表名; +-- 计算最小值 +min(x) + select min(字段名) from 表名; +-- 统计记录 +count(x) + #统计实际的行数 + select count(*) from 表名; + #只统计"字段/表达式"部分非空值得行数 + select count(字段/表达式) from 表名; + +-- 内联查询 +select 字段名 from 表1 [inner] join 表2 on 条件; -- 显式内连接 +select 字段名 from 表1,表2 where 条件; -- 隐式内连接 +select 字段名 from 表1 natural join 表2; -- 自然连接 +-- 左外联查询 +select 字段名 from 表1 left join 表2 on 条件; +-- 右外联查询 +select 字段名 from 表1 right join 表2 on 条件; +-- 多表查询 +select 字段名 from 表1,表2,表3... where 表1.关联字段 = 表2.关联字段 and 表1.关联字段 = 表3.关联字段 ...; +select 字段名 from 表1 inner join 表2 on 条件 + inner join 表3 on 条件...; +``` + + + +# 2、作业 + +```sql +use mockq; +-- 1.设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 +-- 2.数据库的表结构 +-- 表(一)Student (学生表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(主码) +-- Sname varchar (20) 否 学生姓名 +-- Ssex varchar (20) 否 学生性别 +-- Sbirthday datetime 可 学生出生年月 +-- Class varchar (20) 可 学生所在班级 +create table student( + Sno varchar(20) not null, + Sname varchar(20) not null, + Ssex varchar(20) not null, + Sbirthday datetime, + Class varchar(20), + primary key(Sno) +); + +-- 表(二)Course(课程表) +-- 属性名 数据类型 可否为空 含义 +-- Cno varchar (20) 否 课程号(主码) +-- Cname varchar (20) 否 课程名称 +-- Tno varchar (20) 否 教工编号(外码) +-- drop table score; +create table course( + Cno varchar(20) not null, + Cname varchar(20) not null, + Tno varchar(20) not null, + primary key(Cno), + foreign key(Tno) references teacher(Tno) on update cascade +); +-- alter table course add constraint fk_course_Tno foreign key(Tno) references teacher(Tno); +-- alter table course drop foreign key fk_course_Tno; +-- 表(三)Score(成绩表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(外码) +-- Cno varchar (20) 否 课程号(外码) +-- Degree Decimal(4,1) 可 成绩 +-- 主码:Sno+ Cno +create table score( + Sno varchar(20) not null, + Cno varchar(20) not null, + Degree decimal(4,1), + foreign key(Sno) references student(Sno) on update cascade, + foreign key(Cno) references course(Cno) on update cascade +); +alter table score add primary key(Sno,Cno); +-- 表(四)Teacher(教师表) +-- 属性名 数据类型 可否为空 含义 +-- Tno varchar (20) 否 教工编号(主码) +-- Tname varchar (20) 否 教工姓名 +-- Tsex varchar (20) 否 教工性别 +-- Tbirthday datetime 可 教工出生年月 +-- Prof varchar (20) 可 职称 +-- Depart varchar (20) 否 教工所在部门 +create table teacher( + Tno varchar(20) not null, + Tname varchar(20) not null, + Tsex varchar(20) not null, + Tbirthday datetime, + Prof varchar(20), + Depart varchar(20) not null, + primary key(Tno) +); +-- 3.数据库中的数据: +-- -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select * from student inner join course; +-- -- 2,查询没有学生的教师的所有信息 +select * from student,score,course,teacher where student.Sno = score.Sno and course.Cno = score.Cno and course.Tno = teacher.Tno; + +select * from student inner join score on student.Sno = score.Sno + inner join course on course.Cno = score.Cno + inner join teacher on course.Tno = teacher.Tno; + +-- +-- 表(一)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 +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 +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 +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','64'), + ('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 助教 电子工程系 +insert into teacher values + ('804','李诚','男','1958-12-2','副教授','计算机系'), + ('856','张旭','男','1969-3-12','讲师','电子工程系'), + ('825','王萍','女','1972-5-5','助教','计算机系'), + ('831','刘冰','女','1977-8-14','助教','电子工程系'); +-- 4.查询 +-- ① 查询Score表中的最高分的学生学号和课程号。 +select Sno,Cno from score where Degree = (select max(Degree) from score); +-- ② 查询所有学生的Sname、Cno和Degree列。 +select student.Sname,score.Cno,score.Degree from student,score where score.Sno = student.Sno; -- 隐式内连接 + +select student.Sname,score.Cno,score.Degree from student inner join score on score.Sno = student.Sno; -- 显式内连接 + +select student.Sname,score.Cno,score.Degree from student natural join score; -- 自然连接 +-- ③ 查询所有学生的Sno、Cname和Degree列。 +select score.Sno,course.Cname,score.Degree from score inner join course on score.Cno = course.Cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +select student.Sname,course.Cname, score.Degree from student,course,score where student.Sno = score.Sno and course.Cno = score.Cno; +-- ⑤ 查询“95033”班学生的平均分。 +select avg(score.Degree) from score inner join student on student.Sno = score.Sno and student.Class = '95033'; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from student inner join score on student.Sno = score.Sno and score.Degree = (select !(max(score.Degree)) from score); +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from student inner join score on student.Sno = score.Sno and score.Cno = '3-105' and score.Degree > 76; +-- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from student where year(Sbirthday) = '1977'; +-- ⑩ 查询“张旭“教师任课的学生成绩。 +select score.Degree from score inner join course on score.Cno = course.Cno + inner join teacher on course.Tno = teacher.Tno + and teacher.Tname = '张旭'; +-- ⑪ 查询选修某课程的同学人数多于5人的教师姓名。 +select teacher.Tname from teacher inner join course on teacher.Tno = course.Tno + inner join score on course.Cno = score.Cno + and score.Cno = (select count(Cno) > 5 from score); +-- ⑫ 查询出“计算机系“教师所教课程的成绩表。 +select score.Sno,score.Cno,score.Degree from course inner join score on course.Cno = score.Cno + inner join teacher on course.Tno = teacher.Tno and teacher.Depart = '计算机系'; +-- ⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from teacher where Prof in (select distinct Prof from teacher); +-- select distinct Prof from teacher; +-- ⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select score.Cno,score.Sno,score.Degree from score inner join course on score.Cno = course.Cno + and score.Cno = '3-105' + and score.Degree > 75 order by score.Degree desc; +-- ⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select score.Cno,score.Sno,score.Degree from score inner join course on score.Cno = course.Cno + and score.Cno = '3-105' + and score.Degree > 75; +-- ⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 +select * from score where Degree < (select avg(Degree) from score); +-- ⑰ 查询所有任课教师的Tname和Depart. +select Tname,Depart from teacher; +-- ⑱ 查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from teacher inner join course on teacher.Tno = course.Tno and course.Cno is null; +-- ⑲ 查询“男”教师及其所上的课程。 +select teacher.Tname,course.Cname from course inner join teacher on teacher.Tno = course.Tno and teacher.Tsex = '男'; +-- ⑳ 查询最高分同学的Sno、Cno和Degree列。 +select * from score where Degree = (select max(Degree) from score); +-- 21 查询和“李军”同性别的所有同学的Sname. +select Sname from student where Ssex = '男' and Sname != '李军'; +-- 22 查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex = '男' and Sname != '李军' and Class = '95033'; +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select score.Cno,score.Sno,score.Degree from score inner join student on score.Sno = student.Sno + inner join course on score.Cno = course.Cno + and course.Cname = '计算机导论' + and student.Ssex = '男'; +``` +