Post on 01-Jan-2016
CSE 4701
Chapter 6-1
Chapter 6 6e & 7 5e : Relational Algebra – Part 2
Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department
The University of Connecticut191 Auditorium Road, Box U-155
Storrs, CT 06269-3155steve@engr.uconn.edu
http://www.engr.uconn.edu/~steve(860) 486 - 4818
A large portion of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech.
The remainder of these slides have been adapted from the AWL web site for the textbook.
CSE 4701
Chapter 6-2
Basic Relational Operations: Unary Operations
SELECT s PROJECT or P.
Binary Operations Set operations:
UNION INTERSECTION DIFFERENCE –
CARTESIAN PRODUCT JOIN operations
What is Relational Algebra?
Relational Algebra is a Procedural ParadigmYou Need to Tell What/How to Construct the Result
Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)
CSE 4701
Chapter 6-3
Relational Algebra
R S unionR S intersectionR \ S set differenceR S Cartesian
product
A1, A2, ..., An (R) projection
F (R) selection
R S natural join
R S theta-joinRS division[A1 B1,.., An Bn] rename
CSE 4701
Chapter 6-4
Selection
Selects the Tuples (Rows) From a Relation, Which Satisfy a Selection Condition
Selection Produces a Horizontal Subset of the Operand Relation
General Form F (R)
R is a Relation F is a Boolean Expression on the Attributes of R Resulting Relation Has the Same Schema as R
Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)
CSE 4701
Chapter 6-5
ENO ENAME TITLE
E1 J. Doe Elect. EngE6 L. Chu Elect. Eng.
TITLE='Elect. Eng.'(EMP)ENO ENAME TITLE
E1 J. Doe Elect. Eng.
E2 M. Smith Syst. Anal.
E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP)
Selection Example
CSE 4701
Chapter 6-6
Another Selection Example
ASCnullWBnullnull
CSE 4701
Chapter 6-7
A SELECT Condition is a Boolean Expression Form F1 Y F2 ..., Y Y Fq (Q>=1), Where Fi (I=1,…,q) are Atomic Boolean Expressions of
the Form
aqc or aqb, a, b are Attributes of R and c is a Constant. The Operator Q is one of the Arithmetic
Comparison Operators: <, >, =, <>, >=, <= The Operator Y is one of the Logical Operators: , , ¬
Nesting: ( )
Selection Condition
CSE 4701
Chapter 6-8
Extract Only Certain Columns (Attributes) Specified in an Attribute List X From a Relation R
Produces a New Relation, which is a Vertical Subset of the Operand Relation R
The Schema (Columns) of the Resulting Relation is X General Form X(R)
R is a Relation X is a Subset of the Attributes of R Over Which the
Projection is Performed Project Retrieves Specified Columns of Table/Relation
R which Includes ALL of its Rows (Tuples)
Projection
CSE 4701
Chapter 6-9
Projection Example
PNO,BUDGET(PROJ)
PNO BUDGET
P1 150000
P2 135000
P3 250000
P4 310000
P5 500000
PROJ
PNO BUDGET
P2 135000
P3 250000
P4 310000P5 500000
PNAME
P1 150000Instrumentation
Database Develop.
CAD/CAM
MaintenanceCAD/CAM
CSE 4701
Chapter 6-10
Other Projection Examples
CSE 4701
Chapter 6-11
Relational Algebra Expression Several Operations can be Combined to form a
Relational Algebra Expression (query) Example: Retrieve all Customers over age 60?
Method 1: CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) )
Method 2: Senior-CUST(C#, Addr, Age)
= CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) ) Method 3: CNAME, ADDRESS, AGE (C) where
C = s AGE>60(CUSTOMER)
CSE 4701
Chapter 6-12
ENO ENAME TITLE
E1 J. Doe Elect. Eng.
E2 M. Smith Syst. Anal.
E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP TITLE(PROJ)
Elect.EngSyst.AnalMec.EngProgrammer
TITLE
Characteristics of Projection The PROJECT Operation Eliminates Duplicate Tuples
in the Resulting Relation Why? Projection Must Maintain a Mathematical Set (No
Duplicate Elements)
CSE 4701
Chapter 6-13
Selection with Projection Example
CSE 4701
Chapter 6-14
General FormR S
where R, S are Relations Result contains Tuples from both R and S Duplications are Removed The two Operands R, S should be union-compatible
(type-compatible w.r.t Columns/Attributes) Example: “find students registered for course C1 or C3”
s#(CNO=‘C1’ (S-C))
s#(
CNO=‘C3’ (S-C))
Union
CSE 4701
Chapter 6-15
Union Compatibility Two Relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn)
are said Union-compatible If and Only If They Have The Same Number of Attributes The Domains of Corresponding Attributes are
Compatible, i.e., Dom(Ai)=dom(Bi) for I=1, 2, ..., N Names Do Not Have to be Same!
For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible
The Resulting Relation for Relational Set Operations Has the Same Attribute Names as the First Operand
Relation R1 (by Convention)
CSE 4701
Chapter 6-16
General Form
R – S
where R and S are Relations Result Contains all tuples that are in R, but not in S. R – S <> S – R Again, there Must be Compatibility
Example “Find the students who registered course C1 but not C3”
s#(CNO=‘C1’ (S-C)) –
s#(
CNO=‘C3’ (S-C))
Set Difference
CSE 4701
Chapter 6-17
General Form
R S
where R and S are Relations Result Contains all Tuples that are in R and S. R S = R – (R – S) Again, there Must be Compatibility
Example “find the students who registered for both C1 and C3”
s#(CNO=‘C1’ (S-C))
s#(
CNO=‘C3’ (S-C))
Set Intersection
CSE 4701
Chapter 6-18
Union, Difference, Intersection Examples
What are these OtherThree Result Tables?
CSE 4701
Chapter 6-19
Given Relations R of Degree k1 and Cardinality card1
S of Degree k2 and Cardinality card2
Cartesian Product
R S
is a Relation of Degree (k1+ k2) and Consists of Tuples of Degree (k1+ k2) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S
Cardinality of the Result of the Cartesian Product R S is card1 * card2
What is One Problem with Cartesian Product w.r.t. the Result Set?
Cartesian Product
CSE 4701
Chapter 6-20
Cartesian Product: Example
A B C
a1a2a3
b1b1b4
c3c5c7
FE
f1f5
e1e2
A B C E
a1a1a2a2a3a3
b1b1b1b1b4b4
c3c3c5c5c7c7
e1e2e1e2e1e2
R S
R S F
f1f5f1f5f1f5
CSE 4701
Chapter 6-21
Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R S is a relation of schema
R’(A1, …, An, B1, …, Bm). Example “Get a list containing (S#, C#) for all students who live in Storrs
but are not registered for the database course”
Cartesian Product
(S#(
city=‘Storrs’(STUDENT))
C#
(CNAME=‘Database’
(COURSE))) – S#,
C#(S-C)
CSE 4701
Chapter 6-22
Cartesian Product Example
ENO ENAME EMP.TITLE SAL.TITLE SAL
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
EMP SAL
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
CSE 4701
Chapter 6-23
General Form
R S
where R, S are Relations, F is a Boolean Expression, called a Join Condition.
A Derivative of Cartesian Product R S = (R S)
R(A1, A2, ..., Am, B1, B2, ..., Bn) is the Resulting Schema of a -Join over R1 and R2:
R1(A1, A2, ..., Am) R2 (B1, B2, ..., Bn)
Theta Join (-Join)
CSE 4701
Chapter 6-24
A -Join Condition is a Boolean Expression of the form F1 y1 F2 y2 ..., yn-1 Fq (q>=1), where
Fi (i=1,…,q) are Atomic Boolean Expressions of the form
Ai Bj, Ai, Bj are Attributes of R1 and R2 Respectively q is one of the Algorithmic Comparison Operators
=, <>, >, <. >=, <= The Operator yi (i=1,…,n-1) is Either a Logical
AND operator or a logical OR operator
-Join Condition
CSE 4701
Chapter 6-25
-Join Example
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
ENO ENAME
E1 J. Doe
M. SmithE2
E3 A. Lee
E4 J. Miller
E5 B. Casey
E6 L. Chu
E7 R. DavisE8 J. Jones
TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
SAL
40000
34000
27000
24000
34000
40000
2700034000
EMP E.TITLE=SAL.TITLE SAL
SAL.TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
CSE 4701
Chapter 6-26
Other Types of Join Equi-join (EQUIJOIN)
The Expression only Contains one or more Equality Comparisons Involving Attributes from R1 and R2
Natural Join Denoted as R S Special Equi-join of Two Relations R and S Over a
Set of Attributes Common to both R and S By Common, it means that each Join Attribute in A
has not only Compatible Domains but also the Same Name in both Relations R and S
CSE 4701
Chapter 6-27
Examples
A B C
a1a2a3
b1b1b4
c3c5c7
B E
b1b5
e1e2
R S
R R.B=S.B S
A R.B
a1a2
b1b1
C E
c3c5
e1e1
S.B
b1b1
EQUIJOIN
A R.B C E
a1a2
b1b1
c3c5
e1e1
R SNatural Join
CSE 4701
Chapter 6-28
Natural Join Natural Join Combines Relations on Attributes with the
Same Names STUDENT(S#, SN, CITY, Email) S-C(S#, C#, G)
Example Query 1: “list of students with complete course grade info”
STUDENT S-C All Natural Joins can be Expressed by a Combination of
Primitive Operators Example Query 2: “print all students info (courses taken and grades)”
S#, SN, CITY, Email, C#, G (
STUDENT.S# = S-C.S# (STUDENT S-C))
CSE 4701
Chapter 6-29
Natural Join Example
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 70000
Syst. Anal. 80000
Mech. Eng. 56000
Programmer 60000
ENO ENAME E.TITLE SAL
E1 J. Doe Elect. Eng. 70000
E2 M. Smith Syst. Anal. 80000
56000
80000
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
EMP SAL
60000E4 J. Miller Programmer
80000E5 B.Casey Syst.Anal
70000E6 L. Chu Elect.Eng
56000E7 R.Davis Mech.Eng
CSE 4701
Chapter 6-30
Another Natural Join Example
CSE 4701
Chapter 6-31
Yet Another Natural Join Example
1455
CSE 4701
Chapter 6-32
Given Relations R(T,U) of degree r S(U) of degree s
The Division of R by S, R ÷ S
Results is a Relation of Degree (rs) Consists of all (rs)-tuples t such that for all s-tuples u in S, the tuple tu is in R.
Quotient (Division)
CSE 4701
Chapter 6-33
Division Example
ENO
E3
R ÷ S
ENO PNO PNAME
E1 P1 Instrumentation 150000
BUDGET
E2 P1 Instrumentation 150000E2 P2 Database Develop. 135000E3 P1 InstrumentationE3 P4 MaintenanceE4 P2 InstrumentationE5 P2 InstrumentationE6 P4E7 P3 CAD/CAME8 P3 CAD/CAM
310000150000150000310000250000250000
R
Maintenance
150000
S
PNO PNAME BUDGET
P1 Instrumentation 150000P4 Maintenance 310000
Find the employees who work for both project P1 and project P4?
CSE 4701
Chapter 6-34
Division: Another Example “list the S# of students that have taken all of the
courses listed in S-C”
S#
(S-C)C#
(S-C)
“list the S# of students who have taken all of the courses taught by instructor Smith”
S#
(S-CC#
(Instructor=‘Smith’(C)))
CSE 4701
Chapter 6-35
Relational Algebra
Fundamental OperatorsDerivable from the fundamental operators
Selection Projection Union Difference Cartesian Product
Intersection Join, Equi-join, Natural Join
CSE 4701
Chapter 6-36
A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If Any Relational Algebra Operator in the Set Cannot
be Derived in Terms of a Sequence of Others in Set Any Relational Algebra Operator Not in the Set Can
Be Derived in Terms of a Sequence of Only the Operators in the Set
Important Concepts:The Set of Algebra Operations {S ,P , , –, } is a
Complete Set of Relational Algebra Operations Any Query Language Equivalent to These Five
Operations is Called Relationally Complete
All Relational Algebra Operations
CSE 4701
Chapter 6-37
Fundamental Operators Selection Projection Union Set Difference Cartesian Product
Relational Algebra: Summary
Form:
<Operator><Operand(s)> Result>
Relation (s) Relation
Additional Operators Join Intersection Quotient (Division)
Union Compatibility Same Degree Corresponding
Attributes Defined Over the Same Domain
CSE 4701
Chapter 6-38
Semi-Join General form: R F S
R is Target Relation, S is the Source Relation Semi-join
A Form of Join where the Result Contains only those Tuples of the Target Relation, which Participate in the Join with the Source Relation
Benefit: Decreases the Number of Tuples that need to be Handled in the Join
R F S = (R FS)
= (R) F(S) = R F(S) where A is a set of Attributes of R, B is a set of
attributes of S
CSE 4701
Chapter 6-39
Semi-Join Example
B C D
b2b2b3b4b5b6b7b8b9b10
S
A B
R
b1b1b3b4
a1a2a3a4. R
BSALR ( P B S)=
c1c1c1c2c2c2c3c3c4c4
d9d8d7d9d8d7d5d4d4d9
A B
b3b4
a3a4.
CSE 4701
Chapter 6-40
Additional DB Operations
Additional Operations, Needed for Database Applications, but were Not Part of the Original Relational Algebra, Include: OUTER JOIN OUTER UNION Aggregate Functions and Grouping
CSE 4701
Chapter 6-41
Definition LEFT OUTER JOIN: R1 F R2 lets every Tuple in
R1 Appear in the result. RIGHT OUTER JOIN: R1 F R2 lets every Tuple
in R2 Appear in the result. FULL OUTER JOIN: R1 F R2 lets every Tuple in
R1 or R2 Appear in the result Examples - See Textbook Chapter 7
Outer Joins
CSE 4701
Chapter 6-42
Commonly Used Aggregate Functions: SUM, COUNT, AVERAGE, MIN, MAX They Often Applied to sets of Values or sets of
Tuples in Database Applications
[<grouping attributes> ] f <function list> (R) The Grouping Attributes are Optional
Aggregate Functions
CSE 4701
Chapter 6-43
Example 1: Retrieve the average salary of all employees (no grouping):
R(AVG_SAL) = s AVG(SALARY) (EMPLOYEE) Example 2:
For each department, retrieve the department number, the number of employees, and the average salary (in the department):
R(D#,Num_E,AVG_SAL)
= GROUPDNO( s DNO, COUNT(ENO), AVG(SALARY)(EMPLOYEE)) DNO is called the grouping attribute in this example
Examples of Aggregate Functions
CSE 4701
Chapter 6-44
Concluding Remarks
What have we Seen in Chapter 7? Basic Concepts of Relational Model Including
Relation/Table, Tuple/Row, Attribute/Column, Domain/Attribute Value
Concept of SK, CK, PK, and FK for Identification and Referential Integrity
Integrity Constraints as they Relate to Referential Dependencies Check for Modification Operations
Relational Algebra Operations that Allow Querying via Selection, Project, Join, and Other Set Operations
Overall, Relational Theory is Basis for SQL, Normal Forms, ER-Relational Translation, etc.