Projection (relational algebra)

In relational algebra, a projection is a unary operation written as formula_1, where formula_2 is a relation and formula_3 are attribute names. Its result is defined as the set obtained when the components of the tuples in formula_2 are restricted to the set formula_5 – it "discards" (or "excludes") the other attributes.
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 formula_2 has attributes formula_7, formula_8, formula_9, where the values of formula_8 are numbers, then
formula_11
is like formula_2, but with all formula_8-values halved.
Related concepts.
The closely related concept in set theory (see: projection (set theory)) differs from that of relational algebra in that, in set theory, one projects onto ordered components, not onto attributes. For instance, projecting formula_14 onto the second component yields 7.
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.
When implemented in SQL standard the "default projection" returns a multiset instead a set, and the projection is obtained by the addition of the codice_1 keyword to eliminate duplicate data.
Example.
For an example, consider the relations depicted in the following two tables which are the relation and its projection on (some say "over") the attributes and :
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 formula_16 is the restriction of the tuple formula_17 to the set formula_5 so that
where formula_20 is an attribute value, formula_21 is an attribute name, and formula_22 is an element of that attribute's domain — see Relation (database).
The result of a projection formula_1 is defined only if formula_5 is a subset of the header of formula_2.
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.