The relational algebra is a set of operations that manipulate relations as they are defined in the relational model and as such describes part of the data manipulation aspect of this data model. Because of their algebraic properties these operations are often used in database query optimization as an intermediate representation of a query to which certain rewrite rules can be applied to obtain a more efficient version of the query.
The exact set of operations may differ per definition and also depends on whether the unlabeled relational model (that uses mathematical relations) or the labeled relational model (that used the labeled generalization of mathematical relations) is used. We will assume the labeled case here as this is the most common way the relational model is defined.
The Fundamental Operations
The Selection
The result of the selection is only defined if the attribute names that it mentions are in the header of the relation that it operates upon.
- We need here (and also for all the other operations) an example.
The Projection
The projection is a unary operation that is written as π_{a1,..,an}(R) where a_{1},..,a_{n} is a set of attribute names. The result of such a projection is defined as the set that is obtained when all tuples in R are restricted to the set {a_{1},..,a_{n}}. More formally:
- π_{a1,..,an}(R) = { t|_{{a1,..,an}} : t ∈ R }
The Cartesian Product
The cartesian product is a binary operation that is very similar to the Cartesian product in set theory. It is written as R × S where R and '\'S are relations. The result of the cartesion product is the set of all combinations of tupels in R and S''. More formally:
- R × S = { t ∪ s : t ∈ R, s ∈ S }
The Set Union
The set union is a binary operation that is written as R ∪ S and is defined as the usual set union in set theory:
- R ∪ S = { t : t ∈ R ∨ t ∈ S }
The Set Difference
The result of the set difference is only defined when the two relations have the same headers.The Rename
The rename operation is a unary operation that is written as ρ_{a/b}(R) where a and b are attribute names and R is a relation. The result is identical to R except that the b field in all tupels is renamed to a a field. More formally:
- ρ_{a/b}(R) = { t[a/b] : t ∈ R }
The Expressive Power
These operations are enough to express all queries that can be expressed in tuple calculus and domain calculus which is essentially the same as first-order logic.
- insert a sketch of proof here
Other Operations expressible with the Basic Operations
Next to the six basic operations some other operations are also often included even though they can be expressed by combinations of the basic operations. This is because they either have interesting algebraic properties or because they can be implemented more efficiently than their simulations. These operations are the set intersection, the natural join and the division.
The Set Intersection
The result of the set intersection is only defined when the two relations have the same headers. This operation can be simulated in the basic operations as follows:
- R ∩ S = R - (R - S)
The Natural Join
The natural join is a binary operation that is written as R |×| S where R and S are relations. The result of the cartesion product is the set of all combinations of tupels in R and S that are equal on their common attribute names. More formally:
- R |×| S = { t ∪ s : t ∈ R, s ∈ S, fun(t ∪ s) }
- someting it being as used as the most common operation to link related relations, i.e., relations with relationships between them, see also foreign key
- S' := ρ_{d1/b1}(...ρ_{dm/bm}( S)...)
- T := σ_{b1=d1}(...σ_{bm=dm}(R × S')...)
- U := π_{a1,...,an,b1,...,bm,c1,...,cm}(T)
The Division
The division is a binary operation that is written as R ÷ S. The result consists of the restrictions of tuples in R to the attribute names unique to R, i.e., in the header of R but not in the header of S, for which it holds that all their combinations with tuples in S are present in R. More formally:
- R ÷ S = { t|_{{a1,...,an}} : ∀ s ∈ S ( (t|_{{a1,...,an}} ∪ s) ∈ R) }
The simulation of the division with the basic operations is as follows. We assume that a_{1},...,a_{n} are the attribute names unique to R and b_{1},...,b_{m} are the attribute names of S. In the first step we project R on its unique attribute names and construction all combinations with tuples in S:
- T := π_{a1,...,an}(R) × S
- U := T - R
- V := π_{a1,...,an}(U)
- W := π_{a1,...,an}(R) - V
Generalized Operations
The General Selection
- Allows general propositional formulas and other comparison operators
The θ-join
- Allow other comparison operators
Operations for null values
- Discuss the outer joins here