数据库

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

2021-05-30  本文已影响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)

σ dept_name = 'Physics' (instructor)

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

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

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

σ dept_name = 'Physics' ∧ salary > 90000 (instructor)

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

② 投影运算

    投影运算可以选择返回要返回的属性(相等于 SQL 中的 select)。
Π 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 年秋季学期的课程
Π 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);

⑤ 集合差运算

Π 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)附加的关系代数运算

① 集合交运算

Π 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))

       \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)

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

    dept_nameGavg(salary)(instructor)

2、元组关系验算

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

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

    { t| P( t ) }

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

符号表示:

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

上一篇 下一篇

猜你喜欢

热点阅读