【数据库系统】第5讲 关系演算

第5讲 关系演算

概述

关系演算是以数理逻辑中的谓词演算为基础的。按谓词变量的不同,可分为关系元组演算关系域演算
使用关系演算可以对关系进行查询操作。

关系元组演算

关系元组演算的基本形式: { t ∣ P ( t ) } \{t\mid P(t)\} {tP(t)},其中元组变量 t ∈ R t\in R tR
公式 P ( t ) P(t) P(t)可由如下规则递归定义:

  • 每个原子公式是公式:
    • s ∈ R s\in R sR
    • 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 P1P2 P 1 ∨ P 2 P_1\lor P_2 P1P2也是公式;
  • 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{计算机}}\} {ttStudentst[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]tStudents(uStudents)(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)}\} {ttStudents(uSCu[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)\} {ttStudents(uSC)(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(StudentsSCCourses)÷Π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]tStudents(uCoursesu[Tname]=李明)((vSC)(v[Sno]=t[Sno]v[Cno]=u[Cno]))}

关系代数分析

  1. 先查询李明老师讲授的所有课程的编号: Π C n o ( σ T n a m e = 李明 ( C o u r s e s ) ) \Pi_{Cno}(\sigma_{Tname=\text{李明}}(Courses)) ΠCno(σTname=李明(Courses))
  2. 将所有学生的选课情况合并起来,并留下姓名和课程编号: Π 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(StudentsSCCourses)
  3. 使用除法查询学过李明老师讲授的所有课程的同学的姓名: Π 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(StudentsSCCourses)÷Π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,计算机

  • 22
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值