CH 4 Relational Model
CH 4 Relational Model
2
Introduction
• Relational algebra and calculus are the
theoretical concepts used on relational model.
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 ()
• 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).
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.
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
• RS
– 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
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
• RS
– 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.
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
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.
34
Right Outer Join
• R⟖S
36
List branches and properties in same city and
any unmatched properties.
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
• RS
– 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 RS
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
• GAAL(R)
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
54
1000 + 2*50 + 5 + (50 + 5) = 1 160
(1000 + 50) + 2*(1000 * 50) = 101 050
ORDERDETAILS
ORDERDETAILS
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
61