0% found this document useful (0 votes)
2 views26 pages

Lecture 9 10

The document provides an introduction to the relational model, focusing on relational algebra operations such as selection, projection, union, and various join types. It explains how to compose operations, perform joins (including theta, natural, outer, and semijoins), and utilize aggregate functions and grouping. Additionally, it includes practice exercises and references for further reading on database systems.

Uploaded by

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

Lecture 9 10

The document provides an introduction to the relational model, focusing on relational algebra operations such as selection, projection, union, and various join types. It explains how to compose operations, perform joins (including theta, natural, outer, and semijoins), and utilize aggregate functions and grouping. Additionally, it includes practice exercises and references for further reading on database systems.

Uploaded by

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

Lecture 9-10

Introduction to relational Model

Marina Gul
Sukkur IBA University Khairpur Campus
2 Agenda
The Relational Algebra
Select Operation
Project Operation
Union Operation
Set Difference Operation
Set intersection Operation
Cartesian Product Operation
Rename Operation
Join Operations
3
Function of the relational algebra
operations
4 Composition of Operations
Can build expressions using multiple
operations
Example: A=C (r x s)

rxs

A=C (r x s)
5 Join Operations
Join is a derivative of Cartesian product.
Equivalent to using join predicate as selection formula, over Cartesian product of
the two operand relations.
There are various forms of the Join operation, each with subtle differences, some
more useful than others:
• Theta join
• Equijoin (a particular type of Theta join)
• Natural join
• Outer join
• Semijoin
6 Theta join ( -join) R F S
Defines a relation that contains tuples satisfying the predicate F from the Cartesian
product of R and S.

The predicate F is of the form R.ai  S.bi where  may be one of the comparison operators
(<, , >, , =, ).

Can rewrite Theta join using basic Selection and Cartesian product operations. R F S=
F(R  S)

Degree of a Theta join is sum of degrees of the operand relations R and S. If predicate F
contains only equality (=), the term Equijoin is used.

Example: List the names and comments of all clients who have viewed a property for rent.

(clientNo, fName, lName(Client)) Client.clientNo = Viewing.clientNo (clientNo, propertyNo, comment(Viewing))


7 Natural join (R S)
An Equijoin of the two relations R and S over all common attributes x. One
occurrence of each common attribute is eliminated from the result.

Example: List the names and comments of all clients who have viewed a property
for rent.

(clientNo, fName, lName(Client)) (clientNo, propertyNo, comment(Viewing))


8 Practice
Find the names of all instructors together with the course id of all courses they
taught.
9 Practice
Find the names of all instructors in the Comp. Sci. department together with
the course titles of all the courses that the instructors teach
1
0
Outer Joins
In an outer join, along with tuples that satisfy the matching
criteria, we also include some or all tuples that do not match the
criteria.
Result table has two rows where cities are same.
There are no rows corresponding to branches in Bristol and
Aberdeen.
To include unmatched rows in result table, use an Outer join.
1
1
Left Outer Join (R S)
(Left) outer join is join in which tuples from R that do not have matching values in
common columns of S are also included in result relation.

Unmatched columns are filled with NULLs.


1
2
Practice
Produce a status report on property viewings.
1
3
Right Outer Join (R S)
Right Outer join includes those rows of second (right) table that are unmatched with
rows from first (left) table.
1
4
Full Outer Join
Includes rows that are unmatched in both tables.
Unmatched columns are filled with NULLs.
1
5
Semijoin (R F S)
Defines a relation that contains the tuples of R that participate in the join of R with S.
Can rewrite Semijoin using Projection and Join:

R F S = A(R F S)

A is the set of all attributes for R


1
6
Practice
List complete details of all staff who work at the branch in Glasgow.

Staff Staff.branchNo=Branch.branchNo (city=‘Glasgow’(Branch))


1
7
Division (R  S)
Defines a relation over the attributes C that consists of set of tuples from R that match
combination of every tuple in S.
Expressed using basic operations:

T1  C(R)
T2  C((S X T1) – R)
T  T1 – T2

Example: Identify all clients who have viewed all properties with three rooms.
(clientNo, propertyNo(Viewing))  (propertyNo(rooms = 3 (PropertyForRent)))
1
8
Aggregate Operations AL(R)
Applies aggregate function list, AL, to R to define a relation over the aggregate list.
AL contains one or more (<aggregate_function>, <attribute>) pairs .

Main aggregate functions are: COUNT, SUM, AVG, MIN, and MAX.
1
9
Practice
How many properties cost more than £350 per month to rent?

R(myCount) COUNT propertyNo (σrent > 350 (PropertyForRent))


2
0
Grouping Operation GAAL(R)
Groups tuples of R by grouping attributes, GA, and then applies aggregate function list,
AL, to define a new relation.

AL contains one or more (<aggregate_function>, <attribute>) pairs.

Resulting relation contains the grouping attributes, GA, along with results of each of the
aggregate functions.
2
1
Practice
Find the number of staff working in each branch and the sum of their salaries.

R(branchNo, myCount, mySum) branchNo  COUNT staffNo, SUM salary (Staff)


Relational schema for part of the DreamHome
2 case study
2
2
3
2
4
University Schema
2
5
2
6
Suggested Reading
Chapter 5 of Database Systems: A Practical Approach to Design,
Implementation, and Management by Thomas Conolly 6th Edition
Chapter 6 of Database System Concepts by Abraham Silberschatz

You might also like