In database theory, relational algebra is a theory that uses algebraic structures for modeling data and defining queries on it with well founded semantics. The theory was introduced by Edgar F. Codd. The main application of relational algebra is to provide a theoretical foundation for relational databases, particularly query languages for such databases, chief among which is SQL. Relational databases store tabular data represented as relations. Queries over relational databases often likewise return tabular data represented as relations. The main purpose of relational algebra is to define operators that transform one or more input relations to an output relation. Given that these operators accept relations as input and produce relations as output, they can be combined and used to express complex queries that transform multiple input relations (whose data are stored in the database) into a single output relation (the query results). Unary operators accept a single relation as input. Examples include operators to filter certain attributes (columns) or tuples (rows) from an input relation. Binary operators accept two relations as input and combine them into a single output relation. For example, taking all tuples found in either relation (union), removing tuples from the first relation found in the second relation (difference), extending the tuples of the first relation with tuples in the second relation matching certain conditions, and so forth.
Introduction Relational algebra received little attention outside of pure mathematics until the publication of E.F. Codd's relational model of data in 1970. Codd proposed such an algebra as a basis for database query languages. A relation of arity n is a set of n‑tuples. Relational algebra operates on homogeneous sets of tuples, S = { ( s j 1 , s j 2 , . . . s j n ) ∣ j ∈ 1... m } , {\displaystyle S=\{(s_{j1},s_{j2},...s_{jn})\mid j\in 1...m\},} where an n‑tuple is a tuple (row; index j) with n 'types of attributes' (or data domains), thus m is the number of rows of tuples in a table and n is the number of columns (and all entries in each column have the same 'type'). A relation also has a unique tuple called the header which gives each column a unique name or attribute inside the relation. Attributes are used in projections and selections.
Set operators
The relational algebra uses set union, set difference, and Cartesian product from set theory, and adds additional constraints to these operators to create new ones. For set union and set difference, the two relations involved must be union-compatible—that is, the two relations must have the same set of attributes. Because set intersection is defined in terms of set union and set difference, the two relations involved in set intersection must also be union-compatible. For the Cartesian product to be defined, the two relations involved must have disjoint headers (i.e. they must not have a common attribute name). In addition, the Cartesian product is defined differently from the one in set theory, in the sense that tuples are considered to be "shallow" for the purposes of the operation. That means the Cartesian product of a set of n-tuples with a set of m-tuples yields a set of "flattened" ( n + m ) {\displaystyle (n+m)} -tuples (whereas basic set theory would have prescribed a set of 2-tuples, each containing an n-tuple and an m-tuple). In relational algebra, the Cartesian product R × S {\displaystyle R\times S} is defined formally as
The cardinality of the Cartesian product is the product of the cardinalities of its factors, that is, |R × S| = |R| × |S|.
Projection
A projection (Π) is a unary operation written as Π a 1 , … , a n ( R ) {\displaystyle \Pi _{a_{1},\ldots ,a_{n}}(R)} where a 1 , … , a n {\displaystyle a_{1},\ldots ,a_{n}} 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 { a 1 , … , a n } {\displaystyle \{a_{1},\ldots ,a_{n}\}} . Note: when implemented in SQL standard the "default projection" returns a multiset instead of a set, and the Π projection to eliminate duplicate data is obtained by the addition of the DISTINCT keyword.
Selection
A generalized selection (σ) is a unary operation written as σ φ ( R ) {\displaystyle \sigma _{\varphi }(R)} where φ is a propositional formula that consists of atoms as allowed in the normal selection and the logical operators ∧ {\displaystyle \wedge } (and), ∨ {\displaystyle \lor } (or) and ¬ {\displaystyle \neg } (negation). This selection selects all those tuples in R for which φ holds. To obtain a listing of all friends or business associates in an address book, the selection might be written as
… excerpt ends here. Continue reading the full article.


