Unit-2 Relational Algebra & Calculus
Unit-2 Relational Algebra & Calculus
Unit-2 Relational Algebra & Calculus
language that can assist its users to query the database instances. There
are two kinds of query languages − relational algebra and relational
calculus.
1. Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses
operators to perform queries. An operator can be either unary or binary.
They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
Select
Project
Union
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
Notation − σp(r)
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price'
is 450.
For example −
r∪ s = { t | t ∈ r or t ∈ s}
Notion − r U s
Where r and s are either database relations or relation result set (temporary
relation).
Notation − r − s
Notation − r Χ s
r Χ s = { q t | q ∈ r and t ∈ s}
Notation − ρ x (E)
Set intersection
Assignment
Natural join
2. Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
Notation − {T | Condition}
For example −
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers
(∀).
For example −
Notation −
{a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner
attributes.
For example −
Just like TRC, DRC can also be written using existential and universal
quantifiers. DRC also involves relational operators.