0% found this document useful (0 votes)
76 views

Chapter2 Session3

The document discusses relational algebra operations including selection, projection, Cartesian product, joins, and set operations. Selection allows filtering tuples that satisfy a predicate, projection selects certain columns and discards others. Cartesian product combines all tuples from two relations. Joins match tuples from two relations based on a condition. Outer joins include unmatched tuples with null values. Union combines tuples from relations while intersection finds tuples that are in both relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Chapter2 Session3

The document discusses relational algebra operations including selection, projection, Cartesian product, joins, and set operations. Selection allows filtering tuples that satisfy a predicate, projection selects certain columns and discards others. Cartesian product combines all tuples from two relations. Joins match tuples from two relations based on a condition. Outer joins include unmatched tuples with null values. Union combines tuples from relations while intersection finds tuples that are in both relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Database Systems

Chapter 2: Database Design


session 3:
Relational Algebra

1
Outline
1 Relational Algebra

2 Select and Product

3 Cartesian product

4 Inner Join and Outer Join

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

Phép chọn: lấy dòng +Phép chiếu : lấy cột


4
Select Operation
The select operation selects tuples that satisfy a
given predicate.
Notation:  p (r)
p is called the selection predicate

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))

Instead of giving the name of a relation as the


argument of the projection operation, we give an
expression that evaluates to a relation.
11
Cartesian-Product Operation
tích đề cart
Hoạt động sản phẩm Cartesian (được ký hiệu
bởi X) cho phép chúng tôi kết hợp thông tin từ bất
kỳ hai mối quan hệ nào.
We write the Cartesian product of relations P and Q
as P × Q

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 ))

 We get only those tuples of “instructor X teaches” that pertain


to instructors and the courses that they taught.
The result of this expression, shown in the next slide

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

Reverse of left outer join.


Retrieves all tuples of B and null values for attributes
of A in non-matching tuples of 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

Full Outer Join: A <join condition> B

Ensures that all tuples of A and B are present in


the result set

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:

Query 1: List customers whose cred_lim is greater than £500.


Query 2: List customers whose cred_lim is greater than £500
and lives in London.

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

You might also like