In
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 (computer science), semantics. The theory was introduced by Edgar F. Codd.
The main applica ...
, a projection is a
unary operation written as
, where
is a
relation and
are attribute names. Its result is defined as the
set obtained when the components of the
tuples in
are restricted to the set
– 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 onto attribute list (age) yields – 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, then
is like
, but with all
-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
Set theory is the branch of mathematical logic that studies Set (mathematics), sets, which can be informally described as collections of objects. Although objects of any kind can be collected into a set, set theory – as a branch of mathema ...
(see:
projection (set theory)) differs from that of
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 (computer science), semantics. The theory was introduced by Edgar F. Codd.
The main applica ...
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 of
existential quantification
Existentialism is a family of philosophy, philosophical views and inquiry that explore the human individual's struggle to lead an Authenticity (philosophy), authentic life despite the apparent Absurdity#The Absurd, absurdity or incomprehensibili ...
in
predicate logic
First-order logic, also called predicate logic, predicate calculus, or quantificational logic, is a collection of formal systems used in mathematics, philosophy, linguistics, and computer science. First-order logic uses quantified variables ove ...
. 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
A computer language is a formal language used to communicate with a computer. Types of computer languages include:
* Software construction#Construction languages, Construction language – all forms of communication by which a human can Comput ...
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
An alphabet is a standard set of letter (alphabet), letters written to represent particular sounds in a spoken language. Specifically, letters largely correspond to phonemes as the smallest sound segments that can distinguish one word from a ...
.
When implemented in
SQL standard the "default projection" returns a
multiset instead of a set, and the 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 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.
Formal definition
More formally the semantics of projection are defined as follows:
:
where