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

Chapter 5 DBMS

Chapter5DBMS

Uploaded by

an
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Chapter 5 DBMS

Chapter5DBMS

Uploaded by

an
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 5

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

relational algebra. These operations enable a user to


specify basic retrieval requests as relational algebra
expressions (Query).
Example RA expression:
 (EMPLOYEE)
Dno=4 AND salary>25000
(EMPLOYEE)

 (Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>40000) (EMPLOYEE) 3


Relational Algebra Operations
Taxonomy
Relational
Algebra
Operations

Relational Database operations Set operations

Extended Set Operations Traditional Set Operations

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>

(The <condition> must involve only columns from the mentioned


table) 5
Example: SELECT operation
We can write this in Relational algebra expression:
 (table_name)
<condition> [Where σ is “Phi”]
EMPLOYEE
Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
Franklin T Wong 333445555 1955-12-08 638 Voss, Houston, TX M 40000 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

Example Display those Employee who are working in department number 5.


 (EMPLOYEE)
Dno=5
Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno
Franklin T Wong 333445555 1955-12-08 638 Voss, Houston, TX M 40000 888665555 5
Ramesh K Narayan 666884444 1962-09-15 975 Fire Oak, Humble, TX M 38000 333445555 5

Subset of EMPLOYEE relation


6
SELECT operation with comparison operators (=, <, ≤, >, ≥, ≠), and
Boolean operators (and, or, not to) form a general selection 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:

(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>40000) (EMPLOYEE)


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
Ramesh K Narayan 666884444 1962-09-15 975 Fire Oak, Humble, TX M 38000 333445555 5

Output subset tuples of relation Employee

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:

In SQL, the SELECT condition is typically specified in the


WHERE clause of a query.

For example, the following operation:



(EMPLOYEE)
Dno=4 AND salary>25000

would correspond to the following SQL query:

SQL> SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000;

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)

a. π Lname, Fname, Dno (EMPLOYEE) b. π Sex, Salary(EMPLOYEE)

Lname Fname Dno Sex Salary


Wong Franklin 5 F 43000
Wallace Jennifer 4 M 38000
Narayan Ramesh 5
Duplicate elimination

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

 would correspond to the following SQL query:


SQL> SELECT DISTINCT Sex, Salary FROM EMPLOYEE;
Sex Salary
Output with Duplicate elimination F 43000
M 38000

11
Relational Algebra Operations from Set Theory
The UNION, INTERSECTION, and MINUS Operations

 UNION, INTERSECTION, and SET DIFFERENCE (also


called MINUS or EXCEPT) set theoretic operations are used
to merge the elements of two sets.
 These are binary operations, because these are applied
to
two sets (of tuples).
 When these operations are applied to the two relations, the
relations must have the same type of tuples; this condition
has been called union compatibility or type compatibility.
 It means, R(A1, A2, ..., An) and S(B1, B2, ..., Bn) are union
compatible, if these two relations have the same number of
attributes and each corresponding pair of attributes has12the
same domain.
Copyright © 2015 Ramez Elmasri and Shamkant B. Navathe
UNION
Operation
• UNION: The result of this operation, denoted by R U S, is
a relation that includes all tuples that are either in R or in S
or in both R and S. Duplicate tuples are eliminated.
(a) Two union-compatible relations (b) STUDENT U INSTRUCTOR.

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

Cartesian product would correspond to the following SQL


query:
SQL> Select * from Student Cross Join Major;
18
Review
questions
1. Define Relational Algebra. Why do we need Relation Algebra for DBMS?
2. Illustrate diagrammatically the taxonomy (classification) of relation algebra
operations.
3. List the operations of relational algebra and the purpose of each.
4. Explain unary relation operations: SELECT and PROJECT with example.
5. Differentiate unary and binary relational algebra operations with example.
6. Explain relational algebra binary operations: UNION, INTERSECTION, SET
DIFFERENCE (or MINUS) with example.
7. Explain relational algebra binary operations: CARTESIAN PRODUCT (x)
with example.
8. What is union compatibility? Why do the UNION, INTERSECTION, and SET
DIFFERENCE operations require that the relations on which they are applied
be union compatible?

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

You might also like