Projection (relational algebra)

{{Short description|Operation that restricts a relation to a specified set of attributes}}

{{About|relational algebra|other uses of the term "projection"|Projection (disambiguation)}}

In relational algebra, a projection is a unary operation written as \Pi_{a_1, ...,a_n}( R ), where R is a relation and a_1,...,a_n are attribute names. Its result is defined as the set obtained when the components of the tuples in R are restricted to the set \{a_1,...,a_n\} – it discards (or excludes) the other attributes.{{cite web|url=http://www.cs.rochester.edu/~nelson/courses/csc_173/relations/algebra.html|title=Relational Algebra|publisher=cs.rochester.edu|accessdate=2014-07-28}}

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 R has attributes a, b, c, where the values of b are numbers, then

\Pi_{a,\ b \times 0.5,\ c}( R )

is like R, but with all b-values halved.http://www.csee.umbc.edu/~pmundur/courses/CMSC661-02/rel-alg.pdf See Problem 3.8.B on page 3

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 (3,7) 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 of a set, and the {{Pi}} projection is obtained by the addition of the DISTINCT keyword to eliminate duplicate data.

Example

For an example, consider the relations depicted in the following two tables which are the relation {{math|Person}} and its projection on (some say "over") the attributes {{math|Age}} and {{math|Weight}}:

border="0" cellpadding="0" cellspacing="20" align="center"
\text{Person}

! \Pi_\text{Age,Weight}(\text{Person})

style="vertical-align: top"

|

{| align="center" border="0" cellpadding="10" cellspacing="0" style="border-collapse: collapse; border: 1px solid black"

style="background-color: silver; text-align: left"

! style="border: 1px solid black" width="34%" | Name

! style="border: 1px solid black" width="33%" | Age

! style="border: 1px solid black" width="33%" | Weight

style="border: 1px solid black" | Harry

| style="border: 1px solid black" | 34

| style="border: 1px solid black" | 180

style="border: 1px solid black" | Sally

| style="border: 1px solid black" | 28

| style="border: 1px solid black" | 164

style="border: 1px solid black" | George

| style="border: 1px solid black" | 28

| style="border: 1px solid black" | 170

style="border: 1px solid black" | Helena

| style="border: 1px solid black" | 54

| style="border: 1px solid black" | 154

style="border: 1px solid black" | Peter

| style="border: 1px solid black" | 34

| style="border: 1px solid black" | 180

|

align="center" border="0" cellpadding="10" cellspacing="0" style="border-collapse: collapse; border: 1px solid black"
style="background-color: silver; text-align: left"

! style="border: 1px solid black" width="50%" | Age

! style="border: 1px solid black" width="50%" | Weight

style="border: 1px solid black" | 34

| style="border: 1px solid black" | 180

style="border: 1px solid black" | 28

| style="border: 1px solid black" | 164

style="border: 1px solid black" | 28

| style="border: 1px solid black" | 170

style="border: 1px solid black" | 54

| style="border: 1px solid black" | 154

|}

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.

Formal definition

More formally the semantics of projection are defined as follows:

: \Pi_{a_1, ...,a_n}( R ) = \{ \ t[a_1,...,a_n] : \ t \in R \ \},

where t[a_1,...,a_n] is the restriction of the tuple t to the set \{a_1,...,a_n\} so that

: t[a_1,...,a_n] = \{ \ ( a', v ) \ | \ ( a', v ) \in t, \ a' \in \{a_1,...,a_n \} \},

where (a', v) is an attribute value, a' is an attribute name, and v is an element of that attribute's domain — see Relation (database).

The result of a projection \Pi_{a_1, ...,a_n}( R ) is defined only if \{a_1,...,a_n\} is a subset of the header of R.

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.

See also

References

{{reflist}}

{{DEFAULTSORT:Projection (Relational Algebra)}}

Category:Relational algebra

ru:Алгебра Кодда#Проекция