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)^**
-
找出大学里的最高工资。
// 找到非最高工资
instructor.salary(
instructor.salary<d.salary ( instrutor ×
d(instructor)))
[SQL]
select instrutor.salary
from instructor, instructor d
where instructor.salary < d.salary;
// 全部人的工资 - 非最高工资 = 最高工资
salary(instructor) -
instructor.salary(
instructor.salary<d.salary ( instrutor ×
d(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 = R∪S(σr.A1=s.A1∧r.A2=s.A2∧...∧r.An=s.An(r×s))
- 找出所有老师的姓名,连同他们教的所有课程的 course_id
name,course_id(instructor⋈teaches)
③ 赋值运算
r⋈s 等同于:
temp1 R × S
temp2
r.A1=s.A1∧r.A2=s.A2∧...∧r.An=s.An(temp1)
result = R∪S(temp2)
④ 外连接运算
- 左连接:⟕
- 右连接:⟖
- 全外连接:⟗
(3)扩展的关系代数运算
① 广义投影
允许在投影列表中使用算术运算和字符串函数等来对投影进行扩展。
- 获得老师的信息及每个月的工资
ID,name,dept_name,salary/12(instructor)
② 聚集
表示对值的集合使用聚集函数(sum、avg、count、min、max)。
- 找出所有教师的工资总和。
Gsum(salary)(instructor)
- 找出在 2010 年春季学期教课的教师数
Gcount_distinct(ID) ( semester='Spring'∧year=2010 (instructor))
- 求出每个系的平均工资
dept_nameGavg(salary)(instructor)
2、元组关系验算
关系代数表达式提供了产生查询结果的过程序列,这个序列能生成查询的答案。与之相比,元组关系盐酸是非过程化的查询语言。它只描述所需信息,而不给出获得该信息的具体过程。
元组关系演算中的查询表达式为:
{ t| P( t ) }
使所有谓词(条件)为真的元组 t 的集合。
符号表示:
-
逻辑运算与(∧)、或(∨)、非(﹁)
-
存在:
-
所有:
-
蕴含:
- 找出所有工资在 80000 美元以上的教师的 ID、name、dept_name 和 salary。
{ t | t instructor ∧ t[salary] > 80000 }

网友评论