子查询
θsome与 θall子查询
=some和in的关系同
表达式 = some(子查询)
表达式 in (子查询)
select
Sname
from
Student S
where
S#
in(select S# from SC where S# = S.S# and C#='001');
select
Sname
from
Student S
where
S# =some (select S# from SC where S# = S.S# and C#='001');
<>all和not in的关系同
select
Sname
from
Student S
where
S# not in(select S# from SC where S# = S.S# and C#='001');
select
Sname
from
Student S
where
S# <>all (select S# from SC where S# = S.S# and C#='001');
将张三同学001号课的成绩置为其班级该门课的平均成绩
update
SC
set
score =
(select AVG(SC2.Score)
from
SC SC1,Student S1,SC SC2,Student S2
where S1.class = S2.class
and
SC1.S# = S1.S#
and
SC2.S# = S2.S#
and
and S1.Sname ='张三'
and
SC1.S# = '001'
and
SC1.C# = SC2.C#
)
AB CD四个表 AC关联 BD关联
然后拿AB表去做题目里的限制
拿CD表去取AB已经做完限制后剩下的正确的某列的值
exists子查询
[not] exists(子查询)//子查询中有无元组存在
例1
例2
例3
例4
例5
关系代数操作
集合并交差操作
子查询{Union[all]|intersect[all}|except[all] 子查询}
通常情况下自动删除重复元组:不带all
Union all m+n次
intersect all min(m,n)次
except all max(0,m-n)次
空值处理
is null
is not null
内连接、外连接
笛卡尔积:单纯的排列组合 自然连接:自然连接是一种特殊的等值连接,它会把重复列消除。
自然连接一定是等值连接,等值连接不一定是自然连接。
tip:等值连接要求相等的分量,不一定是公共属性;而自然连接要求相等的分量必须是公共属性。
视图及其应用
三级模式两层映像结构
(1)视图概念和结构(存的是导出视图的公式)
(2)视图定义
(3)视图使用
(4)视图更新
①含有聚合函数不可更新
②缺少主键列的不可更新
③还有下图的
(5)视图撤销
drop view view_name
总结
图片来源:战德臣教授《数据库系统》