Lecture 7-9 (R.A)
Lecture 7-9 (R.A)
Lecture 7-9 (R.A)
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:
rs
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.
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