Professional Documents
Culture Documents
RELATIONAL
ALGEBRA
OUTLINE
Relational Algebra
Unary Relational Operations
Relational Algebra Operations From Set Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
Slide
4 -2
RELATIONAL ALGEBRA
Relational algebra is a procedural language used to
query the database tables to access data in different
ways.
Slide
4 -3
It consists of a set of operations that take one or two
relations as input (table from which data has to be
accessed) and produce a new relation as their result(a
temporary table holding the data asked for by the user)
R Relational Algebra R
R Relational Algebra R
R
These operations enable a user to specify basic retrieval
requests (or queries).It provides operators which
performs operations on existing relation to derive results
relations.
Slide
It results always distinct tuple. 4 -4
Characteristics of relational operators :
Slide
4 -5
Slide
4 -6
These operations can be categorised into two groups; unary
and binary operations:
Unary Operations:
Theseoperate on one relation.
Examples of unary operations include; selection and projection,
rename
Binary Operations;
These work on pairs of relations.
Examples include the; Cartesian product, union, set difference, join,
intersections and division operations
Slide
4 -7
UNARY RELATIONAL OPERATIONS
SELECT Operation
Selection is an operation that select rows or tuples from a relation
that satisfy a selection condition while others are discarded.
Syntax:
In general, the select operation is denoted by
<selection condition> (R)
where the symbol (sigma) is used to denote the select operator,
and the selection condition is a Boolean expression specified on
the attributes of relation R
Slide
4 -8
1)The
Boolean expression specified in <selection condition> is made up of a
number of clauses of the form
2) Clauses can be arbitrarily connected by the Boolean operators AND, OR, and
NOT to form a general selection condition.
Slide
4 -9
ID Fname Lname Age
1 Jon Stark 25
2 Arya Stark 28
3 Bran Stark 26
Query
•σage > 25 (Employee)
• σ(select predicate)
• r(relation) – Employee
• p(proposition logic) – age >25
• Result – returning the list of students with age greater than 25.
2 Arya Stark 28
3 Bran Stark 26
Slide
4 -10
ID Fname Lname Age
1 Jon Stark 25
2 Arya Stark 28
3 Bran Stark 26
Query
•σ (age > 25) and (Fname = 'Arya’ )(Student)
• σ(select predicate)
• r(relation) – Employee
• p(proposition logic) – σ(age > 25) and (Fname = 'Arya’ )(Student)
OR
σ(age > 25) ^ (Fname = 'Arya’) (Student)
• Result – returning the list of students with age greater than 25 and
Fname equals to arya
It is a unary operator.
Operation is applied to each tuple individually.
Degree of the relation from select operation is same
as input relation.
It is commutative in nature.
The number of rows returned by selection operation is
less than or equal to number of rows in original table.
Slide
4 -12
SELECT Operation Properties
The SELECT operation <selection condition>(R) produces a relation S that has the
same schema as R
The result of the PROJECT operation has only the attributes specified in
<attribute list> and in the same order as they appear in the list. Hence, its
degree is equal to the number of attributes in <attribute list>.
Project
Slide
Department from 4 -19
table
If the list of attributes includes a key of R, then the number
of tuples is equal to the number of tuples in R.
Slide
4 -20
UNARY RELATIONAL OPERATIONS (CONT.)
Rename Operation
It is used to store the Intermediate result of any query.
Intermediate results are stored in a temporary table using assignment operator and it
is renamed.
The rename operator can also be used to rename attributes.
The rename operator is
The general Rename operation can be expressed by any of the following forms:
Slide
4 -21
Notation: ρOldName→NewName(r)
For example, ρ Father→Parent (Paternity)
If there are two or more attributes involved in a
renaming operation, then ordering is meaningful: e.g.,
ρBranch,Salary → Location,Pay(Employees)
Slide
4 -22
RELATIONAL ALGEBRA OPERATIONS FROM
SET THEORY
Relations are a set of tuples. So set operations can be applied to them, but
it is not possible in all the cases. SET operators can be applied to the
relations only if they are UNION COMPATIBLE .
Both the relations , i.e. R and S should be of same arity, i.e. they should have same
number of attributes.
Domain of the similar attributes, i.e. domain of ith attribute of R and domain of ith
attribute of S, should be same and this is applicable to all the attributes.
Slide
4 -23
For example :
Slide
4 -24
For example :
Slide
4 -25
RELATIONAL ALGEBRA OPERATIONS FROM
SET THEORY
UNION Operation
The result of this operation, denoted by R S, is a relation that includes all
tuples that are either in R or in S or in both R and S. Duplicate tuples are
eliminated.
Union, intersection and difference operation, the two operand relations
must be Union compatible or “type compatible”.
Slide
4 -26
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY (CONT.)
INTERSECTION OPERATION
The result of this operation, denoted by R S, is a relation that includes all
tuples that are in both R and S. The two operands must be "type compatible"
Example: The result of the intersection operation (figure below) includes only
those who are both students and instructors.
Slide
STUDENT INSTRUCTOR
4 -27
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY (CONT.)
Set Difference (or MINUS) Operation
The result of this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type compatible”.
Example: The figure shows the names of students who are not instructors, and
the names of instructors who are not students.
STUDENT-INSTRUCTOR
Slide
4 -28
INSTRUCTOR-STUDENT
RELATIONAL ALGEBRA OPERATIONS
FROM SET THEORY (CONT.)
Notice that both union and intersection are commutative
operations; that is
R S = S R, and R S = S R
If the relation R has m tuples and the relation S has n tuple then R × S have
m times n tuples.
The result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation Q with
degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
Slide
4 -34
1) Member IDs of members who have borrowed books.
2) Member IDs of members and the Book IDs of the books
they have borrowed books.
3) Details of the members who were born on 21/10/1997.
4) Rename the Member relation as Library Member.
5) Display the details of members and books
Slide
4 -35