From f052a29a8d275194c334309133896cce5d1f002c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=98=80=E8=90=B1?= <1047723331@qq.com> Date: Wed, 1 Mar 2023 20:08:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= 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" | 320 ++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 "16 \346\236\227\346\230\200\350\220\261/20230228\345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/16 \346\236\227\346\230\200\350\220\261/20230228\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/16 \346\236\227\346\230\200\350\220\261/20230228\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..aae2a4e --- /dev/null +++ "b/16 \346\236\227\346\230\200\350\220\261/20230228\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,320 @@ +# 一、笔记 + +## -- 1.内连接 + +特点 两个表共有内容 + +示例 + +```sql +SELECT 字段列表 FROM A表 INNER JOIN B表; +``` + + + +## -- 2.左连接 + +特点 A表格全部内容和B表格共有内容 + +示例 + +```sql +SELECT 字段列表 FROM A表 LEFT JOIN B表; +``` + + + +## -- 3.右连接 + +特点 B表格全部内容和A表格共有内容 + +示例 + +```sql +SELECT 字段列表 FROM A表 RIGHT JOIN B表; +``` + + + +## -- 4全外连接 + +特点 A表全部和B表全部 + +示例 + +```sql +SELECT 字段列表 FROM A表 LEFT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +UNION +SELECT 字段列表 FROM A表 RIGHT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列; +``` + + + +## -- 5全外连接 + +特点 除去A表和B表共有的 + +示例 + +```sql +SELECT 字段列表 FROM A表 LEFT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +WHERE 条件 +UNION +SELECT 字段列表 FROM A表 RIGHT JOIN B表 +ON A表.两表都有的列 = B表.两表都有的列 +WHERE 条件; +``` + + + +# 二、作业 + +1. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 -- + +```sql +create database qaq charset utf8; +``` + +1. **数据库的表结构** + +```sql +use yyg; +``` + +## 表(一)Student (学生表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ------------ | +| Sno | varchar (20) | 否 | 学号(主码) | +| Sname | varchar (20) | 否 | 学生姓名 | +| Ssex | varchar (20) | 否 | 学生性别 | +| Sbirthday | datetime | 可 | 学生出生年月 | +| Class | varchar (20) | 可 | 学生所在班级 | + +```sql +create table Student( + Sno varchar(20) primary key comment"学号", + Sname varchar(20) not null comment"学生姓名", + Ssex varchar(20) not null comment"学生性别", + Sbirthday datetime comment'学生出生年月', + Class varchar(20) comment'学生所在班级' +); +``` + +## 表(二)Course(课程表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| ------ | ------------ | -------- | ---------------- | +| Cno | varchar (20) | 否 | 课程号(主码) | +| Cname | varchar (20) | 否 | 课程名称 | +| Tno | varchar (20) | 否 | 教工编号(外码) | +| | | | | + +```sql +create table Course( + Cno varchar (20) primary key comment'课程号', + Cname varchar(20) not null comment'课程名称 ', + Tno varchar(20) not null comment'教工编号', + foreign key (Tno) references Teacher(Tno) +); +``` + +## 表(三)Score(成绩表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| -------------- | ------------ | -------- | -------------- | +| Sno | varchar (20) | 否 | 学号(外码) | +| Cno | varchar (20) | 否 | 课程号(外码) | +| Degree | Decimal(4,1) | 可 | 成绩 | +| 主码:Sno+ Cno | | | | + +```sql +create table Score( + Sno varchar(20) not null comment'学号', + Cno varchar(20) not null comment'课程号', + Degree Decimal(4,1) comment'成绩', + foreign key (Sno) references Student(Sno), + foreign key (Cno) references Course(Cno), + primary key(Sno,Cno) +); +``` + +## 表(四)Teacher(教师表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ---------------- | +| Tno | varchar (20) | 否 | 教工编号(主码) | +| Tname | varchar (20) | 否 | 教工姓名 | +| Tsex | varchar (20) | 否 | 教工性别 | +| Tbirthday | datetime | 可 | 教工出生年月 | +| Prof | varchar (20) | 可 | 职称 | +| Depart | varchar (20) | 否 | 教工所在部门 | + +```sql +create table Teacher( + Tno varchar(20) primary key not null comment'教工编号', + Tname varchar(20)not null comment'教工姓名', + Tsex varchar(20)not null comment'教工性别', + Tbirthday datetime comment'教工出生年月', + Prof varchar(20) comment'职称', + Depart varchar(20)not null comment'教工所在部门' +); +``` + +## 表(一)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 | + +```sql +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 | + +```sql +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 | + +```sql +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 | +| ---- | ----- | ---- | --------- | ------ | +| 804 | 李诚 | 男 | 1958-12-2 | 副教授 | +| 856 | 张旭 | 男 | 1969-3-12 | 讲师 | +| 825 | 王萍 | 女 | 1972-5-5 | 助教 | +| 831 | 刘冰 | 女 | 1977-8-14 | 助教 | + +```sql +insert into Teacher values + (804,'李诚','男','1958-12-2','副教授','计算机系'), + (856,'张旭','男','1969-3-12','讲师','电子工程系'), + (825,'王萍','女','1972-5-5','助教','计算机系'), + (831,'刘冰','女','1977-8-14','助教','电子工程系'); +``` + +1. **数据库中的数据**: -- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ + +```sql +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,查询没有学生的教师的所有信息 + +```sql +select Teacher.Tno,Tname,Tsex,Tbirthday,Prof,Depart +from Teacher left join Course on Teacher.Tno = Course.Tno +left join Score on Course.Cno = Score.Cno +where Score.Sno is null; +``` + +1. 查询 -- + +```sql +-- 1 查询Score表中的最高分的学生学号和课程号。 +select Sno 学号,Cno 课程号 from Score +where Degree = (select max(Degree) from Score); +-- 2 查询所有学生的Sname、Cno和Degree列。 +select Sname,Cno,Degree +from Student inner join Score on Student.Sno = Score.Sno; +-- 3 查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from Score inner join Course on Score.Cno = Course.Cno; +-- 4 查询所有学生的Sname、Cname和Degree列。 +select Sname,Cname,Degree from Student,Course,Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno; +-- 5 查询“95033”班学生的平均分。 +select avg(Degree) from Score inner join Student on Score.Sno=Student.Sno where class=95033; +-- 6 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from Score where Degree>(select Degree from Score where Sno = '109' and Cno = '3-105' ) and Cno = '3-105'; +-- 7 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from Score a where Degree <(select MAX(degree) from Score b where a.Cno=b.Cno) and Sno in(select Sno from Score group by Sno having count(*)>1); +-- 8 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from Score where Degree >(select Degree from Score where Sno='109' and Cno='3-105'); +-- 9 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from Student where Sbirthday=(select Sbirthday from Student where Sno='108'); +-- 10 查询“张旭“教师任课的学生成绩。 +-select Sno,Degree from Score where Cno=(select Cno from Course inner join Teacher ON Course.Tno= Teacher.Tno where Tname='张旭' ); +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>5)); +-- 12 查询出“计算机系“教师所教课程的成绩表。 +select sno,Cno ,Degree from Score where Cno in +(select Cno from Course where Tno IN (select Tno from Teacher where Depart='计算机系')); +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from Teacher where prof in (select Prof from Teacher where Teacher.Depart = '计算机系') xor prof in (select prof from Teacher where Teacher.Depart = '电子工程系' ); +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno= '3-245'); +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select * from Score a WHERE Degree <(select avg(Degree) from score b where a.cno =b.cno); +-- 17 查询所有任课教师的Tname和Depart. +select Tname,Depart from Teacher; +-- 18 查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from Teacher left join Course on Teacher.Tno=Course.Tno left join Score on Course.Cno=Score.Cno where Score.Cno is null; +-- 19 查询“男”教师及其所上的课程。 +select Tname,Depart from Teacher where Tsex = '男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 +select Sno,Cno,degree from Score where Degree = (select max(Degree) from Score); +-- 21 查询和“李军”同性别的所有同学的Sname. +select Sname 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 * from Score where Sno in (select Sno from Student where Ssex ='男') and Cno = (select Cno from Course where Cname='计算机导论'); +``` \ No newline at end of file -- Gitee