InsertInto St (S#, Sname, avgScore) Select S#, Sname, Avg(Score) From Student, SC Where Student.S# = SC.S# Groupby Student.S# ;
删除(Delete)
1
DeleteFrom 表名[ Where 条件表达式] ;
如果 Where 条件省略,则删除所有的元组。
例 1:删除 98030101 号同学所选的所有课程
1
DeleteFrom SC Where S# ='98030101' ;
例 2:删除自动控制系的所有同学
1 2 3
DeleteFrom Student Where D# In (Select D# From Dept Where Dname ='自动控制');
例 3:删除有四门不及格课程的所有同学
1 2 3 4 5 6 7
DeleteFrom Student Where S# in ( Select S# From SC Where Score <60 Groupby S# HavingCount(*)>=4);
更改(Update)
1 2 3 4
Update 表名 Set 列名= 表达式| (子查询) [ [ , 列名= 表达式| (子查询) ] … ] [ Where 条件表达式] ;
如果 Where 条件省略,则更新所有的元组。
例 1:将所有教师工资上调 5%
1 2
Update Teacher Set Salary = Salary *1.05 ;
例 2:将所有计算机系的教师工资上调 10%
1 2 3 4 5 6
Update Teacher Set Salary = Salary *1.1 Where D# in ( Select D# From Dept Where Dname ='计算机');
例 4:当某同学 001 号课的成绩低于该课程平均成绩时,将该同学该门课成绩提高 5%
1 2 3 4 5 6
Update SC Set Score = Score *1.05 Where C# ='001'and Score <some( SelectAVG(Score) From SC Where C# ='001') ;
例 5:将张三同学 001 号课的成绩置为其班级该门课的平均成绩
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Update SC Set Score = ( SelectAVG(SC2.Score) From SC SC1, Student S1, SC SC2, Student S2 Where S1.Sclass = S2.Sclass and SC1.S# = S1.S# and SC2.S# = S2.S# and S1.Sname='张三' and SC1.C# ='001' and SC1.C# = SC2.C#) Where C# = ‘001’ and S# in ( Select S# From Student Where Sname = ‘张三’ );
Select S#, Sname From Student Where S# in ( Select S# From SC Where C# = ‘001’ );
例 3:求既学过 001 号课程,又学过 002 号课程的学生的学号
1 2 3 4 5 6 7
Select S# From SC Where C# ='001' and S# in ( Select S# From SC Where C# ='002') ;
例 4:列出没学过李明老师讲授课程的所有同学的姓名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Select Sname From Student Where S# notin ( Select S# From SC, Course C, Teacher T Where T.Tname ='李明' and SC.C# = C.C# and T.T# = C.T# );
-- 错误写法 Select Sname From Student S, SC, Course C, Teacher T Where T.Tname <>'李明' and C.C# = SC.C# and SC.S# = S.S# and T.T# = C.T#;
非相关子查询:内层查询独立进行,没有涉及任何外层查询相关信息的子查询。
相关子查询:内层查询需要依靠外层查询的某些参量作为限定条件才能进行的子查询
外层向内层传递的参量需要使用外层的表名或表别名来限定
例 5:求学过 001 号课程的同学的姓名
1 2 3 4 5 6
Select Sname From Student Stud Where S# in ( Select S# From SC Where S# = Stud.S# and C# ='001');
2. θ-Some 与 θ-All 子查询
1 2
表达式 θ some (子查询) 表达式 θ all (子查询)
θ 是比较运算符: < , > , >= , <= , = , <>。
如果表达式的值至少与子查询结果的某一个值相比较满足 θ 关系,则表达式 θ some (子查询)的结果便为真;
如果表达式的值与子查询结果的所有值相比较都满足 θ 关系,则表达式 θ all (子查询)的结果便为真。
例 1:找出工资最低的教师姓名
1 2 3 4 5
Select Tname From Teacher Where Salary <=all ( Select Salary From Teacher);
例 2:找出 001 号课成绩不是最高的所有学生的学号
1 2 3 4 5 6 7
Select S# From SC Where C# ='001' and Score <some ( Select Score From SC Where C# ='001');
例 3:找出所有课程都不及格的学生姓名(相关子查询)
1 2 3 4 5 6
Select Sname From Student Where60>all ( Select Score From SC Where S# = Student.S#);
例 4:找出 001 号课成绩最高的所有学生的学号
1 2 3 4 5 6 7
Select S# From SC Where C# ='001' and Score >=all ( Select Score From SC Where C# ='001');
例 5:找出 98030101 号同学成绩最低的课程号
1 2 3 4 5 6 7
Select C# From SC Where S# ='98030101' and Score <=all ( Select Score From SC Where S# ='98030101');
例 6:找出张三同学成绩最低的课程号
1 2 3 4 5 6 7
Select C# From SC, Student S Where Sname ='张三' and S.S#=SC.S# and Score <=all ( Select Score From SC Where S#=S.S# );
等价关系
表达式 in (子查询)等价于表达式 = some (子查询);
表达式 not in (子查询)等价于表达式 <> all (子查询),**而不是表达式 <> some (子查询)**。
3.(NOT)EXISTS 子查询
1
[not] Exists (子查询);
语义:子查询结果中有无元组存在。
不加not形式的Exists谓词可以不用。
例 1:检索学过 001 号教师主讲的所有课程的所有同学的姓名
1 2 3 4 5 6 7 8 9 10
Select Sname From Student Wherenotexists ( -- 不存在 Select*-- 有一门001教师主讲课程 From Course Where Course.T# ='001' andnotexists ( -- 该同学没学过 Select* From SC Where S# = Student.S# and C# = Course.C#));
例 2:列出没学过李明老师讲授任何一门课程的所有同学的姓名
1 2 3 4 5 6 7 8 9
Select Sname From Student Wherenotexists ( Select* From Course, SC, Teacher Where Tname ='李明' and Course.T# = Teacher.T# and Course.C# = SC.C# and S# = Student.S# );
例 3:列出至少学过 98030101 号同学学过所有课程的同学的学号
1 2 3 4 5 6 7 8 9 10 11
Select DISTINC S# From SC SC1 Wherenotexists ( Select* From SC SC2 Where SC2.S# ='98030101' andnotexists ( Select* From SC Where C# = SC2.C# and S# = SC1.S#));
SelectDISTINCT Jno From SPJ SPJ1 Wherenotexists ( Select* From SPJ SPJ2 Where SPJ2.Sno ='S1' andnotexists ( Select* From SPJ SPJ3 Where SPJ3.Pno = SPJ2.Pno and SPJ3.Jno = SPJ1.Jno ) );