returns the result of expression E under the name x:
ρx(E)
If a relational-algebra expression E has arity n then:
ρx(A1,A2,…,An)(E)
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.
Set-Intersection Operation
For relation r and s, set-intersection operation on r and s is defined as:
r∩s={t∣t∈r∧t∈s}=r−(r−s)
例子:
Based on “emp-dept” schema, find all employees working as 'CLERK' and having salary higher than 2000.
select distinct S.name
from student as S
where not exists (
(
select course_id
from course
where dept_name = 'Biology'
)
except
(
select T.course_id
from takes as T
where S.ID = T.ID
)
);
例子2:
Based on “emp-dept” schema, find the jobs provided by all the departments having employees.
首先,确定目标的是job,条件是deptno
Πjob,deptno(emp)÷Πdeptno(dept)
例子3:
找到提供了工资在3000到4000之间所有职位的部门,给出部门名称。
首先确定目标是deptno,条件是job
\Large \Pi_{dname}(dept \bowtie \Pi_{deptno,job}(emp)\div\Pi_{job}(\sigma_{sal\ge 3000 \and sal <4000}(emp)))
Assignment Operation
The assignment operation (←) provides a convenient way to express complex queries. 相当于with ... as...
Outer Join
Outer join is an extension of the join operation that avoids loss of information.
left outer join operation is defined as:
(r⋈s)∪(r−ΠR(r⋈s)×{(null,…,null)})
Extended operations部分
Generalized Projection
Generalized project(广义投影) extends the projection operation by allowing arithmetic functions to be used in the projection list.
ΠF1,F2,…,Fn(E)
each of F1,F2,…,Fn are arithmetic expressions involving constants and attributes in the schema of E
Aggregation functions
Aggregate operation(聚集运算) in relational algebra is defined as:
G1,G2,…,GnGF1(A1),F2(A2),…,Fn(An)(E)
E is any relational-algebra expression
G1,G2,…,Gnis a list of attributes on which to group (can be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
例子1:
Based on “emp-dept” schema,Find the average salary in each department