Projection (relational algebra)
From Wikipedia, the free encyclopedia
This article or section needs to be wikified to meet Wikipedia's quality standards. Please help improve this article with relevant internal links. (August 2007) |
[edit] Introduction
In relational algebra, a projection is a unary operation written as where a1,...,an 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 {a1,...,an}.
Projection is relational algebra's counterpart of existential quantification in predicate logic. The attributes not included correspond to existentially quantified variables in the predicate whose extension the operand relation represents. The example below illustrates this point.
Because of the correspondence with existential quantification, some authorities prefer to define projection in terms of the excluded attributes. In a computer language it is of course possible to provide notations for both, and that was done in ISBL and several languages that have taken their cue from ISBL.
A nearly identical concept occurs in the category of monoids, called a string projection, which consists of removing all of the letters in the string that do not belong to a given alphabet.
[edit] Explanation
For an example, consider the relations depicted in the following two tables which are the relation Person and its projection on (some say, over) the attributes Age and Weight:
Person | πAge,Weight(Person) | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Suppose the predicate of Person is "Name is age years old and weighs weight." Then the given projection represents the predicate, "There exists Name such that Name is age years old and weighs weight."
Note that Harry and Peter have the same age and weight, but since the result is a relation, and therefore a set, this combination only appears once in the result.
More formally the semantics of projection are defined as follows:
where t[a1,...,an] is the restriction of the tuple t to the set {a1,...,an} so that
The result of a projection is defined only if {a1,...,an} is a subset of the header of R.
It is interesting to note that projection over no attributes at all is possible, yielding a relation of degree zero. In this case the cardinality of the result is zero if the operand is empty, otherwise one. The two relations of degree zero are the only ones that cannot be depicted as tables.