0% found this document useful (0 votes)
28 views16 pages

Relational Algebra and Caluculus

Uploaded by

medhanaidubonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views16 pages

Relational Algebra and Caluculus

Uploaded by

medhanaidubonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INTRODUCTION TO RELATIONAL ALGEBRA & RELATIONAL CALCULUS

Query Language

In simple words, a Language which is used to store and retrieve data from database is known as
query language. For example – SQL

There are two types of query language:


1.Procedural Query language
2.Non-procedural query language

1. Procedural Query language:

In procedural query language, user instructs the system to perform a series of operations to produce
the desired results. Here users tells what data to be retrieved from database and how to retrieve it.

For example – Let’s take a real world example to understand the procedural language, you are asking
your younger brother to make a cup of tea, if you are just telling him to make a tea and not telling
the process then it is a non-procedural language, however if you are telling the step by step process
like switch on the stove, boil the water, add milk etc. then it is a procedural language.

2. Non-procedural query language:

In Non-procedural query language, user instructs the system to produce the desired result without
telling the step by step process. Here users tells what data to be retrieved from database but doesn’t
tell how to retrieve it.

Now let’s back to our main topic of relational algebra and relational calculus.

Relational Algebra:

Relational algebra is a conceptual procedural query language used on relational model.

Relational Calculus:

Relational calculus is a conceptual non-procedural query language used on relational model.

Note:
I have used word conceptual while describing relational algebra and relational calculus, because they
are theoretical mathematical system or query language, they are not the practical implementation,
SQL is a practical implementation of relational algebra and relational calculus.

Relational Algebra, Calculus, RDBMS & SQL:

Relational algebra and calculus are the theoretical concepts used on relational model.

RDBMS is a practical implementation of relational model.

SQL is a practical implementation of relational algebra and calculus.

Relational Algebra in DBMS?

Relational algebra is a procedural query language that works on relational model. The purpose of a
query language is to retrieve data from database or perform various operations such as insert,
update, delete on the data. When I say that relational algebra is a procedural query language, it
means that it tells what data to be retrieved and how to be retrieved.

On the other hand relational calculus is a non-procedural query language, which means it tells what
data to be retrieved but doesn’t tell how to retrieve it. We will discuss relational calculus in a
separate tutorial.

Types of operations in relational algebra

We have divided these operations in two categories:


1. Basic Operations
2. Derived Operations

Basic/Fundamental Operations:

1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)

Derived Operations:

2. Left, Right, Full outer join (⟕, ⟖, ⟗)


1. Natural Join (⋈)

3. Intersection (∩)
4. Division (÷)

Lets discuss these operations one by one with the help of examples.

Select Operator (σ)

Select Operator is denoted by sigma (σ) and it is used to find the tuples (or rows) in a relation (or
table) which satisfy the given condition.

If you understand little bit of SQL then you can think of it as a where clause in SQL, which is used for
the same purpose.

Syntax of Select Operator (σ)

σ Condition/Predicate(Relation/Table name)

Select Operator (σ) Example

Table: CUSTOMER

---------------

Customer_Id Customer_Name Customer_City

----------- ------------- -------------

C10100 Steve Agra

C10111 Raghu Agra

C10115 Chaitanya Noida

C10117 Ajeet Delhi

C10118 Carl Delhi

Query:

σ Customer_City="Agra" (CUSTOMER)

Output:

Customer_Id Customer_Name Customer_City

----------- ------------- -------------

C10100 Steve Agra


C10111 Raghu Agra

Project Operator (∏)

Project operator is denoted by ∏ symbol and it is used to select desired columns (or attributes) from
a table (or relation).

Project operator in relational algebra is similar to the Select statement in SQL.

Syntax of Project Operator (∏)

∏ column_name1, column_name2, ...., column_nameN(table_name)

Project Operator (∏) Example

In this example, we have a table CUSTOMER with three columns, we want to fetch only two columns
of the table, which we can do with the help of Project Operator ∏.

Table: CUSTOMER

Customer_Id Customer_Name Customer_City

----------- ------------- -------------

C10100 Steve Agra

C10111 Raghu Agra

C10115 Chaitanya Noida

C10117 Ajeet Delhi

C10118 Carl Delhi

Query:

∏ Customer_Name, Customer_City (CUSTOMER)

Output:

Customer_Name Customer_City

------------- -------------

Steve Agra

Raghu Agra

Chaitanya Noida

Ajeet Delhi

Carl Delhi

Union Operator (∪)

Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two tables
(relations).
Lets discuss union operator a bit more. Lets say we have two relations R1 and R2 both have same
columns and we want to select all the tuples(rows) from these relations then we can apply the union
operator on these relations.

Note: The rows (tuples) that are present in both the tables will only appear once in the union set. In
short you can say that there are no duplicates present after the union operation.

Syntax of Union Operator (∪)

table_name1 ∪ table_name2

Union Operator (∪) Example

Table 1: COURSE

Course_Id Student_Name Student_Id

--------- ------------ ----------

C101 Aditya S901

C104 Aditya S901

C106 Steve S911

C109 Paul S921

C115 Lucy S931

Table 2: STUDENT

Student_Id Student_Name Student_Age

------------ ---------- -----------

S901 Aditya 19

S911 Steve 18

S921 Paul 19

S931 Lucy 17

S941 Carl 16

S951 Rick 18

Query:

∏ Student_Name (COURSE) ∪ ∏ Student_Name (STUDENT)

Output:

Student_Name

------------

Aditya

Carl
Paul

Lucy

Rick

Steve

Note: As you can see there are no duplicate names present in the output even though we had few
common names in both the tables, also in the COURSE table we had the duplicate name itself.

Intersection Operator (∩)

Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples) from two
tables (relations).

Lets say we have two relations R1 and R2 both have same columns and we want to select all those
tuples(rows) that are present in both the relations, then in that case we can apply intersection
operation on these two relations R1 ∩ R2.

Note: Only those rows that are present in both the tables will appear in the result set.

Syntax of Intersection Operator (∩)

table_name1 ∩ table_name2

Intersection Operator (∩) Example

Lets take the same example that we have taken above.


Table 1: COURSE

Course_Id Student_Name Student_Id

--------- ------------ ----------

C101 Aditya S901

C104 Aditya S901

C106 Steve S911

C109 Paul S921

C115 Lucy S931

Table 2: STUDENT

Student_Id Student_Name Student_Age

------------ ---------- -----------

S901 Aditya 19

S911 Steve 18

S921 Paul 19

S931 Lucy 17

S941 Carl 16
S951 Rick 18

Query:

∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)

Output:

Student_Name

------------

Aditya

Steve

Paul

Lucy

Set Difference (-)

Set Difference is denoted by – symbol. Lets say we have two relations R1 and R2 and we want to
select all those tuples(rows) that are present in Relation R1 but not present in Relation R2, this can
be done using Set difference R1 – R2.

Syntax of Set Difference (-)

table_name1 - table_name2

Set Difference (-) Example

Lets take the same tables COURSE and STUDENT that we have seen above.

Query:
Lets write a query to select those student names that are present in STUDENT table but not present
in COURSE table.

∏ Student_Name (STUDENT) - ∏ Student_Name (COURSE)

Output:

Student_Name

------------

Carl

Rick

Cartesian product (X)

Cartesian Product is denoted by X symbol. Lets say we have two relations R1 and R2 then the
cartesian product of these two relations (R1 X R2) would combine each tuple of first relation R1 with
the each tuple of second relation R2. I know it sounds confusing but once we take an example of this,
you will be able to understand this.

Syntax of Cartesian product (X)


R1 X R2

Cartesian product (X) Example

Table 1: R

Col_A Col_B

----- ------

AA 100

BB 200

CC 300

Table 2: S

Col_X Col_Y

----- -----

XX 99

YY 11

ZZ 101

Query:
Lets find the cartesian product of table R and S.

RXS

Output:

Col_A Col_B Col_X Col_Y

----- ------ ------ ------

AA 100 XX 99

AA 100 YY 11

AA 100 ZZ 101

BB 200 XX 99

BB 200 YY 11

BB 200 ZZ 101

CC 300 XX 99

CC 300 YY 11

CC 300 ZZ 101

Note: The number of rows in the output will always be the cross product of number of rows in each
table. In our example table 1 has 3 rows and table 2 has 3 rows so the output has 3×3 = 9 rows.

Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)

Rename (ρ) Example

Lets say we have a table customer, we are fetching customer names and we are renaming the
resulted relation to CUST_NAMES.

Table: CUSTOMER

Customer_Id Customer_Name Customer_City

----------- ------------- -------------

C10100 Steve Agra

C10111 Raghu Agra

C10115 Chaitanya Noida

C10117 Ajeet Delhi

C10118 Carl Delhi

Query:

ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))

Output:

CUST_NAMES

----------

Steve

Raghu

Chaitanya

Ajeet

Carl

Relational Calculus
Relational calculus is a non-procedural query language that tells the system what data to be retrieved
but doesn’t tell how to retrieve it.

Types of Relational Calculus


1. Tuple Relational Calculus (TRC)

Tuple relational calculus is used for selecting those tuples that satisfy the given condition.
Table: Student

First_Name Last_Name Age

---------- --------- ----

Ajeet Singh 30

Chaitanya Singh 31

Rajeev Bhatia 27

Carl Pratap 28

Lets write relational calculus queries.

Query to display the last name of those students where age is greater than 30

{ t.Last_Name | Student(t) AND t.age > 30 }

In the above query you can see two parts separated by | symbol. The second part is where we define
the condition and in the first part we specify the fields which we want to display for the selected
tuples.

The result of the above query would be:

Last_Name

---------

Singh

Query to display all the details of students where Last name is ‘Singh’

{ t | Student(t) AND t.Last_Name = 'Singh' }

Output:
First_Name Last_Name Age

---------- --------- ----

Ajeet Singh 30

Chaitanya Singh 31

2. Domain Relational Calculus (DRC)

In domain relational calculus the records are filtered based on the domains.
Again we take the same table to understand how DRC works.
Table: Student

First_Name Last_Name Age

---------- --------- ----

Ajeet Singh 30

Chaitanya Singh 31

Rajeev Bhatia 27

Carl Pratap 28

Query to find the first name and age of students where student age is greater than 27

{< First_Name, Age > | ∈ Student ∧ Age > 27}

The symbols used for logical operators are: ∧ for AND, ∨ for OR and ┓ for NOT.
Note:

Output:

First_Name Age

---------- ----

Ajeet 30

Chaitanya 31

Carl 28

Difference between Relational Algebra and Relational Calculus:

Basis of
S.NO Comparison Relational Algebra Relational Calculus

Relational Calculus is a Declarative


Language Type It is a Procedural language.
1. (non-procedural) language.

2. Procedure Relational Algebra means how Relational Calculus means what


Basis of
S.NO Comparison Relational Algebra Relational Calculus

to obtain the result. result we have to obtain.

In Relational Algebra, the


order is specified in which the In Relational Calculus, the order is
Order
operations have to be not specified.
3. performed.

Relation Calculus can be domain-


Relational Algebra is
Domain dependent because of domain
independent of the domain.
4. relational calculus.

Relational Calculus is not nearer to


Programming Relational Algebra is nearer to
programming language but to
language a programming language.
5. natural language.

The SQL includes only some


SQL is based to a greater extent on
Inclusion in SQL features from the relational
the tuple relational calculus.
6. algebra.

Relational Algebra is one of


the languages in which
For a database language to be
queries can be expressed but
Relationally relationally complete, the query
the queries should also be
completeness written in it must be expressible in
expressed in relational
relational calculus.
calculus to be relationally
7. complete.

The evaluation of the query


relies on the order The order of operations does not
Query
specification in which the matter in relational calculus for the
Evaluation
operations must be evaluation of queries.
8. performed.

9. Database For accessing the database, For accessing the database,


access relational algebra provides a relational calculus provides a
solution in terms of what is solution in terms as simple as what is
Basis of
S.NO Comparison Relational Algebra Relational Calculus

required and how to get that


required and lets the system find the
information by following a
solution for that.
step-by-step description.

The completeness of a language is


measured in the manner that it is
The expressiveness of any
least as powerful as calculus. That
given language is judged using
Expressiveness implies relation defined using some
relational algebra operations
expression of the calculus is also
as a standard.
definable by some other expression,
10. the language is in question.

Example Queries on Relational Algebra

Given below are a few examples of a database and a few queries based on that.

(1). Suppose there is a banking database which comprises following tables :

Customer(Cust_name, Cust_street, Cust_city)

Branch(Branch_name, Branch_city, Assets)

Account (Branch_name, Account_number, Balance)

Loan(Branch_name, Loan_number, Amount)

Depositor(Cust_name, Account_number)

Borrower(Cust_name, Loan_number)

Query : Find the names of all the customers who have taken a loan from the bank and also have an
account at the bank.

Solution:

Step 1 : Identify the relations that would be required to frame the resultant query.

First half of the query(i.e. names of customers who have taken loan) indicates “borrowers”
information.

So Relation 1 —–> Borrower.

Second half of the query needs Customer Name and Account number which can be obtained from
Depositor relation.
Hence, Relation 2——> Depositor.

Step 2 : Identify the columns which you require from the relations obtained in Step 1.

Column 1 : Cust_name from Borrower

Column 2 : Cust_name from Depositor

Step 3 : Identify the operator to be used. We need to find out the names of customers who are
present in both Borrower table and Depositor table.

Hence, operator to be used—-> Intersection.

Final Query will be

Difference between Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC) :

S. Basis of
No. Comparison Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC)

The Tuple Relational Calculus The Domain Relational Calculus


(TRC) is used to select tuples (DRC) employs a list of attributes
from a relation. The tuples with from which to choose based on
1. Definition
specific range values, tuples the condition. It’s similar to TRC,
with certain attribute values, but instead of selecting entire
and so on can be selected. tuples, it selects attributes.

In TRC, the variables represent In DRC, the variables represent


Representation
2. the tuples from specified the value drawn from a specified
of variables
relations. domain.

A tuple is a single element of A domain is equivalent to column


3. Tuple/ Domain relation. In database terms, it is data type and any constraints on
a row. the value of data.

4. Filtering This filtering variable uses a This filtering is done based on the
S. Basis of
No. Comparison Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC)

tuple of relations. domain of attributes.

The predicate expression DRC takes advantage of domain


condition associated with the variables and, based on the
TRC is used to test every row condition set, returns the
5. Return Value
using a tuple variable and return required attribute or column that
those tuples that met the satisfies the criteria of the
condition. condition.

Membership The query cannot be expressed The query can be expressed using
5.
condition using a membership condition. a membership condition.

The QUEL or Query Language is The QBE or Query-By-Example is


6. Query Language
a query language related to it, query language related to it.

It reflects traditional pre- It is more similar to logic as a


7. Similarity
relational file structures. modeling language.

Notation: {T | P (T)} or {T | Notation: { a1, a2, a3, …, an | P


8. Syntax
Condition (T)} (a1, a2, a3, …, an)}

{T | EMPLOYEE (T) AND


9. Example { | < EMPLOYEE > DEPT_ID = 10 }
T.DEPT_ID = 10}

Focuses on selecting tuples from Focuses on selecting values from


10. Focus
a relation a relation

Uses scalar variables (e.g., a1, a2,


11. Variables Uses tuple variables (e.g., t)
…, an)

12. Expressiveness Less expressive More expressive


S. Basis of
No. Comparison Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC)

More difficult to use for simple


13. Ease of use Easier to use for simple queries.
queries.

Useful for selecting specific values


Useful for selecting tuples that
or for constructing more complex
14. Use case satisfy a certain condition or for
queries that involve multiple
retrieving a subset of a relation.
relations.

You might also like