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 six basic operations of the algebra are the selection, the projection, the cartesian product, the set union, the set difference, and the rename.
The selection is a unary operation that is written as σa=b(R) or σa=v(R) where a and b are attribute names, v is a value constant and R is a relation. The first selection selects all those tupels in R that have the same value in the a and the b attribute. The second selection all those tuples in R that have the value v in the a attribute. More formally:
The projection is a unary operation that is written as πa1,..,an(R) where a1,..,an 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 {a1,..,an}. More formally:
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:
The set union is a binary operation that is written as R ∪ S and is defined as the usual set union in set theory:
The set difference is a binary operation that is written as R - S and is defined as the usual set difference in set theory:
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:
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.
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 is a binary operation that is written as R ∩ S and is defined as the usual set intersection in set theory:
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:
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:
The simulation of the division with the basic operations is as follows. We assume that a1,...,an are the attribute names unique to R and b1,...,bm 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:
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.The Projection
where f|A is defined as the restriction of the function f to the set A, i.e., f|A = { (x, y) | (x, y) ∈ f, x ∈ A }.The Cartesian Product
To ensure that the result of the cartesian product is again a relation it is required that the headers of R and S are disjoint, i.e., do not contain the same attribute.The Set Union
The result of the set union is only defined when the two relations have the same headers.The Set Difference
The result of the set difference is only defined when the two relations have the same headers.The Rename
where t[a/b] is defined as the tuple t with the b attribute renamed to a, i.e., t[a/b] = { (c, v) | (c, v) ∈ t, c ≠ b } ∪ { (a, t(b)) }.
The result of the rename is only defined when the attribute a did not appear already in the header of the operand.The Expressive Power
Other Operations expressible with the Basic Operations
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:The Natural Join
where fun(r) is a predicate that is true for a binary relation r iff r is a functional binary relation. This operation can be regarded as a generalization of the previously defined cartesian product since it is the special case where R and S have no common attributes.
The simulation of the natural join with the basic operations is as follows. Assume that a1,...,an are the attribute names unique to R, b1,...,bm are the attribute names common to R and S and c1,...,cm are the attribute unique to S. Furthermore assume that the attribute names d1,...,dm are neither in R nor in S. In a first step we can now rename the common attribute names in S:
Then we take the cartesion product and select the tuples that are to be joined:
Finally we take a projection to get rid of the renamed attributes:
The Division
where a1,...,an is the set of attribute names unique to R. It is usually required that the attribute names in the header of S are a subset of those of R because otherwise the result of the operation will always be empty.
In the next step we subtract R from this relation:
Note that in U we have the combinations that "should have been in R but weren't". So if we now take the projection on the attribute names unique to R then we have the restrictions of the tuples in R for which not all combinations with tuples in S were present in R:
So what remains to be done is take the projection of R on its unique attribute names and subtract those in V:
Generalized Operations
The General Selection
The θ-join
Operations for null values
Algebraic properties