Chapter 5 DBMS
Chapter 5 DBMS
Relational Algebra
1
Overvie
w
Chapter 5- Relational algebra
6. Introduction
6.1 Unary Relational Operations: SELECT and PROJECT
6.1.1 The SELECT operation
6.1.2 The PROJECT operation
6.2 Relational Algebra Operations from Set Theory
6.2.1 The UNION, INTERSECTION, and MINUS Operations
6.2.2 The CARTESIAN PRODUCT (CROSS PRODUCT) Operation
In-Class Exercise & Assignment
2
Relational
Algebra
In this chapter we discuss the formal languages for
the
relational model: the Relational Algebra.
Definition:
The basic set of operations for the relational model is the
Unary
SELECT, operations
PROJECT UNION,
INTERSECTION,
SET DIFFERENCE, and
CARTESIAN
JOIN Binary operations
PRODUCT OR CROSS
PRODUCT
4
Unary Relational
Operations: SELECT and
PROJECT
The SELECT Operation:
SELECT and PROJECT are unary operations
because they operate on single relation.
The SELECT operation is used to choose a subset of the
tuples from a relation that satisfies a selection condition.
The SELECT operation can also be visualized as
a
horizontal partition of the relation into two sets.
Syntax:
Select < table_name > where <condition>
Eg: Select the tuples for all employees who either work in department 4 and
make over $25,000 per year, or work in department 5 and make over
$40,000, we can specify the following SELECT operation:
Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
Franklin T Wong 333445555 1955-12-08 638 Voss, Houston, TX M 41000 888665555 5
Jennifer S Wallace 987654321 1941-06-20 291 Berry, Bellaire, TX F 43000 888665555 4
7
SELECT operation correspond to the SQL query:
8
The PROJECT
Operation
The PROJECT operation selects certain columns from the
table and discards the other columns.
If we are interested in only certain attributes of a relation,
we use the PROJECT operation to project the relation
over these attributes only.
Therefore, the result of the PROJECT operation can be
visualized as a vertical partition of the relation into two
relations.
The PROJECT operation removes any duplicate tuples,
so the result of the PROJECT operation is a set of distinct
tuples.
9
Example: PROJECT
Operation
EMPLOYEE
Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
Franklin T Wong 333445555 1955-12-08 638 Voss, Houston, TX M 38000 888665555 5
Jennifer S Wallace 987654321 1941-06-20 291 Berry, Bellaire, TX F 43000 888665555 4
Ramesh K Narayan 666884444 1962-09-15 975 Fire Oak, Humble, TX M 38000 333445555 5
[Where π is “Pie”]
Syntax : π<attribute list> (R)
10
PROJECT operation correspond to the SQL query:
In SQL, the PROJECT attribute is specified in the
list
SELECT clause of a query.
For example, the following operation:
(EMPLOYEE)
πSex, Salary
11
Relational Algebra Operations from Set Theory
The UNION, INTERSECTION, and MINUS Operations
1 4
INTERSECTION
Operation
• INTERSECTION: The result of this operation,
denoted by R ∩ S, is a relation that includes all
tuples that are in both R and S.
(C) STUDENT ∩ INSTRUCTOR.
14
SET DIFFERENCE (or MINUS) Operation:
SET DIFFERENCE (or MINUS): The result of this
operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S.
(D) STUDENT – INSTRUCTOR. (E) INSTRUCTOR – STUDENT.
15
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
CARTESIAN PRODUCT operation-also known as
CROSS PRODUCT or CROSS JOIN - which is denoted
by x.
This is also a binary set operation, but the relations on
which it is applied do not have to be union compatible.
It is used between two set of ordered pair.
In general, the CARTESIAN PRODUCT operation applied
by itself is generally meaningless.
It is mostly useful when followed by a selection that
matches values of attributes coming from the component
relations.
16
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
Let’s: R1={1,2} and R2={a, b, c}
Ordered
Then, R1 X R2 = {(1,a), (1,b), (1,c), (2,a), (2,b), set of
(2,c)} R2 X R1 = {(a,1), (a,2), (b,1), (b,2), (c,1), (c,2)}
pairs
R1 R1 X R2 ≠ R2 R2
Hence, X R1 R1 R2
a a
1 1
b b
2 2
c c
R1 X R2 R2 X R1
17
CARTESIAN PRODUCT Operation
STUDENT X MAJOR
STUDENT MAJOR SID SNAME GPA SID MAJOR
SID SNAME GPA SID MAJOR
10 ALI 3.5 10 CS
10 ALI 3.5 10 CS
10 ALI 3.5 11 IS
11 ZOYA 3.7 11 IS
10 ALI 3.5 12 CNET
12 ALEX 4.1 12 CNET
11 ZOYA 3.7 10 CS
11 ZOYA 3.7 11 IS
11 ZOYA 3.7 12 CNET
12 ALEX 4.1 10 CS
12 ALEX 4.1 11 IS
12 ALEX 4.1 12 CNET
19
Review
questions
EMPLOYEE
9. Use above relation EMPLOYEE and write the relational algebra query.
a. Retrieve the records form EMPLOYEE whose department is 4.
b. Select the tuples where salary > 40000.
c. Retrieve the records of all employee who are Male and work for department
number 5.
10. Use above relation EMPLOYEE and write the relational algebra query.
a. List each employee’s First name, Last name and salary.
b. List First name, Last name and salary of all employees who work in
department number 5.
20
Review
questions
11. Convert the given below relational algebra operation into relational query.
a. (EMPLOYEE)
Dno=4
b. Salary > 30,000 (EMPLOYEE)
(EMPLOYEE)
c. (Dno=4 AND Salary > 30,000)
d. Ssn, Fname, Bdate Salary>39000(EMPLOYEE))
(
Fname, Lname, Salary ( Dno=5(EMPLOYEE))
e.
12. Consider the following GRADEBOOK relational schema describing the data for a
grade book of a particular instructor.
CATALOG(Cno, Ctitle) STUDENTS(Sid,
Fname, Lname, Minit)
COURSES(Term, Sec_no, Cno, A, B, C, D)
ENROLLS(Sid, Term, Sec_no)
a. Retrieve the names of students enrolled in the Automata class during the fall
2009 term.
b. Retrieve the Sid values of students who have enrolled in CSc226 and
CSc227. 21
Review
questions
13. Apply the set theoretic operations UNION, INTERSECTION, MINUS on
the given tables and draw the result.
a. STUDENT U INSTRUCTURE
b. STUDENT Ո INSTRUCTURE
c. STUDENT — INSTRUCTURE
d. INSTRUCTURE — STUDENT
STUDENT
INSTRUCTURE
Fn Ln
Fname Lname
Susan Yao
John Smith
Ramesh Shah Ricardo Browne
Johnny Kohler Susan Yao
Barbara Jones Francis Johnson
Amy Ford Ramesh Shah
Jimmy Wang
Ernest Gilbert 22