Relational Algebra

Relational Query Languages

Query language: language in which a user requests information from the database. They are imperative, functional or declarative. Functional languages use relational algebra, declarative languages use tuple relational calculus as well as domain relational language. These are also known as "pure" languages. They all use the same computing power.

Relational Algebra

Image

A procedural language consisting of a set of operations that take one or two relations as input and produce a new relation as their result.

Operations

Select

The select operation selects tuples that satisfy a given predicate.

Notation: σp(r)\sigma_p (r)

pp is called the selection predicate

Result: a subset of tuples with the same attributes of the relation rr.

We allow comparisons using =,,>,,<,=, \neq, >, \geq, <, \leq

Project

A unary operation that returns its argument relation, with certain attributes left out.

Notation: \PIA1,A2,A3,...,Ak(r)\PI_{A_1, A_2, A_3, ..., A_k} (r), where A1,A2,...,AkA_1, A_2, ..., A_k are attribute names and rr is a relation name.

Join (Cartesian-Product)

The Cartesian-Product operation (denoted by X) allows us to combine information from any two relations.

The join operation allows us to combine a select operation and a Cartesian-Product operation into a single operation.

Consider relations r(R)r (R) and s(S)s (S). Let "theta" (θ\theta) be a predicate on attributes in the schema RR "union" SS. The join operation is defined as follows: rθs=σθ(rxs)r \bowtie_{\theta} s = \sigma_{\theta} (r x s)

Union

The union operation allows us to combine two relations. Exactly like the normal union for sets.

Set-Intersection

The set-intersection operation allows us to find tuples that are in both input relations. Exactly like the normal intersection for sets.

Set Difference

The set-difference operation allows us to find tuples that are in one relation but are not in another. Exactly like the difference of sets.

Assignment Operation

It is also possible to assign parts of an expression to temporary relation variables using the \leftarrow

Rename Operation

The results of relational-algebra expressions do not have a name that we can use to refer to them. The rename operator, ρ\rho is used for that purpose.