第5讲 关系演算
概述
关系演算是以数理逻辑中的谓词演算为基础的。按谓词变量的不同,可分为关系元组演算和关系域演算。
使用关系演算可以对关系进行查询操作。
关系元组演算
关系元组演算的基本形式:
{
t
∣
P
(
t
)
}
\{t\mid P(t)\}
{t∣P(t)},其中元组变量
t
∈
R
t\in R
t∈R。
公式
P
(
t
)
P(t)
P(t)可由如下规则递归定义:
- 每个原子公式是公式:
- s ∈ R s\in R s∈R;
- s [ A ] θ c s[A]\mathop{\theta}c s[A]θc;
- s [ A ] θ u [ B ] s[A]\mathop{\theta}u[B] s[A]θu[B];
- 若 P P P是公式,则 ¬ P \lnot P ¬P也是公式;
- 若 P 1 P_1 P1和 P 2 P_2 P2是公式,则 P 1 ∧ P 2 P_1\land P_2 P1∧P2和 P 1 ∨ P 2 P_1\lor P_2 P1∨P2也是公式;
- 若 P ( t ) P(t) P(t)是公式,则 ∃ t ( P ( t ) ) \exists t(P(t)) ∃t(P(t))和 ∀ t ( P ( t ) ) \forall t(P(t)) ∀t(P(t))也是公式;
- 运算符优先级由高到低依次为: ( ) () ()、 ∃ \exists ∃、 ∀ \forall ∀、 ¬ \lnot ¬、 ∧ \land ∧、 ∨ \lor ∨。
下面以 S t u d e n t s ( S n o , S n a m e , S s e x , S a g e , S d e p t ) Students(Sno, Sname, Ssex, Sage, Sdept) Students(Sno,Sname,Ssex,Sage,Sdept)、 C o u r s e s ( C n o , C n a m e , T n a m e ) Courses(Cno, Cname,Tname) Courses(Cno,Cname,Tname)和 S C ( S n o , C n o , S c o r e ) SC(Sno,Cno,Score) SC(Sno,Cno,Score)为例:
例1:查询计算机系的全体学生。
解:
{
t
∣
t
∈
S
t
u
d
e
n
t
s
∧
t
[
S
d
e
p
t
]
=
计算机
}
\{t\mid t\in Students \land {\color{red}t[Sdept]=\text{计算机}}\}
{t∣t∈Students∧t[Sdept]=计算机}。
例2:查询年龄不是最小的全体学生的姓名。
分析:对任意满足要求的学生
t
t
t,总是存在一个学生
u
u
u,使得
t
[
a
g
e
]
>
u
[
a
g
e
]
t[age]>u[age]
t[age]>u[age]。
解:
{
t
[
S
n
a
m
e
]
∣
t
∈
S
t
u
d
e
n
t
s
∧
∃
(
u
∈
S
t
u
d
e
n
t
s
)
(
t
[
S
a
g
e
]
>
u
[
S
a
g
e
]
)
}
\{t[Sname]\mid t\in Students \land {\color{red}\exists(u\in Students)(t[Sage]>u[Sage])}\}
{t[Sname]∣t∈Students∧∃(u∈Students)(t[Sage]>u[Sage])}。
例3:查询课程都及格的全体学生。
分析:对任意满足要求的学生
t
t
t,他的任一课程
u
u
u都有
u
[
S
c
o
r
e
]
≥
60
u[Score]\geq60
u[Score]≥60。
解:
{
t
∣
t
∈
S
t
u
d
e
n
t
s
∧
∀
(
u
∈
S
C
∧
u
[
S
n
o
]
=
t
[
S
n
o
]
)
(
u
[
S
c
o
r
e
]
≥
60
)
}
\{t\mid t\in Students \land {\color{red}\forall(u\in SC\land u[Sno]=t[Sno])(u[Score]\geq60)}\}
{t∣t∈Students∧∀(u∈SC∧u[Sno]=t[Sno])(u[Score]≥60)}。
注:
{
t
∣
t
∈
S
t
u
d
e
n
t
s
∧
∀
(
u
∈
S
C
)
(
u
[
S
n
o
]
=
t
[
S
n
o
]
∧
u
[
S
c
o
r
e
]
≥
60
)
}
\{t\mid t\in Students \land \forall({\color{red}u\in SC})({\color{red}u[Sno]=t[Sno]\land} u[Score]\geq60)\}
{t∣t∈Students∧∀(u∈SC)(u[Sno]=t[Sno]∧u[Score]≥60)}是不对的。我们希望检查当前学生的选课元组,而不是所有学生的选课记录元组。
例4:使用关系代数和关系元组演算查询学过李明老师讲授的所有课程的学生姓名。
解:
- Π S n a m e , C n o ( S t u d e n t s ⋈ S C ⋈ C o u r s e s ) ÷ Π C n o ( σ T n a m e = 李明 ( C o u r s e s ) ) \Pi_{Sname,Cno}(Students\Join SC\Join Courses)\div\Pi_{Cno}(\sigma_{Tname=\text{李明}}(Courses)) ΠSname,Cno(Students⋈SC⋈Courses)÷ΠCno(σTname=李明(Courses));
- { t [ S n a m e ] ∣ t ∈ S t u d e n t s ∧ ∀ ( u ∈ C o u r s e s ∧ u [ T n a m e ] = 李明 ) ( ∃ ( v ∈ S C ) ( v [ S n o ] = t [ S n o ] ∧ v [ C n o ] = u [ C n o ] ) ) } \{t[Sname]\mid t\in Students \land \forall(u\in Courses\land u[Tname]=李明)(\exists(v\in SC)(v[Sno]=t[Sno]\land v[Cno]=u[Cno]))\} {t[Sname]∣t∈Students∧∀(u∈Courses∧u[Tname]=李明)(∃(v∈SC)(v[Sno]=t[Sno]∧v[Cno]=u[Cno]))}。
关系代数分析:
- 先查询李明老师讲授的所有课程的编号: Π C n o ( σ T n a m e = 李明 ( C o u r s e s ) ) \Pi_{Cno}(\sigma_{Tname=\text{李明}}(Courses)) ΠCno(σTname=李明(Courses));
- 将所有学生的选课情况合并起来,并留下姓名和课程编号: Π S n a m e , C n o ( S t u d e n t s ⋈ S C ⋈ C o u r s e s ) \Pi_{Sname,Cno}(Students\Join SC\Join Courses) ΠSname,Cno(Students⋈SC⋈Courses);
- 使用除法查询学过李明老师讲授的所有课程的同学的姓名: Π S n a m e , C n o ( S t u d e n t s ⋈ S C ⋈ C o u r s e s ) ÷ Π C n o ( σ T n a m e = 李明 ( C o u r s e s ) ) \Pi_{Sname,Cno}(Students\Join SC\Join Courses)\div\Pi_{Cno}(\sigma_{Tname=\text{李明}}(Courses)) ΠSname,Cno(Students⋈SC⋈Courses)÷ΠCno(σTname=李明(Courses))。
关系元组演算分析:对于任意满足条件的学生 t t t,总满足条件:对于任意李明讲授的课程 u u u,总存在选课记录 v v v,使得 v [ S n o ] = t [ S n o ] v[Sno]=t[Sno] v[Sno]=t[Sno]和 v [ C n o ] = u [ C n o ] v[Cno]=u[Cno] v[Cno]=u[Cno]成立。
QBE语言
QBE语言是基于屏幕表格的查询语言,不用书写复杂的查询公式,仅需将条件填写在表格中即可查询。
QBE操作框架由关系名、属性名、操作命令和查询条件构成。
QBE操作命令:
- P.:显示输出操作
- D.:删除操作
- I.:插入操作
- U.:更新操作
下面以
S
t
u
d
e
n
t
s
(
S
n
o
,
S
n
a
m
e
,
S
s
e
x
,
S
a
g
e
,
S
d
e
p
t
)
Students(Sno, Sname, Ssex, Sage, Sdept)
Students(Sno,Sname,Ssex,Sage,Sdept)为例:
例1:向
S
t
u
d
e
n
t
s
Students
Students中插入元组
⟨
10001
,
刘一
,
男
,
18
,
数学
⟩
\langle 10001, \text{刘一}, \text{男}, 18, \text{数学}\rangle
⟨10001,刘一,男,18,数学⟩和
⟨
10002
,
陈二
,
男
,
20
,
计算机
⟩
\langle 10002, \text{陈二}, \text{男}, 20, \text{计算机}\rangle
⟨10002,陈二,男,20,计算机⟩。
解: