Relational Algebra I
Relational Algebra I
Relational Algebra I
Relational Algebra
• Procedural language
Select Operation
• Notation: σP (r)
Defined as
σP (r) := {t | t ∈ r and P (t)}
where
– r is a relation (name),
– P is a formula in propositional calculus, composed of
conditions of the form
<attribute> = <attribute> or <constant>
Instead of “=” any other comparison predicate is allowed
(6=, <, > etc).
Conditions can be composed through ∧ (and), ∨ (or), ¬ (not)
A B C D
α α 1 7
α β 5 7
β β 12 3
β β 23 10
σA=B∧D>5(r)
A B C D
α α 1 7
β β 23 10
Project Operation
r πA,C (r)
A B C A C
α 10 2 α 2
α 20 2 β 2
β 30 2 β 4
β 40 4
Cartesian Product
r s
A B C D E
α 1 α 10 +
β 2 β 10 +
β 20 −
γ 10 −
r×s
A B C D E
α 1 α 10 +
α 1 β 10 +
α 1 β 20 −
α 1 γ 10 −
β 2 α 10 +
β 2 β 10 +
β 2 β 20 −
β 2 γ 10 −
Union Operator
• For r ∪ s to be applicable,
1. r, s must have the same number of attributes
2. Attribute domains must be compatible (e.g., 3rd column
of r has a data type matching the data type of the 3rd
column of s)
r∪s
A B
α 1
α 2
β 1
β 3
• For r − s to be applicable,
1. r and s must have the same arity
2. Attribute domains must be compatible
r s
A B A B
α 1 α 2
α 2 β 3
β 1
r−s
A B
α 1
β 1
Rename Operation
• Example:
ρx(E)
returns the relational algebra expression E under the name x
If a relational algebra expression E (which is a relation) has
the arity k, then
ρx(A1,A2,...,Ak )(E)
returns the expression E under the name x, and with the
attribute names A1, A2, . . . , Ak .
Composition of Operations
• Example: σA=C (r × s)
r×s
A B C D E
α 1 α 10 +
α 1 β 10 +
α 1 β 20 −
α 1 γ 10 −
β 2 α 10 +
β 2 β 10 +
β 2 β 20 −
β 2 γ 10 −
σA=C (r × s)
A B C D E
α 1 α 10 +
β 2 β 10 +
β 2 β 20 −