Chapter2 Session3
Chapter2 Session3
1
Outline
1 Relational Algebra
3 Cartesian product
5 Set Operations
1
2
Relational Algebra
Đại số quan hệ bao gồm một tập hợp các hoạt
động lấy một hoặc hai mối quan hệ làm đầu vào và
tạo ra một mối quan hệ mới do kết quả của chúng..
Hoạt động cơ bản.
Hoạt động unary: hoạt động trên một mối quan hệ.
• Select
• Project
• Rename
Binary Operations: operate on pairs of relations.
• Union , Intersection
• Set different –
• Cartesian product x
• Join operations
3
Selection and Project
Unary operation
5
Select Operation
Example: select those tuples of the instructor relation
where the instructor is in the “Physics” department.
Instructor relation
Query
dept_name=“Physics” (instructor)
Result
6
Select Operation
We allow comparisons using =, ≠, <, ≤, >, and ≥ in
the selection predicate.
we can combine several predicates into a larger
predicate by using the connectives and (∧), or (∨),
and not (¬)
7
Select Operation
Example: Find the instructors in Physics with a
salary greater $90,000, we write:
Query:
Result:
8
Project Operation
Project operation selects certain columns from the
table and discards the other columns.
Notation:
A1,A2,A3 ….Ak (r)
where A1, A2, …, Ak are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by
erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
9
Project Operation
Example: eliminate the dept_name attribute
of instructor relation
Query
ID, name, salary (instructor)
Result:
10
Composition of Relational
Operations
The result of a relational-algebra operation is relation
and therefore of relational-algebra operations can be
composed together into a relational-algebra
expression.
Consider the query -- Find the names of all
instructors in the Physics department.
name( dept_name =“Physics” (instructor))
12
Cartesian-Product Operation
Example: the Cartesian product of the relations
instructor and teaches is written as:
instructor X teaches
instructor
teaches
13
Cartesian-Product Operation
Result:
14
Cartesian-Product Operation
We construct a tuple of the result out of each possible pair
of tuples: one from the instructor relation and one from the
teaches relation.
Since the instructor ID appears in both relations we
distinguish between these attribute by attaching to the
attribute the name of the relation from which the attribute
originally came.
instructor.ID
teaches.ID
Assume that we have n1 tuples in instructor and n2 tuples in
teaches, so there are n1 ∗ n2 tuples in r
15
Join Operation
The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every
tuple of teaches.
Most of the resulting rows have information
about instructors who did NOT teach a particular
course.
16
Join Operation phép nối
To get only those tuples of “instructor X teaches”
that pertain to instructors and the courses that they
taught, we write:
instructor.id = teaches.id (instructor x teaches ))
17
Join Operation
The table corresponding to:
instructor.id = teaches.id (instructor x teaches))
18
Join Operation phép nối
19
Join Operation
20
Join operator
21
Outer Joins
In Inner Join, we matched rows are returned and
unmatched rows are not returned.
But, in outer join, we include those tuples which
meet the given condition along with that, we also
add those tuples which do not meet the required
condition.
There are three types of outer joins:
Left outer join
Right outer join
Full outer join
22
Outer Joins
23
Left Outer Join
Left Outer Join : A <join condition> B
Ensures that all tuples in the in the relation A are
present in the result set.
The tuples in A without matching tuples in B are
filled with null values for B’s attributes
24
Left Outer Join example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN NULL NULL
25
Right Outer Join
Right Outer Join: A <join condition> B
26
Right Outer Join example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
NULL NULL NULL CH Chemistry
27
Full Outer Join
28
Full Outer Join Example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students <course = course#> Courses
Result:
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter CN NULL NULL
NULL NULL NULL CH Chemistry
29
Set operations
Example
Section relation
30
Union Operation
The union operation allows us to combine two relations
Notation: r s
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
31
Union Operation
Example: to find all courses taught in the Fall 2017
semester, or in the Spring 2018 semester, or in both
course_id ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
The result of this expression, shown in the next slide
32
Union Operation
section relation result
33
Set-Intersection Operation
The set-intersection or intersection operation
allows us to find tuples that are in both the
input relations.
Notation: r s
Assume:
r, s have the same arity
attributes of r and s are compatible
34
Set-Intersection Operation
Example: Find the set of all courses taught in
both the Fall 2017 and the Spring 2018
semesters.
course_id ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
Result:
35
Set Difference Operation
The set-difference operation allows us to find
tuples that are in one relation but are not in
another.
Notation r – s
Set differences must be taken between
compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
36
Set Difference Operation
Example: to find all courses taught in the Fall
2017 semester, but not in the Spring 2018
semester
course_id ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
Result:
37
The Rename Operation
The results of relational-algebra expressions do
not have a name that we can use to refer to them.
The rename operator, , is provided for that
purpose
The expression:
x (E)
returns the result of expression E under the name x
Another form of the rename operation:
x(A1,A2, .. An) (E)
38
Equivalent Queries
There is more than one way to write a query in relational
algebra.
Example: Find information about courses taught by
instructors in the Physics department with salary greater than
90,000
Query 1
dept_name=“Physics” salary > 90,000 (instructor)
Query 2
dept_name=“Physics” ( salary > 90.000 (instructor))
The two queries are not identical; they are, however, equivalent
-- they give the same result on any database.
39
Equivalent Queries
40
Exercises
Example: Customer
Ex1:
41
Exercises sid bid day
Reserves 22 101 10/10/11
Ex2 58 103 11/12/11
sid sname rating age
bid bname color
22 Jesly 7 45.0 101 Interlake Blue
31 Mishail 8 55.5 102 Interlake Red
58 Raj 10 35.0 103 Clipper Green
sailors 104 Marine Red
1.Find names of sailors who’ve reserved boat #103 Boats
2.Find names of sailors who’ve reserved a red boat
3.Find sailors who’ve reserved a red or a green boat
4.Find sailors who’ve reserved a red and a green boat
5. Find the names of sailors who’ve reserved all boats
42
43