You are on page 1of 35

THE

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 :

 Relational operators always work on one or more relational table.


 Relational operators always produce another relational table.
 The result table produced by a relational operator has all the
properties of relational model.
 Union, intersection and difference operation, the two operand
relations must be Union compatible.

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

<attribute name> <comparison op> <constant value>,


or
<attribute name> <comparison op> <attribute name>

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.

ID Fname Lname Age

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

ID Fname Lname Age


Slide
4 -11
2 Arya Stark 28
 Characteristics of selection operator :

 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 SELECT operation is commutative; i.e.,


 <condition1>(< condition2> ( R)) = <condition2> (< condition1> ( R))

 A cascaded SELECT operation may be applied in any order;


i.e.,
 <condition1>(< condition2> (<condition3> ( R))
 = <condition2> (< condition3> (< condition1> ( R)))

 A cascaded SELECT operation may be replaced by a single


selection with a conjunction of all the conditions; i.e., Slide
 <condition1>(< condition2> (<condition3> ( R)) 4 -13
 = <condition1> AND < condition2> AND < condition3> ( R)))
Slide
4 -14
PROJECT Operation
Projection is an operation that select specified
attributes from a relation.

It is used to filter the column .

The result of the projection is a new relation


having the selected attributes.

It can also be used to change the order of


attributes in a relation.
 Syntax:
The general form of the project operation is
<attribute list>(R)
where  (pi) is the symbol used to represent the project operation
and <attribute list> is the desired list of attributes from the attributes of
relation R.
OR

 where A1, A2 etc are attribute names(column names).

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

 It will only project or show the columns or attributes asked for,


and will also remove duplicate data from the columns. Slide
4 -17
Slide
4 -18
PROJECT Operation Properties

 The number of tuples in the result of projection <list> Ris


always less or equal to the number of tuples in R.

 If the attribute list includes only nonkey attributes of R,


duplicate tuples are likely to occur; the PROJECT operation
removes any duplicate tuples, so the result of the PROJECT
operation is a set of tuples and hence a valid relation.
This is known as duplicate elimination.

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.

  <list2> R)<list1> R


<list1>
as long as<list2>contains theattributes in<list1>.
commutatively does not hold on PROJECT operation.
Example:

namename,age,address R)<list1> 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 .

 Two relations R and S , are said to be UNION COMPATIBLE, only if


they satisfy these two conditions :

 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 :

R ( Sid , Sname ) and S ( Sid , Sname , Marks )

R ( Sid , Sname ) and S ( Sid , Marks )

R ( Sid , Sname ) and S ( Sid , StudentName )

Slide
4 -24
 For example :

R ( Sid , Sname ) and S ( Sid , Sname , Marks ) are not UNION


COMPATIBLE, as R has 2 attributes while S has 3 attributes.

R ( Sid , Sname ) and S ( Sid , Marks ) are not UNION


COMPATIBLE, as domain of name and marks is different.
Domain of names would be certain strings while domain of marks
would be integer and float numbers.

R ( Sid , Sname ) and S ( Sid , StudentName ) are UNION


COMPATIBLE, as R and S both have 2 attributes and their
domains are also same, only the column names are different.

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

 Both union and intersection can be treated as n-ary operations


applicable to any number of relations as both are associative
operations; that is
R  (S  T) = (R  S)  T, and (R  S)  T = R  (S  T)

 The minus operation is not commutative; that is, in general


R-S≠S–R
Slide
4 -29
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY (CONT.)
 CARTESIAN (or cross product) Operation
 The product of two relations is a cartesian product and it is the
concatenation of every tuple of one relation with every tuple of a second
relation.

 If the relation R has m tuples and the relation S has n tuple then R × S have
m times n tuples.

 If any one of R and S is empty, R × S ⇒ Φ

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

 The two operands do NOT have to be "type compatible”


Slide
4 -30
Slide
4 -31
Slide
4 -32
Slide
4 -33
 CUSTOMER(CID,Cname,Caddress,Cage,Status)
1) Display the Customer Name and Status
2) Display the customer details where sales is greater than
50000
3) Display the customers with age less than 30 and active
status

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

You might also like