Lecture 7-9 (R.A)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Lecture 7-9

Introduction to relational Model


Outline
The Relational Algebra
Select Operation
Project Operation
Union Operation
Set Difference Operation
Set intersection Operation
Cartesian Product Operation
Rename Operation
Join Operations
Relational schema for part of the
DreamHome case study
Example
List all cities where there is either a branch office or a property for rent.
Set Difference Operation
Notation: r – s
Defined as: r – s = {t | t  r and t  s}
Set differences must be taken between compatible relations.
1. r and s must have the same arity
2. attribute domains of r and s must be compatible
Set difference of two relations

Relations r, s:

r – s:
Example
List all cities where there is a branch office but no properties for rent.
Example
Find all the courses taught in the Fall 2009 semester but not in Spring 2010 semester.
Set-Intersection Operation
Notation: r  s
Defined as: r  s = { t | t  r and t  s }
Assume:
1. r, s have the same arity
2. attributes of r and s are compatible

Note: r  s = r – (r – s)
Set intersection of two relations
Relation r, s:

rs
Example
List all cities where there is both a branch office and property for rent.
Cartesian-Product Operation
Notation: r x s
Defined as: r x s = {t q | t  r and q  s}

Assume that attributes of r(R) and s(S) are disjoint. (That is, R  S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
Joining two relations -- Cartesian-product
Relations r, s:

r x s:
Cartesian-product – naming issue
Relations r, s:
B

r x s: r.B s.B
Example
List the names and comments of all clients who have viewed a property for rent.
The names of clients are held in the Client relation and the details of viewings are
held in the Viewing relation. To obtain the list of clients and the comments on
properties they have viewed, we need to combine these two relations:

In its present form, this relation contains more information than we require. For
example, the first tuple of this relation contains different clientNo values. To obtain
the required list, we need to carry out a Selection operation on this relation to extract
those tuples where Client.clientNo =Viewing.clientNo. The complete operation is
thus:
Example
Find the names of all instructors in the Physics department together with the course
id of all courses they taught.
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-algebra
expressions.
Allows us to refer to a relation by more than one name.

Example:  x (E) returns the expression E under the name X

If a relational-algebra expression E has arity n, then  x ( A1 , A2 ,..., An ) ( E )


returns the result of expression E under the name X, and with the attributes renamed
to A1 , A2 , …., An .
Renaming a Table
Allows us to refer to a relation, (say E) by more than one name.
 x (E)

returns the expression E under the name X


r.A r.B s.A s.B
α 1 α 1
α 1 β 2
β 2 α 1
β 2 β 2

Relations r r x  s (r)
Example
Find the highest salary in the university.
Strategy
(1) compute first a temporary relation consisting of those salaries that are not the
largest and
(2) take the set difference between the relation salary (instructor) and the temporary
relation just computed, to obtain the result.
Function of the relational algebra
operations.
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)
rxs

A=C (r x s)
University Schema
Practice Exercise
show records of physics department.
Show instructor table without the dept_name attribute
find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in
both using union operation
find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester
Suggested Reading
Chapter 5 of Database Systems: A Practical Approach to Design, Implementation,
and Management by Thomas Conolly 6th Edition
Chapter 2 of Database System Concepts by Abraham Silberschatz

You might also like