关系代数
最后更新于
关系代数(Relational Algebra)与SQL查询语言类似,不过多用于理论,SQL查询语言用于现实的数据库查询。
Three types of operations/operators on relations:
fundamental operations:基本运算
additive operations:由基本运算组合而成的附加运算
extended operations:扩展运算
Fundamental operations 部分
相当于SQL查询语言中的where
Select operation is defined as:
is called the selection predicate, connected by : (and), (or), (not) , op is one of: , , , , ,
相当于SQL查询语言中的select
Notation:
例子1:
例子2:
Use $1, $2, . . . refer to the first attribute, the second attribute, and so on.
例子:
如果表结构为instructor(ID, name, dept_name, salary)
,那么:
即选出所有不是最高工资的工资。
Union operation on relation r and s is defined as:
r, s must have the same arity (same number of attributes)
The attribute domains must be compatible(兼容的)
例子:
Based on “emp-dept” schema, find all employees working as 'CLERK' or having salary higher than 2000, or fulfill both conditions.
Set difference operation on relation r and s is defined as:
r, s must have the same arity
The attribute domains must be compatible
例子:
Based on “emp-dept” schema, find all employees working as 'CLERK' ,but not having salary higher than 2000.
Cartesian-product operation on relation r and s is defined as:
例子1:
Give the relational algebra expression for the operations shown:
例子2:
Based on “emp-dept” schema, find all employees’ name and their departments' name .
returns the result of expression E under the name x:
If a relational-algebra expression E has arity n then:
Additive operations 部分
We define additional operations that do not add any power to the relational algebra, but that simplify common queries. An additional operation can be replaced/re-represented by basic operations.
For relation r and s, set-intersection operation on r and s is defined as:
例子:
Based on “emp-dept” schema, find all employees working as 'CLERK' and having salary higher than 2000.
Natural join is associative(结合律)
Natural join is commutative (交换律)
The theta join operation is defined as:
记不住公式没问题,看例子理解。
例子1:
Find all students who have taken all courses offered in the Biology department.
例子2:
Based on “emp-dept” schema, find the jobs provided by all the departments having employees.
首先,确定目标的是job,条件是deptno
例子3:
找到提供了工资在3000到4000之间所有职位的部门,给出部门名称。
首先确定目标是deptno,条件是job
Outer join is an extension of the join operation that avoids loss of information.
left outer join operation is defined as:
Extended operations部分
Generalized project(广义投影) extends the projection operation by allowing arithmetic functions to be used in the projection list.
Aggregate operation(聚集运算) in relational algebra is defined as:
例子1:
Based on “emp-dept” schema,Find the average salary in each department
例子2:
获得工资比所在部门平均工资高的员工姓名、工资以及所在部门的平均工资。
where are attribute names and is a relation name
Let r and s be relations on schemas R and S respectively, and assuming , natural-join operation is defined as:
where is a predicate on attributes in
The assignment operation () provides a convenient way to express complex queries. 相当于with ... as...
each of are arithmetic expressions involving constants and attributes in the schema of E
is any relational-algebra expression
is a list of attributes on which to group (can be empty)
Each is an aggregate function
Each is an attribute name