Inrelational algebra, aprojection is aunary operation written as, where is arelation and are attribute names. Its result is defined as theset obtained when the components of thetuples in are restricted to the set – itdiscards (orexcludes) the other attributes.[1]
In practical terms, if a relation is thought of as a table, then projection can be thought of as picking a subset of its columns. For example, if the attributes are (name, age), then projection of the relation {(Alice, 5), (Bob, 8)} onto attribute list (age) yields {5,8} – we have discarded the names, and only know what ages are present.
Projections may also modify attribute values. For example, if has attributes,,, where the values of are numbers, thenis like, but with all-values halved.[2]
The closely related concept inset theory (see:projection (set theory)) differs from that ofrelational algebra in that, in set theory, one projects onto ordered components, not onto attributes. For instance, projecting onto the second component yields 7.
Projection is relational algebra's counterpart ofexistential quantification inpredicate logic. The attributesnot included correspond to existentially quantified variables in the predicate whoseextension 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 acomputer language it is of course possible to provide notations for both, and that was done inISBL and several languages that have taken their cue from ISBL.
A nearly identical concept occurs in the category ofmonoids, called astring projection, which consists of removing all of the letters in thestring that do not belong to a givenalphabet.
When implemented inSQL standard the "default projection" returns amultiset instead of a set, and theπ projection is obtained by the addition of theDISTINCT keyword to eliminate duplicate data.
For an example, consider the relations depicted in the following two tables which are the relationPerson and its projection on (some say "over") the attributesAge andWeight:
|
|
Suppose the predicate of Person is "Name isage years old and weighsweight." Then the given projection represents the predicate, "There existsName such thatName isage years old and weighsweight."
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 is therestriction of the tuple to the set so that
where is an attribute value, is an attribute name, and is an element of that attribute's domain — seeRelation (database).
The result of a projection is defined only if is asubset of theheader of.
Projection over no attributes at all is possible, yielding arelation of degree zero. In this case thecardinality 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.