Unit-2 Relational Algebra & Calculus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Relational database systems are expected to be equipped with a query

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.

The fundamental operations of relational algebra are as follows −

 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
We will discuss all these operations in the following sections.

Select Operation (σ)


It selects tuples that satisfy the given predicate from a relation.

Notation − σp(r)

Where σ stands for selection predicate and r stands for relation. p is


prepositional logic formula which may use connectors like and, or, and not.
These terms may use relational operators like − =, ≠, ≥, <, >, ≤.

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.

σsubject = "database" and price = "450" or year > "2010"(Books)


Output − Selects tuples from books where subject is 'database' and 'price'
is 450 or those books published after 2010.

Projection Operation (∏)


It projects column(s) that satisfy a given predicate.

Notation − ∏A1, A2, An (r)

Where A1, A2, An are attribute names of relation r.

Duplicate rows are automatically eliminated, as relation is a set.

For example −

∏subject, author (Books)


Selects and projects columns named as subject and author from the relation
Books.

Union Operation (∪)


It performs binary union between two given relations and is defined as −

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).

For a union operation to be valid, the following conditions must hold −

 r, and s must have the same number of attributes.


 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book
or an article or both.

Set Difference (−)


The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.

Notation − r − s

Finds all the tuples that are present in r but not in s.

∏ author (Books) − ∏ author (Articles)


Output − Provides the name of authors who have written books but not
articles.

Cartesian product (Χ)


Combines information of two different relations into one.

Notation − r Χ s

Where r and s are relations and their output will be defined as −

r Χ s = { q t | q ∈ r and t ∈ s}

σauthor = 'tutorials'(Books Χ Articles)


Output − Yields a relation, which shows all the books and articles written
by tutorialspoint.

Rename Operation (ρ)


The results of relational algebra are also relations but without any name.
The rename operation allows us to rename the output relation. 'rename'
operation is denoted with small Greek letter rho ρ.

Notation − ρ x (E)

Where the result of expression E is saved with name of x.


Additional operations are −

 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.

Relational calculus exists in two forms −

Tuple Relational Calculus (TRC)


Filtering variable ranges over tuples

Notation − {T | Condition}

Returns all tuples T that satisfies a condition.

For example −

{T.name | Author(T) AND T.article = 'database' }


Output − Returns tuples with 'name' from Author who has written article on
'database'.

TRC can be quantified. We can use Existential (∃) and Universal Quantifiers
(∀).

For example −

{R| ∃T ∈Authors(T.article='database' AND R.name=T.name)}


Output − The above query will yield the same result as the previous one.

Domain Relational Calculus (DRC)


In DRC, the filtering variable uses the domain of attributes instead of entire
tuple values (as done in TRC, mentioned above).

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 −

{< article, page, subject > | ∈ Tutorials ∧ subject = 'database'}


Output − Yields Article, Page, and Subject from the relation Tutorials,
where subject is database.

Just like TRC, DRC can also be written using existential and universal
quantifiers. DRC also involves relational operators.

The expression power of Tuple Relation Calculus and Domain Relation


Calculus is equivalent to Relational Algebra.

RC Notation – Non Procedural Expression Language


RC is a non-procedural expression language that does not require any
sequence to be followed in the expression of query transactions. One
declarative statement could be used express a query transaction.
The calculus query syntax is:
{t | COND(t)}
Where t is the tuple variable.
COND(t) is the conditional expression.
The AND, OR, and NOT logical operators are also used in the expressions. An
example of a RC expression is:
{t | STUDENT(t) AND t. GPA > 3.0}
This query would return all students whose GPA is greater than 3.0.
Expressions and Formulas in RC are in the form:
{t1.Aj, t2.Ak,…,tn.Am | COND(t1, t2, …, tn, tn+1, tn+2, …, tn+m)}
Where COND is the condition or formula.
Existential and Universal Quantifiers, note that in RC quantifies that are
largely unique to RC can appear in fomulas. The quantifiers include:
Universal quantifier (∀): Is true if all tuples make the formula true.
Existential quantifier (∃): Is true if there exists some tuple that makes the
formula true
Example would include:
List the first name, last name and address of Students whose Major is
Computer Science.
Q1: {s.Fname, s.Lname, s.Address | STUDENTS(s) AND (∃ m) (MAJOR(m)
AND m.major=’Computer Science’ AND m.studentnumb=t.studentnumb)}
3. SQL Notation:
SQL is a structured query language, a comprehensive database language
with data definition, queries, and updates.[1] It is with SQL that RA and RC
are implemented physically in a database management program. Therefore
this is the major difference between RA, RC and SQL, in that SQL will take the
conceptual expressions of RA and RC and implement them at the computer
level or physical level. SQL includes many declarative statements that it
would take another article to write all of them but below are some few
included here:
Create: used to create schema and tables
Insert: used to insert data to the tables
Select: used to select tuples that satisfy a condition, also used to select
attributes.
Select fname From STUDENTS Where major = ‘computer science’:
selects first name of students from the students table whose major is
computer science.
It is noted that RC is much easier to implement in SQL as the RC notation is
similar to SQL syntax.
Difference between RA & RC:
1. Relational algebra manipulate relations and provide expressions in the
form of queries whereas relational calculus are formed queries on the basis
of pairs of expressions.
2. RA have operator like join, union, intersection, division, difference,
projection, selection etc. whereas RC has tuples and domain oriented
expressions.
3. RA is procedural language whereas RC is non procedural query system.
4. Expressive power of RA and RC are equivalent. This means any query that
could be expressed in RA could be expressed by formula in RC.
5. Any Relational Calculus (RC) formula is translated in Algebric query.
6. Modification in queries is easy in RA than RC.
7. RA formed the mathematical form and have no specific query language,
RC also has mathematical form but has one query language QUEL.
8. Relational algebra is easy to manipulate and understand than RC.
9. RA queries are more powerful than the RC.
10. RC are formed WFFs whereas RA does not form any formula.

You might also like