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

Chapter 4 - A_Relational_algebra I

sss

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 4 - A_Relational_algebra I

sss

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter 4A.

Relational
Algebra I
COMP3278 Introduction to
Database Management Systems

Department of Computer Science, The University of Hong Kong

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ For other uses, please email : [email protected]
Motivation
(Query 5 in Chapter 3B) Find the dept. names where
employees named Smith work. How does the DBMS execute
SELECT D.name this SQL query?
FROM Employees E, Works_in W, Departments D DBMS Join which two tables first?
WHERE E.name = 'Smith' AND
E.employee_id = W.employee_id AND
Which constraint is applied
Query
W.department_id = D.department_id; processer first?

Employees Works_in Departments


employee_id name salary employee_id department_id since department_id name budget
1 Jones 26000 1 1 2001-1-1 1 Toys 122000
2 Smith 28000 2 1 2002-4-1 2 Tools 239000
3 Parker 35000 2 2 2005-2-2 3 Food 100000
4 Smith 24000 3 3 2003-1-1
2
4 3 2005-1-1
Relational Algebra
Relational Algebra is similar to normal algebra (as
in 2+3*x-y), except it uses relations (tables) as
operand, and a new set of operators.
The inner, lower-level operations of a relational DBMS
are (or are similar to) relational algebra operations.

We need to know about relational algebra to


understand query execution and optimization in a
relational DBMS.

3
Section 1

Basic operators

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ for students in COMP3278
For other uses, please email : [email protected]
Basic operators
Select (σ)
Project (π)
That is to say, there
Union () should be programs (or
Set difference (-) functions) implemented in
the DBMS for each of
Cartesian product (×) these relational operators
Rename ()

5 5
Selection
σp(R) = { t | t  R  p(t) }
Example
Consider the relation Author (authorID, name, date of birth)
Select all authors called “May”.
SELECT * Query σname=“May”(Author)
processer
FROM Author Relational algebra
WHERE name = "May";
SQL
Author σname=“May”(Author)
authorID name date of birth authorID name date of birth
101 May Nov 16 101 May Nov 16
102 Bonnie Jan 15 103 May Jul 11
103 May Jul 11
104 Raymond Apr 30
6
105 Tiffany Oct 10
Projection
πA1, A2, …, Ak(R)
A copy of R with only listed attributes A1 to Ak.
Example
Consider the relation Book (bookID, title, publisher)
Report only the bookID and title of all the books.
SELECT bookID, title Query πbookID, title(Book)
processer
FROM Book; Relational algebra

SQL

Book πbookID, title(Book)


bookID title publisher bookID title
115 Stuffy doll ABC 115 Stuffy doll
116 Angel’s feather MTG 116 Angel’s feather
117 Little girl MGH 117 Little girl
7
118 Myr ABC 118 Myr
Union
RS = { t | t  R  t  S }
R and S must have the same number of attributes
and attribute domains compatible.
Example
Find the name of all products in Audio_CD and DVD tables.
SELECT name
FROM Audio_CD
UNION
Query
processer
πname (Audio_CD)  πname (DVD)
SELECT name Relational algebra
FROM DVD;
SQL

Audio_CD DVD
name #tracks name length subtitle

One Heart 14 Prince of Persia 110 English, Chinese

Miracle 14 Villon’s Wife 90 Japanese


Legend is born: Ip Man 90 Chinese 8
Union
πname (Audio_CD)  πname (DVD)
name
One Heart
Miracle
Prince of Persia
Villon’s Wife
Legend is born: Ip Man

πname (Audio_CD) πname (DVD)


name name

One Heart Prince of Persia

Miracle Villon’s Wife


Legend is born: Ip Man

Audio_CD DVD
name #tracks name length subtitle

One Heart 14 Prince of Persia 110 English, Chinese

Miracle 14 Villon’s Wife 90 Japanese


Legend is born: Ip Man 90 Chinese 9
Set difference
R-S = { t | t  R  t  S }
R and S must have the same number of attributes
and attribute domains compatible.
Example
Find the ID of the students who haven’t submitted
the assignment.
SELECT student_id
FROM Student Query
EXCEPT processer
πstudent_id(Student) – π student_id(Submit)
SELECT student_id Relational algebra
FROM Submit;
SQL
Student Submit
student_id name gender major student_id assignment_id date
123 Kit M CS 456 1 28/9
456 Yvonne F CS 789 1 25/9
789 Paul M CS
10
Set difference
πstudent_id(Student) – π student_id(Submit)
student_id
123

πstudent_id(Student) πstudent_id(Submit)
student_id student_id
123 456
456 789
789

Student Submit
student_id name gender major student_id assignment_id date
123 Kit M CS 456 1 28/9
456 Yvonne F CS 789 1 25/9
789 Paul M CS
11
Cartesian product
R×S={tq|tRqS}
No attributes with a common name in R and S.
Example
Display the date of the tutorials of the course
“Introduction to Database Management Systems”.
SELECT Tutorial.date πTutorial.date (
FROM Course, Tutorial
WHERE
σCourse.name=“Introduction to Database Management Systems”(
Query
Coruse.name=“Introduction to processer σCourse.course_id=Tutorial.course_id” (Course × Tutorial)
Database Management Systems” AND )
Course.course_id = Tutorial.course_id; )
SQL Relational algebra
Course Tutorial
course_id name tutorial_id course_id date
c1119 Data Structures and Algorithms 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
2 c0278a 15/9
12
Cartesian product
Course × Tutorial
Course.course_id Course.name Tutorial Tutorial Tutorial
.tutorial_id .course_id .date
c1119 Data Structures and Algorithms 1 c1119 5/9
c1119 Data Structures and Algorithms 1 c0278a 7/9
c1119 Data Structures and Algorithms 2 c0278a 15/9
c0278a Introduction to Database Management Systems 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
c0278a Introduction to Database Management Systems 2 c0278a 15/9

Course Tutorial
course_id name tutorial_id course_id date
c1119 Data Structures and Algorithms 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
2 c0278a 15/9
13
Cartesian product
Course × Tutorial
Course.course_id Course.name Tutorial Tutorial Tutorial
.tutorial_id .course_id .date
c1119 Data Structures and Algorithms 1 c1119 5/9
c1119 Data Structures and Algorithms 1 c0278a 7/9
c1119 Data Structures and Algorithms 2 c0278a 15/9
c0278a Introduction to Database Management Systems 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
c0278a Introduction to Database Management Systems 2 c0278a 15/9

σCourse.course_id=Tutorial.course_id (Course × Tutorial)


Course.course_id Course.name Tutorial Tutorial Tutorial
.tutorial_id .course_id .date
c1119 Data Structures and Algorithms 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
c0278a Introduction to Database Management Systems 2 c0278a 15/9

14
Cartesian product
πTutorial.date (
σCourse.name=“Introduction to Database Management Systems”( Tutorial.date

σCourse.course_id=Tutorial.course_id” (Course × Tutorial) 7/9

) 15/9

σCourse.name=“Introduction to Database Management Systems”( σCourse.course_id=Tutorial.course_id” (Course × Tutorial))


Course.course_id Course.name Tutorial Tutorial Tutorial
.tutorial_id .course_id .date
c0278a Introduction to Database Management Systems 1 c0278a 7/9
c0278a Introduction to Database Management Systems 2 c0278a 15/9

σCourse.course_id=Tutorial.course_id (Course × Tutorial)


Course.course_id Course.name Tutorial Tutorial Tutorial
.tutorial_id .course_id .date
c1119 Data Structures and Algorithms 1 c1119 5/9
c0278a Introduction to Database Management Systems 1 c0278a 7/9
c0278a Introduction to Database Management Systems 2 c0278a 15/9

15
Rename
Notation: X(E)
Rename operator allows us to name and refer to the
results of relational-algebra expressions
 X (E) returns the expression E under the name X
SELECT Tutorial.date πTutorial.date (
FROM Course, Tutorial
WHERE
σCourse.name=“Introduction to Database Management Systems”(
Coruse.name=“Introduction to Database σCourse.course_id=Tutorial.course_id” (Course × Tutorial)
Management Systems” AND )
Course.course_id = Tutorial.course_id; )
SQL Relational algebra (Without rename)

SELECT T.date πT.date (


FROM Course C, Tutorial T
WHERE
σC.name=“Introduction to Database Management Systems”(
C.name=“Introduction to Database σC.course_id=T.course_id” ( C (Course ) ×  T (Tutorial) )
Management Systems” AND )
C.course_id = T.course_id; )
SQL
Relational algebra (With Course rename to C, Tutorial rename to T) 16
Section 2

Exercises

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ for students in COMP3278
For other uses, please email : [email protected]
Question 1
Given the following relational schema:
Student (UID, name, age).
Course (CID, title).
Enroll (UID, CID) with UID referencing Student and
CID referencing Course.
*UID and CID, age are interger; name and title are varchar.

Which of the following is (are) valid Relational


Algebra expression(s)?
The left and right parts
πUID(Student)-πCID(Course) of Set difference have
to be comparable
Course-πUID(Enroll) (same number of
attributes).
18
Question 1
Given the following relational schema:
Student (UID, name, age).
Course (CID, title).
Enroll (UID, CID) with UID referencing Student and
CID referencing Course.
*UID and CID, age are interger; name and title are varchar.

Which of the following is (are) valid Relational


Algebra expression(s)?
Student and Course have different
σage<18(Student  Course) attributes.

σage<18(πUID, name(Student)) No attribute “age” for selection.


19
Question 2 SELECT employee_id,name
FROM Employee
WHERE employee_id < 100;
SQL

Find the employeeIDs and names of employees


whose employeeID < 100.
Employee
Query processer employeeID name start_date Query processer

97 May Nov 16, 1997


98 Felix Jun 30, 2003
Option 1 Option 2
99 May Sep 18, 2007
πname, employeeID(Employee)
σemployeeID<100(Employee) 100 George Jan 1, 2008
employeeID name
employeeID name start_date
97 May
97 May Nov 16, 1997
98 Felix
98 Felix Jun 30, 2003
99 May
99 May Sep 18, 2007
100 George
πname, employeeID(σemployeeID<100(Employee)) σemployeeID<100(πname, employeeID(Employee))
employeeID name employeeID name
97 May 97 May
98 Felix
Which one is better? 98 Felix
99 May 99 May
20
Question 3
(Query 4 in Chapter 3B) Find the dept. id(s) where
employees named Smith work.
Option 1
π W.department_id (σE.name=“Smith” (σE.employee_id=W.employee_id(E(Employees) × W(Works_in) ) ) )

Employees Works_in
employee_id name salary employee_id department_id since
SELECT W.department_id
1 Jones 26000 1 1 2001-1-1
FROM Employees E, Works_in W
2 Smith 28000 2 1 2002-4-1 WHERE E.name = 'Smith' AND
… … … 2 2 2005-2-2 E.employee_id = W.employee_id;
10000 … … 3 3 2003-1-1
Employees (10000 tuples) 4 3 2005-1-1
Works_in (5 tuples) 21
Question 3
(Query 4 in Chapter 3B) Find the dept. id(s) where
employees named Smith work.
Option 1
π W.department_id (σE.name=“Smith” (σE.employee_id=W.employee_id(E(Employees) × W(Works_in) ) ) )
Option 2
π W.department_id (σE.employee_id=W.employee_id(σE.name=“Smith” (E(Employees ) ) × W(Works_in )) )

Employees Works_in
employee_id name salary employee_id department_id since
SELECT W.department_id
1 Jones 26000 1 1 2001-1-1
FROM Employees E, Works_in W
2 Smith 28000 2 1 2002-4-1 WHERE E.name = 'Smith' AND
… … … 2 2 2005-2-2 E.employee_id = W.employee_id;
10000 … … 3 3 2003-1-1
Employees (10000 tuples) 4 3 2005-1-1
Works_in (5 tuples) 22
Banking example
Foreign key Foreign key

Branch Loan Borrower


branch_id name asset branch_id loan_id amount customer_id loan_id
B1 Central 7100000 B3 L1 900 C1 L3
B2 Causeway Bay 9000000 B1 L2 1500 C4 L2
B3 Aberdeen 400000 B1 L3 1000 C2 L1
B4 North Point 3700000

Foreign
Foreign key
Foreign key
key
Account Depositor Customer
account_id customer_id customer_id name address
branch_id account_id balance Foreign
A1 C1 key C1 Kit CB320
B1 A1 500
B2 A2 400 A1 C2 C2 Ben CB326
A2 C2 C3 Jolly CB311
B2 A3 900
A3 C4 C4 Yvonne CB415
B1 A4 700
A4 C4

23
Question 4
Find the customer_id of all customers who have a
loan, an account, or both in a bank.
SELECT customer_id
FROM Borrower
UNION customer_id (Borrower)   customer_id (Depositor)
SELECT customer_id
FROM Depositor

Find the customer_id of all customers who have both


a loan and an account in a bank.
SELECT customer_id
FROM Borrower customer_id (Borrower)   customer_id (Depositor)
INTERSECT
SELECT customer_id
Wait! Do we have set intersection in
FROM Depositor
relational algebra ☺?
 customer_id (Borrower) – ( customer_id (Borrower) –  customer_id (Depositor))
24
Additional operators
These operations do not add any power to relational
algebra, but simplify common queries.

Set intersection (  )
Natural join ( )
Division (  )
Assignment (  )

25
Chapter 4A.

END
COMP3278 Introduction to
Database Management Systems

Slides prepared by - Dr. Chui Chun Kit, http://www.cs.hku.hk/~ckchui/ for students in COMP3278
For other uses, please email : [email protected]

You might also like