Download presentation
Presentation is loading. Please wait.
Published byTamsyn Logan Modified over 8 years ago
1
RELATIONAL ALGEBRA CHAPTER 6 1
2
LECTURE OUTLINE Unary Relational Operations: SELECT and PROJECT Relational Algebra Operations from Set Theory Binary Relational Operations: JOIN and DIVISION Query Trees 2
3
THE RELATIONAL ALGEBRA Relational algebra Basic set of operations for the relational model Similar to algebra that operates on numbers Operands and results are relations instead of numbers Relational algebra expression Composition of relational algebra operations Possible because of closure property Model for SQL Explain semantics formally Basis for implementations Fundamental to query optimization 3
4
SELECT OPERATOR 4
5
SELECT OPERATOR (CONT’D.) 5
6
SELECT OPERATOR PROPERTIES 6
7
WHAT IS THE EQUIVALENT RELATIONAL ALGEBRA EXPRESSION? Employee SELECT* FROMEmployee WHEREJobType = 'Faculty'; 7 IDNameSDeptJobType 12ChenFCSFaculty 13WangMMATHSecretary 14LinFCSTechnician 15LiuMECEFaculty
8
PROJECT OPERATOR 8
9
PROJECT OPERATOR PROPERTIES 9
10
WHAT IS THE EQUIVALENT RELATIONAL ALGEBRA EXPRESSION? Employee SELECTDISTINCT Name, S, Department FROMEmployee WHEREJobType = 'Faculty'; 10 IDNameSDeptJobType 12ChenFCSFaculty 13WangMMATHSecretary 14LinFCSTechnician 15LiuMECEFaculty
11
WORKING WITH LONG EXPRESSIONS 11
12
OPERATORS FROM SET THEORY Merge the elements of two sets in various ways Binary operators Relations must have the same types of tuples (union-compatible) UNION R ∪ S Includes all tuples that are either in R or in S or in both R and S Duplicate tuples eliminated INTERSECTION R ∩ S Includes all tuples that are in both R and S DIFFERENCE (or MINUS) R – S Includes all tuples that are in R but not in S 12
13
CROSS PRODUCT OPERATOR Binary operator aka CARTESIAN PRODUCT or CROSS JOIN R × S Attributes of result is union of attributes in operands deg(R × S) = deg(R) + deg(S) Tuples in result are all combinations of tuples in operands |R × S| = |R| * |S| Relations do not have to be union compatible Often followed by a selection that matches values of attributes What if both operands have an attribute with the same name? 13 Course deptcnuminstructorterm CS338JonesSpring CS330SmithWinter STATS330WongWinter TA namemajor AshleyCS LeeSTATS Course TA deptcnuminstructortermnamemajor CS338JonesSpringAshleyCS 330SmithWinterAshleyCS STATS330WongWinterAshleyCS 338JonesSpringLeeSTATS CS330SmithWinterLeeSTATS 330WongWinterLeeSTATS
14
RENAMING RELATIONS & ATTRIBUTES 14 Student nameyear Ashley4 Lee3 Dana1 Jo1 Jaden2 Billie3
15
JOIN OPERATOR 15
16
JOIN OPERATOR (CONT’D.) 16 Course deptcnuminstructorterm CS338JonesSpring CS330SmithWinter STATS330WongWinter TA namemajor AshleyCS LeeSTATS Course TA deptcnuminstructortermnamemajor CS338JonesSpringAshleyCS 330SmithWinterAshleyCS STATS330WongWinterLeeSTATS
17
NATURAL JOIN 17
18
NATURAL JOIN EXAMPLE Who has taken a course taught by Anderson? 18
19
Binary operator R ÷ S Attributes of S must be a subset of the attributes of R attr(R ÷ S) = attr(R) – attr(S) t tuple in (R ÷ S) iff (t × S) is a subset of R Used to answer questions involving all e.g., Which employees work on all the critical projects? Works(enum,pnum)Critical(pnum) “Inverse” of cross product DIVISION OPERATOR 19 Works enumpnum E35P10 E45P15 E35P12 E52P15 E52P17 E45P10 E35P15 Critical pnum P15 P10 Works ÷ Critical enum E45 E35 (Works ÷ Critical ) × Critical enumpnum E45P15 E45P10 E35P15 E35P10
20
REVIEW OF OPERATORS 20
21
COMPLETE SET OF OPERATIONS 21
22
NOTATION FOR QUERY TREES Representation for computation cf. arithmetic trees for arithmetic computations Leaf nodes are base relations Internal nodes are relational algebra operations 22
23
SAMPLE QUERIES 23
24
LECTURE SUMMARY Relational algebra Language for relational model of data Collection of unary and binary operators Retrieval queries only, no updates Notations Inline Sequence of assignments Operator tree 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.