Chapter 4 - A_Relational_algebra I
Chapter 4 - A_Relational_algebra I
Relational
Algebra I
COMP3278 Introduction to
Database Management Systems
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?
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
Audio_CD DVD
name #tracks name length subtitle
Audio_CD DVD
name #tracks name length subtitle
π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|tRqS}
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
14
Cartesian product
πTutorial.date (
σCourse.name=“Introduction to Database Management Systems”( Tutorial.date
) 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)
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.
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
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
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]