Relational algebra
In database theory, relational algebra is a theory that uses algebraic structures for modeling data and defining queries on it with well founded semantics. The theory was introduced by Edgar F. Codd.
The main application of relational algebra is to provide a theoretical foundation for relational databases, particularly query languages for such databases, chief among which is SQL. Relational databases store tabular data represented as relations. Queries over relational databases often likewise return tabular data represented as relations.
The main purpose of relational algebra is to define operators that transform one or more input relations to an output relation. Given that these operators accept relations as input and produce relations as output, they can be combined and used to express complex queries that transform multiple input relations into a single output relation.
Unary operators accept a single relation as input. Examples include operators to filter certain attributes or tuples from an input relation. Binary operators accept two relations as input and combine them into a single output relation. For example, taking all tuples found in either relation, removing tuples from the first relation found in the second relation, extending the tuples of the first relation with tuples in the second relation matching certain conditions, and so forth.
Introduction
Relational algebra received little attention outside of pure mathematics until the publication of E.F. Codd's relational model of data in 1970. Codd proposed such an algebra as a basis for database query languages.Relational algebra operates on homogeneous sets of tuples where we commonly interpret m to be the number of rows of tuples in a table and n to be the number of columns. All entries in each column have the same type.
A relation also has a unique tuple called the header which gives each column a unique name or attribute inside the relation. Attributes are used in projections and selections.
Set operators
The relational algebra uses set union, set difference, and Cartesian product from set theory, and adds additional constraints to these operators to create new ones.For set union and set difference, the two relations involved must be union-compatible—that is, the two relations must have the same set of attributes. Because set intersection is defined in terms of set union and set difference, the two relations involved in set intersection must also be union-compatible.
For the Cartesian product to be defined, the two relations involved must have disjoint headers—that is, they must not have a common attribute name.
In addition, the Cartesian product is defined differently from the one in set theory in the sense that tuples are considered to be "shallow" for the purposes of the operation. That is, the Cartesian product of a set of n-tuples with a set of m-tuples yields a set of "flattened" -tuples. More formally, R × S is defined as follows:
The cardinality of the Cartesian product is the product of the cardinalities of its factors, that is, |R × S| = |R| × |S|.
Projection
A projection is a unary operation written as where is a set of attribute names. The result of such projection is defined as the set that is obtained when all tuples in R are restricted to the set.Note: when implemented in SQL standard the "default projection" returns a multiset instead of a set, and the projection to eliminate duplicate data is obtained by the addition of the
DISTINCT keyword.Selection
A generalized selection is a unary operation written as where is a propositional formula that consists of atoms as allowed in the normal selection and the logical operators , and . This selection selects all those tuples in R for which holds.To obtain a listing of all friends or business associates in an address book, the selection might be written as. The result would be a relation containing every attribute of every unique record where is true or where is true.
Rename
A rename is a unary operation written as where the result is identical to R except that the b attribute in all tuples is renamed to an a attribute. This is commonly used to rename the attribute of a relation for the purpose of a join.To rename the "isFriend" attribute to "isBusinessContact" in a relation, might be used.
There is also the notation, where R is renamed to x and the attributes are renamed to.
Joins and join-like operators
Common extensions
In practice the classical relational algebra described above is extended with various operations such as outer joins, aggregate functions and even transitive closure.Outer joins
Whereas the result of a join consists of tuples formed by combining matching tuples in the two operands, an outer join contains those tuples and additionally some tuples formed by extending an unmatched tuple in one of the operands by "fill" values for each of the attributes of the other operand. Outer joins are not considered part of the classical relational algebra discussed so far.The operators defined in this section assume the existence of a null value, ω, which we do not define, to be used for the fill values; in practice this corresponds to the NULL in SQL. In order to make subsequent selection operations on the resulting table meaningful, a semantic meaning needs to be assigned to nulls; in Codd's approach the propositional logic used by the selection is extended to a three-valued logic, although we elide those details in this article.
Three outer join operators are defined: left outer join, right outer join, and full outer join.
Left outer join
The left outer join is written as R ⟕ S where R and S are relations. The result of the left outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in R that have no matching tuples in S.For an example consider the tables Employee and Dept and their left outer join:
In the resulting relation, tuples in S which have no common values in common attribute names with tuples in R take a null value, ω.
Since there are no tuples in Dept with a DeptName of Finance or Executive, ωs occur in the resulting relation where tuples in Employee have a DeptName of Finance or Executive.
Let r1, r2,..., rn be the attributes of the relation R and let be the singleton
relation on the attributes that are unique to the relation S. Then the left outer join can be described in terms of the natural join as follows:
Right outer join
The right outer join behaves almost identically to the left outer join, but the roles of the tables are switched.The right outer join of relations R and S is written as R ⟖ S. The result of the right outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R.
For example, consider the tables Employee and Dept and their right outer join:
In the resulting relation, tuples in R which have no common values in common attribute names with tuples in S take a null value, ω.
Since there are no tuples in Employee with a DeptName of Production, ωs occur in the Name and EmpId attributes of the resulting relation where tuples in Dept had DeptName of Production.
Let s1, s2,..., sn be the attributes of the relation S and let be the singleton
relation on the attributes that are unique to the relation R. Then, as with the left outer join, the right outer join can be simulated using the natural join as follows:
Full outer join
The outer join or full outer join in effect combines the results of the left and right outer joins.The full outer join is written as R ⟗ S where R and S are relations. The result of the full outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R and tuples in R that have no matching tuples in S in their common attribute names.
For an example consider the tables Employee and Dept and their full outer join:
In the resulting relation, tuples in R which have no common values in common attribute names with tuples in S take a null value, ω. Tuples in S which have no common values in common attribute names with tuples in R also take a null value, ω.
The full outer join can be simulated using the left and right outer joins as follows:
Operations for domain computations
There is nothing in relational algebra introduced so far that would allow computations on the data domains. For example, it is not possible using only the algebra introduced so far to write an expression that would multiply the numbers from two columns, e.g. a unit price with a quantity to obtain a total price. Practical query languages have such facilities, e.g. the SQL SELECT allows arithmetic operations to define new columns in the resultEXTEND keyword. In database theory, this is called extended projection.