0% found this document useful (0 votes)
11 views31 pages

Relational Algebra

Uploaded by

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

Relational Algebra

Uploaded by

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

Relational Algebra

Introduction
• Relational algebra
– Basic set of operations for the relational model
– More operational (procedural), very useful for
representing execution plans.

• Relational calculus
– Higher-level declarative language for specifying
relational queries
– Lets users describe what they want, rather than
how to compute it: Non-operational, declarative.
Relational Algebra
• Relational algebra operations work on one or more relations to define
another relation without changing the original relations.

• Both operands and results are relations, so output from one


operation can become input to another operation.

• Allows expressions to be nested, just as in arithmetic. This property is


called closure.

• Five basic operations in relational algebra: Selection, Projection,


Cartesian product, Union and Set Difference.

• These perform most of the data retrieval operations needed.

• Also have Join, Intersection, and Division operations, which can be


expressed in terms of 5 basic operations.
Relational Algebra

• Basic operations:
– Selection (  ) Selects a subset of rows from relation.
– Projection ( 
) Deletes unwanted columns from relation.
– Cross-product ( ) Allows us to combine two relations.
– Set-difference (
– Union (

) Tuples in reln. 1, but not in reln. 2.
 ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!) useful.
Relational Algebra Operations
Relational Algebra Operations
Selection (or Restriction)
• predicate (R)
– Works on a single relation R and defines a relation that contains only
those tuples (rows) of R that satisfy the specified condition (predicate).
– Selects rows that satisfy selection condition.
– No duplicates in result!
– Schema of result identical to schema of input relation.
– Selection is distributive over binary operators
– Selection is commutative

7
Example - Selection Restriction)
• List all staff with a salary greater than £10,000.

salary > 10000 (Staff)


Projection
• col1, . . . , coln(R)
– Works on a single relation R and defines a relation that
contains a vertical subset of R, extracting the values of
specified attributes and eliminating duplicates.
– Deletes attributes that are not in projection list.
– Schema of result contains exactly the fields in the
projection list, with the same names that they had in the
input relation.
– Projection operator has to eliminate duplicates!

Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it (by DISTINCT). Why not?
Example - Projection
• Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.

staffNo, fName, lName, salary(Staff)


Union
• RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.

• If R and S have I and J tuples, respectively, union is obtained by


concatenating them into one relation with a maximum of (I + J) tuples.
Example - UNION
• List all cities where there is either a branch office or a property for rent.

city(Branch)  city(PropertyForRent)
Set Difference
• R–S
– Defines a relation consisting of the tuples that are
in relation R, but not in S.
– R and S must be union-compatible.
Example - Set Difference
• List all cities where there is a branch office but no properties for rent.

city(Branch) – city(PropertyForRent)
Intersection
• RS
– Defines a relation consisting of the set of all tuples
that are in both R and S.
– R and S must be union-compatible.

• Expressed using basic operations:


R  S = R – (R – S)
Example - Intersection
• List all cities where there is both a branch office and at least one property
for rent.

city(Branch)  city(PropertyForRent)
Cartesian product
• RXS
– Defines a relation that is the concatenation of
every tuple of relation “R with every tuple of
relation S”
Example - Cartesian product
• List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))

18
Pearson Education © 2009
Example - Cartesian product and
Selection
• Use selection operation to extract those tuples where Client.clientNo =
Viewing.clientNo.
sClient.clientNo = Viewing.clientNo((ÕclientNo, fName, lName(Client)) 
(ÕclientNo, propertyNo, comment(Viewing)))

 Cartesian product and Selection can be reduced to a single operation called a Join.

19
Pearson Education © 2009
Join Operations
• JOINs can be used to combine tables
• Join is a derivative of Cartesian product.

• Equivalent to performing a Selection, using join predicate as selection


formula, over Cartesian product of the two operand relations.

• One of the most difficult operations to implement efficiently in an


RDBMS and one reason why RDBMSs have intrinsic performance
problems.

20
Pearson Education © 2009
Join Operations
• Various forms of join operation
– JOIN (Inner Join) : Return rows when there is at least
one match in both tables
– LEFT JOIN(Left Outer Join): Return all rows from the
left table, even if there are no matches in the right table
– RIGHT JOIN(Right Outer Join): Return all rows from the
right table, even if there are no matches in the left table
– FULL JOIN: Return rows when there is a match in one
of the tables

21
Pearson Education © 2009
SQL INNER JOIN Syntax
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON
table_name1.column_name=table_name2.column_name

INNER JOIN is the same as JOIN


The word "INNER" is optional
Example*- Inner Join
Left Join
Example - Left Join
Right Join
Example – Right Join
Full Join
Example – Full Join
FAQ
1. How does Tuple-oriented relational calculus differ from domain-oriented
relational calculus?
2. Define Relational Algebra and Relational Calculus
3. What is degree of a Relation?
4. What is a Relation Schema and a Relation?
5. List various Operators used in Relational Algebra and Calculus
6. Solve the given by relational algebra. The Relations are :
• Emp(empno,ename,sal,job,comm,deptno)
• Dept (deptno,dname,dloc)
• Create a query to list unique jobs that are in deptno 30 and include the
location of department 30 in output
References

OTHER REFRENCES
• https://cs.uwaterloo.ca/~tozsu/courses/CS348/notes/4b-calculus-handout-notes.pdf
• http://www.ccs.neu.edu/home/kathleen/classes/cs3200/4-RAAndRC.pdf
• https://www2.cs.sfu.ca/CourseCentral/354/louie/Chap3_practice.pdf
• http://www.nyu.edu/classes/jcf/CSCI-GA.2433-001_fa11/slides/session5/RelationalAlgebra-
RelationalCalculus-SQL.pdf
• https://blog.inf.ed.ac.uk/da15/files/2015/01/inf1-da-15-t3.pdf
• DatabaseSystemConceptsbySudarshan,Korth(McGraw-HillEducation)

• FundamentalsofDatabaseSystemByElmasari&Navathe-PearsonEducation

• http://ecomputernotes.com/database-system/rdbms

• https://www.tutorialspoint.com/sql/sql-rdbms-concepts.htm

• https://www.studytonight.com/dbms/rdbms-concept
SUGGESTED BOOK REFERENCES
• Ramez Elmasri and Shamkant B. Navathe,“Fundamentals of Database System”,
The Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database SystemConcepts”, McGraw Hall.
• Pratt,”DBMS”, Cengage Learning.

You might also like