1 Unit V Relational Algebra and Relational Calculus Relational Algebra: Relational algebra is a collection of operations that are used to manipulate the entire set of relations. The output of any relational algebra operation is always a relation. A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation that represents the result of a database query. The relational algebra is often considered to be an integral part of the relational data model. Its operations can be divided into two groups. 1. SELECT, PROJECT, and JOIN. 2. Set Operations: UNION, INTERSECTION, SET DIFFERENCE, CARTESIAN PRODUCT. Let us consider the following tables which will be used to demonstrate the SELECT and PROJECT operations. EMPLOYEE SSN Name DOB Address Gender Salary SuperSSN DNo 1111 Divya 20-Feb-82 Malleswaram M 22000 4444 1 2222 Scott 10-Dec-60 Rajajinagar M 30000 4444 3 3333 Pooja 22-Jan-65 Indiranagar F 18000 2222 2 4444 Prasad 11-Jan-57 Rajajinagar M 32000 Null 3 5555 Reena 15-Jan-85 MG Road F 8000 4444 3 PROJECTS PNo PName PLocation DNo 10 Library Management USA 2 20 ERP Chennai 1 30 Hospital Management Mumbai 3 40 Wireless Network London 2 The SELECT Operation (σ): The SELECT operation is used to select a subset of the tuples from a relation that satisfies a selection condition. The SELECT operation can also be visualized as a horizontal partition of the relation into two sets of tuples. One set of tuples that satisfy the condition and are selected, another set of tuples that do not satisfy the condition and discarded. This particular operation allows us to manipulate data in single relation. It uses a mathematical symbol, σ (sigma) and name of the relation is shown in brackets and the condition as a subscript. The general form of selection operation is shown below. σ <selection-condition> (Relation) The <selection-condition> in the above syntax may contain logical and arithmetic operators. Relation is the name of the relation from which the data will be selected. We can use operators such as =, <, <=, >=, >, ≠, etc. and also Boolean operators like and, or, and not.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Unit V
Relational Algebra and Relational Calculus
Relational Algebra:
Relational algebra is a collection of operations that are used to manipulate the entire set of
relations. The output of any relational algebra operation is always a relation. A sequence of
relational algebra operations forms a relational algebra expression, whose result will also be a
relation that represents the result of a database query.
The relational algebra is often considered to be an integral part of the relational data model. Its
operations can be divided into two groups.
1. SELECT, PROJECT, and JOIN.
2. Set Operations: UNION, INTERSECTION, SET DIFFERENCE, CARTESIAN PRODUCT.
Let us consider the following tables which will be used to demonstrate the SELECT and
PROJECT operations.
EMPLOYEE
SSN Name DOB Address Gender Salary SuperSSN DNo
1111 Divya 20-Feb-82 Malleswaram M 22000 4444 1
2222 Scott 10-Dec-60 Rajajinagar M 30000 4444 3
3333 Pooja 22-Jan-65 Indiranagar F 18000 2222 2
4444 Prasad 11-Jan-57 Rajajinagar M 32000 Null 3
5555 Reena 15-Jan-85 MG Road F 8000 4444 3
PROJECTS
PNo PName PLocation DNo
10 Library Management USA 2
20 ERP Chennai 1
30 Hospital Management Mumbai 3
40 Wireless Network London 2
The SELECT Operation (σ):
The SELECT operation is used to select a subset of the tuples from a relation that satisfies a
selection condition. The SELECT operation can also be visualized as a horizontal partition of
the relation into two sets of tuples. One set of tuples that satisfy the condition and are selected,
another set of tuples that do not satisfy the condition and discarded. This particular operation
allows us to manipulate data in single relation. It uses a mathematical symbol, σ (sigma) and
name of the relation is shown in brackets and the condition as a subscript.
The general form of selection operation is shown below.
σ <selection-condition> (Relation)
The <selection-condition> in the above syntax may contain logical and arithmetic
operators. Relation is the name of the relation from which the data will be selected. We can use
operators such as =, <, <=, >=, >, ≠, etc. and also Boolean operators like and, or, and not.
2
Examples:
1. Find the employees whose salary is greater than Rs. 10000.
σ <Salary > 10000> (EMPLOYEE)
The result for the above query is as shown below
SSN Name DOB Address Gender Salary SuperSSN DNo
1111 Divya 20-Feb-82 Malleswaram M 22000 4444 1
2222 Scott 10-Dec-60 Rajajinagar M 30000 4444 3
3333 Pooja 22-Jan-65 Indiranagar F 18000 2222 2
4444 Prasad 11-Jan-57 Rajajinagar M 32000 Null 3
2. Find the employees who work for department 3 and whose salary is greater than Rs. 30000.
Emp = σ <DNo = 3> (EMPLOYEE)
Result = σ <Salary > 30000> (Emp)
In the above expression, Emp and Result are two temporary relations. The first
expression gives all employees working for department 3 and the second expression retrieves the
employees from temporary relation whose salary is greater than 30000.
The result for the above query is as shown below
SSN Name DOB Address Gender Salary SuperSSN DNo
4444 Prasad 11-Jan-57 Rajajinagar M 32000 Null 3
An alternate solution for the above problem without using temporary relation is given below.
σ <DNo = 3 and Salary > 30000> (EMPLOYEE)
3. Find all the employees who either work in department 1 and earn Rs 20000 or work in
department 3 and earn Rs. 29000.
σ < (DNo = 1 and Salary > 20000) or (DNo = 3 and Salary > 29000) > (EMPLOYEE)
The result for the above query is as shown below
SSN Name DOB Address Gender Salary SuperSSN DNo
1111 Divya 20-Feb-82 Malleswaram M 22000 4444 1
2222 Scott 10-Dec-60 Rajajinagar M 30000 4444 3
4444 Prasad 11-Jan-57 Rajajinagar M 32000 Null 3
4. List all the projects controlled by department 2.
σ <DNo = 2> (PROJECTS)
The result for the above query is as shown below
PNo PName PLocation DNo
10 Library Management USA 2
40 Wireless Network London 2
3
One of the interesting properties of the selection operation is that it is commutative. Therefore,
all the expressions given below are equivalent.
σ <condition-1> (σ <condition-2> (R))
σ <condition-1 and condition-2> (R)
σ <condition-2> (σ <condition-1> (R))
The PROJECT Operation (π):
The project operation is used to select only few columns from a relation. If we are interested in
only certain attributes of a relation, we use the PROJECT operation to project over these
attributes only. Therefore, the result of the PROJECT operation can be visualized as a vertical
partition of the relation into relations: one has the needed columns (attributes) and contains the
result of the operation and the other contains the discarded columns.
In relational algebra, the mathematical symbol, π (pi) is used for projection. The general syntax
for projection is shown below:
π <attribute> (Relation)
The name of the attributes can be specified separated by comma in the subscript part of π (pi).
Let us consider the relations EMPLOYEE and PROJECTS to demonstrate project (π) operation
in the following examples.
Examples:
1. List the name and salary of all the employees.
π <Name, Salary> (EMPLOYEE)
The above project operation retrieves only Name and Salary columns of EMPLOYEE
relation. The output will be as shown below.
Name Salary
Divya 22000
Scott 30000
Pooja 18000
Prasad 32000
Reena 8000
2. List the project names and their locations.
π <PName, PLocation> (PROJECTS)
The above project operation retrieves only Project Name and Location columns of
PROJECTS relation. The output will be as shown below.
PName PLocation
Library Management USA
ERP Chennai
Hospital Management Mumbai
Wireless Network London
4
3. Retrieve the Name and Salary of all employees who are working for department 1.
π <Name, Salary> (σ <DNo=1> (EMPLOYEE))
The above query involves two operations, First, SELECT operation is applied to retrieve
the employees who are working in department 1 and it gives entire tuples which satisfies the
condition. Second, we have to retrieve only name and salary of the employees from the first
operation.
Name Salary
Divya 22000
Alternatively, we can split the above query into two queries by making use of intermediate
relations.
Dept1 = σ <DNo=1> (EMPLOYEE)
Dept1
SSN Name DOB Address Gender Salary SuperSSN DNo
1111 Divya 20-Feb-82 Malleswaram M 22000 4444 1
Result = π <Name, Salary> (Dept1)
Result
Name Salary
Divya 22000
4. Find the name, address, and salary of the employees who earn more than 25,000 rupees.