美文网首页数据库
[数据库之六] 形式化关系查询语言

[数据库之六] 形式化关系查询语言

作者: 小胡_鸭 | 来源:发表于2021-05-30 11:58 被阅读0次

1、关系代数

  关系代数是一种过程化查询语言。它包括一个运算的集合,这些集合以一个或两个关系为输入,产生一个新的关系作为结果。
  关系代数的基本运算有:选择、投影、并、集合差、笛卡儿积更名
  还有一些其他的运算,即集合交、自然连接、赋值

(1)基本运算

  选择、投影、更名运算称为一元运算,因为它们只对一个关系进行运算。
  并、集合差、笛卡儿积称为二元运算,对两个关系进行运算。

使用大学数据库演示关系代数的使用:

【教师】instructor ( id, name, dept_name, salary )
【课程段】section ( course_id, sec_id, semester, year, building, room_number, time_slot_id )
【教师授课安排】teaches ( id, course_id, sec_id, semester, year )

① 选择运算

  选择运算选出满足给定谓词的元组。(相当于 SQL 中的 where)

  • 选择关系 instructor 中属于 Physics 的那些元组。
σ dept_name = 'Physics' (instructor)

[SQL]
select * from instructor where dept_name = 'Physics';
  • 找到工资大于 90000 美元的所有元组。
σ salary > 90000 (instructor)

[SQL]
select * from instructor where salary > 90000;

  选择谓词中,常使用 =、≠、<、≤、>、≥。还可以使用连词 and(∧)、or(∨)、not(﹁)将多个谓词合并为一个较大的谓词。

  • 找到物理系中工资额大于 90000 美元的教师。
σ dept_name = 'Physics' ∧ salary > 90000 (instructor)

[SQL]
select * from instructor where dept_name = 'Physics' and salary > 90000;

② 投影运算

    投影运算可以选择返回要返回的属性(相等于 SQL 中的 select)。
  • 获得教师列表的 ID、name 和 salary。
Π ID, name, salary (instructor)

[SQL]
select ID, name, salary from instructor;

③ 关系运算的组合

  • 找到物理系的所有老师的名字
Π name(σ dept_name = 'Physics' (instructor))

[SQL]
select name from instructor where dept_name = 'Physics';

④ 并运算

  • 找出开设在 2009 年秋季学期或者 2010 年春季学期或者这二者皆开的所有课程的集合。
// 找出开设在 2009 年秋季学期的课程
Π course_id(σ semester = 'Fall' ∧ year = 2009(section))

// 找出开设在 2010 年春季学期的课程
Π course_id(σ semester = 'Spring' ∧ year = 2010(section))

// 最终的表达式
Π course_id(σ semester = 'Fall' ∧ year = 2009(section)) ∪ Π course_id(σ semester = 'Spring' ∧ year = 2010(section))

[SQL]
select course_id 
from section
where semester = 'Fall' and year = 2009;

select course_id
from section
where semester = 'Spring' and year = 2010;

(select course_id 
 from section
 where semester = 'Fall' and year = 2009)
union
(select course_id
from section
where semester = 'Spring' and year = 2010);

⑤ 集合差运算

  • 找出所有开设在 2009 年秋季学期但是不在 2010 年春季开设的课程。
Π course_id(σ semester = 'Fall' ∧ year = 2009(section)) - Π course_id(σ semester = 'Spring' ∧ year = 2010(section))

[SQL]
(select course_id
 from section
 where semester = 'Fall' and year = 2009)
 except
(select course_id
 from section
 where semester = 'Spring' and year = 2010);

⑥ 笛卡儿积

  • 找出物理系中的所有老师以及他们教授的所有课程。
σ dept_name = 'Physics' ∧ instructor.ID = teaches.ID (instructor × teaches)

// 只需要获得教师的名字和课程ID
Π name, course_id(σ dept_name = 'Physics' ∧ instructor.ID = teaches.ID (instructor × teaches))

// 另外一种写法,效率更佳,先筛选元组再做笛卡儿积
Π name, course_id(σ instructor.ID = teaches.ID ((σ dept_name = 'Physics' (instructor)) × teaches)

⑦ 更名运算

    对关系代数表达式 E 更名为 x

                                                                                    **ρ ~x~ ^(E)^**

    返回表达式 E 的结果,并赋予它名字 x,同时将各属性更名为 A1, A2, ..., An

                                                                                    **ρ ~x(A1,A2,...,An)~ ^(E)^**
  • 找出大学里的最高工资。

    // 找到非最高工资

        \large\Piinstructor.salary(\large\sigmainstructor.salary<d.salary ( instrutor × \large\rhod(instructor)))

[SQL]
select instrutor.salary
from instructor, instructor d
where instructor.salary < d.salary;

// 全部人的工资 - 非最高工资 = 最高工资

     \Pi salary(instructor) - \Piinstructor.salary(\sigmainstructor.salary<d.salary ( instrutor × \rhod(instructor)))

[SQL]
(select salary from instructor)
  except
(select instrutor.salary
 from instructor, instructor d
 where instructor.salary < d.salary)


(2)附加的关系代数运算

① 集合交运算

  • 找出在 2009 年秋季和 2010 年春季都开设的课程。
Π course_id(σ semester = 'Fall' ∧ year = 2009(section)) ∩ Π course_id(σ semester = 'Spring' ∧ year = 2010(section))

[SQL]
(select course_id
 from section
 where semester = 'Fall' and year = 2009)
union
(select course_id
 from section
 where semester = 'Spring' and year = 2010);

② 自然连接运算

形式化定义

      r⋈s = \PiR∪S(σr.A1=s.A1∧r.A2=s.A2∧...∧r.An=s.An(r×s))

  • 找出所有老师的姓名,连同他们教的所有课程的 course_id

       \Piname,course_id(instructor⋈teaches)

③ 赋值运算

r⋈s 等同于:

  temp1 \leftarrow R × S

  temp2 \leftarrow \sigmar.A1=s.A1∧r.A2=s.A2∧...∧r.An=s.An(temp1)

  result = \PiR∪S(temp2)

④ 外连接运算

  • 左连接:⟕
  • 右连接:⟖
  • 全外连接:⟗


(3)扩展的关系代数运算

① 广义投影

  允许在投影列表中使用算术运算和字符串函数等来对投影进行扩展。

  • 获得老师的信息及每个月的工资

    \PiID,name,dept_name,salary/12(instructor)

② 聚集

  表示对值的集合使用聚集函数(sum、avg、count、min、max)。

  • 找出所有教师的工资总和。

    Gsum(salary)(instructor)

  • 找出在 2010 年春季学期教课的教师数

    Gcount_distinct(ID) ( \sigmasemester='Spring'∧year=2010 (instructor))

  • 求出每个系的平均工资

    dept_nameGavg(salary)(instructor)

2、元组关系验算

  关系代数表达式提供了产生查询结果的过程序列,这个序列能生成查询的答案。与之相比,元组关系盐酸是非过程化的查询语言。它只描述所需信息,而不给出获得该信息的具体过程。

  元组关系演算中的查询表达式为:

    { t| P( t ) }

  使所有谓词(条件)为真的元组 t 的集合。

符号表示:

  • 逻辑运算与(∧)、或(∨)、非(﹁)

  • 存在:\exist

  • 所有:\forall

  • 蕴含:\implies

  • 找出所有工资在 80000 美元以上的教师的 ID、name、dept_name 和 salary。

    { t | t \in instructor ∧ t[salary] > 80000 }

相关文章

  • [数据库之六] 形式化关系查询语言

    1、关系代数   关系代数是一种过程化查询语言。它包括一个运算的集合,这些集合以一个或两个关系为输入,产生一个新的...

  • Designing Data-Intensive Applica

    数据查询语言 数据库的查询语言——SQL 与关系数据库一起走入人们视野的还有一种新的查询数据的方式, 说明型语言,...

  • 关系数据库SQL之高级数据查询:去重复、组合查询、连接查询、虚拟

    前言 接上一篇关系数据库SQL之基本数据查询:子查询、分组查询、模糊查询,主要是关系型数据库基本数据查询。包括子查...

  • Elastic Search

    类比关系型数据库 DSL(Domain Specific Language特定领域语言)语法 查询表达式 查询格式...

  • Oracle数据库

    主流关系型数据库 关系型数据库存储数据的特点 结构化查询语言: 数据类型: select * from emp; ...

  • MySQL 常用语法总结

    一、SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件...

  • SQL基础应用

    1.什么是SQL?关系型数据库当中通用的查询语言。全名:结构化查询语言。 SQL标准(ANSI/ISO)SQL-8...

  • 数据库

    1. SQL:结构化查询语言的简称, 是关系数据库的标准语言。SQL 是一种通用的、 功能极强的关系数据库...

  • 数据库设计(五)SQL

    SQL(Structured Query Language),结构化查询语言,是现行关系数据库的标准语言,SQL不...

  • 数据库

    二、重要知识点 1. 关系数据库语言分为关系代数、关系演算和结构化查询语言三大类。 2. 关系的 ...

网友评论

    本文标题:[数据库之六] 形式化关系查询语言

    本文链接:https://www.haomeiwen.com/subject/kttbsltx.html