0% found this document useful (0 votes)
13 views61 pages

CH 4 Relational Model

Chapter 4 of the Advanced Database Management course covers the relational model, focusing on relational algebra and its operations such as selection, projection, union, and join. It also discusses query processing and optimization, emphasizing the importance of efficient execution plans for SQL queries. The chapter includes practical examples and exercises to reinforce the concepts presented.

Uploaded by

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

CH 4 Relational Model

Chapter 4 of the Advanced Database Management course covers the relational model, focusing on relational algebra and its operations such as selection, projection, union, and join. It also discusses query processing and optimization, emphasizing the importance of efficient execution plans for SQL queries. The chapter includes practical examples and exercises to reinforce the concepts presented.

Uploaded by

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

BACS3183

Advanced Database Management


Chapter 4
Relational Model
Learning Outcomes

• Formulate queries in relational algebra.

• Understand the basic concepts of


query processing and optimization.

2
Introduction
• Relational algebra and calculus are the
theoretical concepts used on relational model.

• RDBMS is a practical implementation of


relational model.

• SQL is a practical implementation of relational


algebra and calculus.
1. Relational Algebra
• 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
4
Dream Home
BRANCH (BranchNo, Street, City, Postcode)

STAFF (StaffNo, FName, LName, Position, Sex, DOB, Salary,


BranchNo)

PROPERTYFORRENT (PropertyNo, Street, City, Postcode, Type,


Rooms, Rent, OwnerNo, StaffNo, BranchNo)

PRIVATEOWNER (OwnerNo, FName, LName, Address, TelNo)

CLIENT (ClientNo, FName, LName, TelNo, PrefType, MaxRent)

VIEWING (ClientNo, PropertyNo, ViewDate, Comments)


5
Dream Home Sample Relations

6
7
Relational Algebra Operations

8
Relational Algebra Operations

9
Relational Algebra Symbols
Projection (π)  Union (U)
Selection (σ)  Intersection (∩)
 Different (-)
Rename (R)  Aggregate function: max, min,
Natural join (⋈) avg, sum, count ()

Left outer join ( )  And (^)


 Or (v)
Right outer join ( )
 Equal ( = ) Not equal ( = )
Semijoin ( )( )  More than (>) Less than (<)
Division (÷)  More than or equal to (>)
Less than or equal to (<)
 Like ‘%Address%’
10
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).

Will display all attributes of the relation

11
Example - Selection (or Restriction)
• List all staff with a salary greater than £10,000.
salary > 10000 (Staff)

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

Project yields values for selected attributes

13
Example - Projection
• Produce a list of salaries for all staff, showing
only staffNo, fName, lName, and salary details.
staffNo, fName, lName, salary(Staff)

14
Question
Given the following SQL statements, write
the related relational algebra
Select StaffNo,Fname, Lname, Salary
From Staff
Where salary < 1000;

15
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.
Same
attributes

• 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.
16
UNION
• The Company A has bought another company,
Company B. Thus, the company has 2
customer tables that require merging.
• Merge the customer table with customer_2
table using union

17
Example - Union
• List all cities where there is either a branch
office or a property for rent.
city(Branch)  city(PropertyForRent)

18
List all cities where there is either a branch
office or a property.

(SELECT city
FROM Branch
WHERE city IS NOT NULL) UNION
(SELECT city
FROM PropertyForRent
WHERE city IS NOT NULL);

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

20
Example - Set Difference
• List all cities where there is a branch office
but no properties for rent.
city(Branch) – city(PropertyForRent)

21
List of all cities where there is a branch office
but no properties.
(SELECT city FROM Branch)
EXCEPT
(SELECT city FROM PropertyForRent);

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

23
INTERSECT
• The management wants to know which
customers are dealing with both companies.
Customers which deal with both companies
are considered as not loyal.
• Use INTERSECT to combine rows from two
queries, returning only the rows that appear
in both sets.

24
Example - Intersection
• List all cities where there is both a branch
office and at least one property for rent.

city(Branch)  city(PropertyForRent)

25
List all cities where there is both a branch
office and a property.

(SELECT city FROM Branch)


INTERSECT
(SELECT city FROM PropertyForRent);

26
Join Operations
• Various forms of join operation
– Natural join
– Left Outer join
– Right Outer join
– Semijoin

27
Natural join
• R S
– The result of the natural join is the set of
all combinations of tuples in R and S that
are equal on their common attribute
names
R S

28
Example - Natural Join
• List the clientno, names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client)) (clientNo, propertyNo, comment(Viewing))

29
Outer Join
• To display rows in the result that do not
have matching values in the join column,
use Outer join.
- Left outer join

- Right outer join R S


Left Outer Join
Left outer join

– (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.

R S
Example - Left Outer Join

32
Example - Left Outer join
Produce a status report on property viewings.
propertyNo, street, city(PropertyForRent) Viewing

33
List branches and properties that are in same
city along with any unmatched branches.

SELECT b.*, p.*


FROM Branch1 b LEFT OUTER JOIN
PropertyForRent1 p ON b.bCity = p.pCity;

34
Right Outer Join
• R⟖S

The right outer join of relations R and S is


written as R ⟖ S.
The result of the right outer join is the set of all
combinations of tuples in R and S that are
equal on their common attribute names, in
addition to tuples in S that have no matching
tuples in R.
35
Example - Right Outer Join

36
List branches and properties in same city and
any unmatched properties.

SELECT b.*, p.*


FROM Branch1 b RIGHT OUTER JOIN
PropertyForRent1 p ON b.bCity = p.pCity;

37
Semijoin
• R FS
– Defines a relation that contains the tuples of R that
participate in the join of R with S.

R S
Example - Semijoin

39
Find all staff who work in a Glasgow branch.

SELECT *
FROM Staff s
WHERE EXISTS
(SELECT *
FROM Branch b
WHERE s.branchNo = b.branchNo AND
city = ‘Glasgow );

40
Division
• RS
– Defines a relation over the attributes B that
consists of set of tuples from R that match
combination of every tuple in S.
R S RS

41
Example of Division
Identify students who has completed
Database1 and Database 2

42
Example - Division
• Identify all clients who have viewed all properties
with three rooms.
(clientNo, propertyNo(Viewing)) 
(propertyNo(rooms = 3 (PropertyForRent)))

43
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.
Eg:

44
Find number of Managers and sum of their
salaries.
SELECT COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
WHERE position = ‘Manager’;

45
Example – Aggregate Operations
• How many properties cost more than £350 per
month to rent?
R(myCount) COUNT propertyNo (σrent > 350 (PropertyForRent))

46
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.
47
Example – Grouping Operation
• 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)

48
Find number of staff in each branch and their
total salaries.

SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;

49
2. Query Optimization
Basic Steps in Query Processing
1. Parsing and translation
2. Optimization
3. Evaluation
Query Optimization
 Activity of choosing an efficient execution plan for
processing an SQL query.
• As there are many equivalent transformations of same
high-level query, aim of QO is to
 minimize resource usage (generally refers to total execution
time of the query)

51
52
Find all Managers who work at a London branch.
SELECT *
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND
(s.position = ‘Manager’ AND b.city = ‘London’);
• Assume:
– 1000 tuples in Staff; 50 tuples in Branch;
– 50 Managers; 5 London branches;
– tuples are accessed one at a time.
– Results of intermediate operations are stored on disk.
• Comparison is based on the no. of disk accesses

(1000 + 50) + 2*(1000 * 50) = 101 050

2*1000 + (1000 + 50) = 3 050

54
1000 + 2*50 + 5 + (50 + 5) = 1 160
(1000 + 50) + 2*(1000 * 50) = 101 050

2*1000 + (1000 + 50) = 3 050

1000 + 2*50 + 5 + (50 + 5) = 1 160


(1) Read 1000 Staff, Read 50 Branch
Write 1000 x 50 records, read 1000 x 50 to search

(2) Read 1000 Staff, Read 50 Branch


Join creates 1000 rows of Staff-Branch data; write & read

(3) Read 1000 Staff, write 50 managers


Read 50 Branch, write 5 branches in London
Read 50 managers & 5 branches to create final join
Evaluation of Expressions
• Strategy 1: Materialization.
– Evaluate one operation at a time, starting at the lowest
level. Use intermediate results materialized in
temporary relations to evaluate next level operation(s).
– Generating and saving temporary files on disk is time
consuming and expensive.
• Strategy 2: Pipelining.
– Evaluate several operations simultaneously, and pass
the result (tuple- or block-wise) on to the next
operation.
– Avoid constructing temporary results as much as
possible.
– Also known as stream-based or on-the-fly processing. 56
Materialization

ORDERDETAILS

1. Compute and store  Price>5000 (ORDERDETAILS);


2. Compute and store join of CUSTOMERS and ORDERS;
3. Join the two materialized relations and project on to
CName.
57
Pipelining

ORDERDETAILS

Once a row from ORDERDETAILS satisfying


selection condition has been found,
pass it on to the join.
Similarly, don't store result of (final) join, but pass
tuples directly to projection. 58
Exercise
The following tables form part of a Library database
held in an RDBMS:

Book (ISBN, title, edition, year)


BookCopy (copyNo, ISBN*, available)
Borrower (borrowerNo, borrowerName, borrowerAddr)
BookLoan (copyNo*, dateOut, dateDue, borrowerNo*)

59
1. List all book titles.
2. List all borrower details.
3. List all book titles published in the year 2019.
4. List all copies of book titles that are available for
borrowing.
5. List all copies of the book title “Lord of the Rings” that
are available for borrowing.
6. List the names of borrowers who currently have the
book title “Lord of the Rings” on loan.
7. List the names of borrowers with overdue books.
8. Produce a report of book titles that have been
borrowed by “Peter Bloomfield”.
9. Produce a report with the details of borrowers who
currently have books overdue
60
References

• Database Systems: A Practical Approach to


Design, Implementation and Management.
Connolly, T. M. and Begg, C. E. Pearson.

• Fundamentals of Database Systems. Elmasri, R.


and Navathe, S.B. Pearson.

61

You might also like