From 68d6d932d5ee7eeff3cf0cbe4a9dd84aa025f081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E6=99=93=E7=90=B3?= <2115152499@qq.com> Date: Wed, 1 Mar 2023 11:22:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 "29 \346\262\210\346\231\223\347\220\263/20230227 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/29 \346\262\210\346\231\223\347\220\263/20230227 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/29 \346\262\210\346\231\223\347\220\263/20230227 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..538600a --- /dev/null +++ "b/29 \346\262\210\346\231\223\347\220\263/20230227 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,185 @@ +## 一.笔记 + +### 关联查询 + +```sql +1.内连接 inner join ==> A∩B + A表 inner join B表 on 关联条件 +2.外连接 outer join +左外连接: left outer join 或 left join ==> A 或 A-A∩B + (1)A表: + A表 left join B表 on A表.关联字段 = B表.关联字段 + (2)A-A∩B: + A表 left join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A和B 看占从表位置的表。 +右外连接: right outer join 或 right join ==> B 或 B-A∩B + (1)B表:A表 right join B表 on A表.关联字段 = B表.关联字段 + (2)B-A∩B: + A表 right join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A还是B 得看占从表位置的表。 +全外连接: full outer join 或 full join -- union 代替 +==> A∪B 或 A∪B - A∩B + (1)A∪B + 转换为 左连接的A union 右连接的B + (2)A∪B - A∩B + 转换为 左连接的A-A∩B union 右连接的B-A∩B + +``` + + + +## 二.作业 + +```sql +-- 1. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 +CREATE DATABASE school CHARSET utf8; +use school; + +-- 2. 数据库的表结构 +-- 表(一)Student (学生表) +-- 属性名 数据类型 可否为空 含义 +-- Sno varchar (20) 否 学号(主码) +-- Sname varchar (20) 否 学生姓名 +-- Ssex varchar (20) 否 学生性别 +-- Sbirthday datetime 可 学生出生年月 +-- Class varchar (20) 可 学生所在班级 +create table student( +Sno VARCHAR(20) PRIMARY key, +Sname VARCHAR(20)not null, +Ssex VARCHAR(20)not null, +Sbirthday datetime, +Class VARCHAR(20)); +-- 表(二)Course(课程表) +-- 属性名 数据类型 可否为空 含义 +-- Cno varchar (20) 否 课程号(主码) +-- Cname varchar (20) 否 课程名称 +-- Tno varchar (20) 否 教工编号(外码) +create table Course( +Cno VARCHAR(20) primary key, +Cname VARCHAR(20)not null, +Tno VARCHAR(20)not null, +FOREIGN key Course(Tno) REFERENCES Teacher(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 Score(Sno) REFERENCES Student(Sno), +FOREIGN key Score(Cno) REFERENCES Course(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 PRIMARY key, +Tname VARCHAR(20)not null, +Tsex VARCHAR(20)not null, +Tbirthday datetime, +Prof VARCHAR(20), +Depart VARCHAR(20)not null); + +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',95033),(103,'陆君','男','1974-6-3',95031); +insert into Course VALUES('3-105','计算机导论',825),('3-245','操作系统',804),('6-166','数字电路',856),('9-888','高等数学',831); +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); +INSERT into Teacher VALUES(804,'李诚','男','1958-12-2','副教授','计算机系'),(856,'张旭','男','1969-3-12','讲师','电子工程系'),(825,'王萍','女','1972-5-5','助教','计算机系'),(831,'刘冰','女','1977-8-14','助教','电子工程系'); +-- 3. 数据库中的数据: +-- -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select student.sno,sname,ssex,sbirthday,class,cname +from student left join score on student.sno=score.sno +left join course on score.cno=course.cno; +-- -- 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 +-- 表(二)Course +-- Cno Cname Tno +-- 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 +-- 表(四)Teacher +-- Tno Tname Tsex Tbirthday Prof Depart +-- 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,cno,degree from score INNER JOIN student on score.sno = student.sno; +-- ③ 查询所有学生的Sno、Cname和Degree列。 +SELECT course.cname,sno,degree from score inner join course on score.cno = course.cno; +-- ④ 查询所有学生的Sname、Cname和Degree列。 +SELECT sname,cname,degree from score inner join student on score.sno=student.sno INNER JOIN course on score.cno=course.cno; +-- ⑤ 查询“95033”班学生的平均分。 +SELECT avg(degree) from score inner join Student on Score.Sno=Student.Sno where class=95033; +-- ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +SELECT * from student inner join score on student.sno=score.sno where score.cno='3-105' and degree>(select degree from score where score.cno='3-105' and score.sno=109); +-- ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select !(max(degree)) from score where 不会 +-- ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +SELECT * from score where degree>(SELECT degree from score where sno=109 and cno='3-105'); +-- ⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +SELECT sno,sname,sbirthday from student where sbirthday=(SELECT sbirthday from student where sno=108); +-- ⑩ 查询“张旭“教师任课的学生成绩。 +select degree from score left join course on score.cno=course.cno left join teacher on course.ton=teacher.tno where score.cno=(SELECT course.cno from course left join teacher on course.tno=teacher.tno where tname='张旭'); +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +不会 +-- 12 查询出“计算机系“教师所教课程的成绩表。 +SELECT * from score left join course on score.cno=course.cno left join teacher on course.tno=teacher.tno where depart='计算机系'; +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +SELECT tname,prof from teacher WHERE prof in(SELECT prof from teacher); +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +SELECT score.cno,sno,degree from score INNER JOIN course on score.cno=course.cno and score.cno='3-105' and score.degree>(SELECT max(degree) from score where score.cno='3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +SELECT student.sname,score.degree form score 不会 +-- 17 查询所有任课教师的Tname和Depart. +SELECT tname,depart from teacher; +-- 18 查询所有未讲课的教师的Tname和Depart. +不会 +-- 19 查询“男”教师及其所上的课程。 +SELECT tname,teacher.tno from teacher inner join course on teacher.tno=course.tno where tsex='男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 +select score.sno,cno,degree from score where degree=(SELECT max(degree) from score); +-- 21 查询和“李军”同性别的所有同学的Sname. +SELECT sname,ssex from student where ssex=(SELECT ssex from student where sname='李军'); +-- 22 查询和“李军”同性别并同班的同学Sname. +select sname from student where ssex=(SELECT ssex from student where sname='李军') and class=(select class from student where sname='李军'); +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select sname,ssex,degree from score INNER JOIN student on score.sno=student.sno INNER JOIN course on score.cno=course.cno where score.cno=(SELECT cno from course where cname='计算机导论' and ssex='男'); +-- +``` + -- Gitee