Presentation is loading. Please wait.

Presentation is loading. Please wait.

603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems.

Similar presentations


Presentation on theme: "603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems."— Presentation transcript:

1 603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems

2 Relational Algebra Chapter 5 SELECTION  : Movie title yearlength inColor studioName producerC# Star Wars1977124 yes Fox 12345 Mighty Duck1971104 yes Disney 67890 Suppose we want the Set of tuples in the relation Movie(title, year, length, inColor, studioName, producerC#) that represent Fox movies at least 100 minutes long.  c (R) =  length 100 AND studioName = ‘Box’ (Movie)

3 Relational Algebra  c (R) =  length 100 AND studioName = ‘Box’ (Movie) ==> Result title yearlengthinColorstudioNameproducerC# Star Wars1977124YesFox12345

4 Relational Algebra Chapter 5 PROJECTION  :  Attribute list (R) Movie titleyearlength inColor studioName producerC# Star Wars1977124 yes Fox 12345 Mighty Duck1991104 yes Disney 67890 Wayne’s W.1992 95 yes Paramount 99999  title, year, length (Movie)

5 Relational Algebra Chapter 5 PROJECTION  :  title, year, length (Movie) Movie titleyearlength Star Wars1977124 Mighty Duck1991104 Wayne’s W.1992 95

6 Relational Algebra Chapter 5 Cartesian Product: The Cartesian Product (Cross Product) of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair to be any element of R and the second any element of S. This product is denoted by R  S.

7 Relational Algebra Cartesian Product: R(A 1, A 2, …., A m,B 1, B 2, ….B n )  R 1 (A 1, A 2, …., A m ) R 2 (B 1, B 2, ….B n ) * A tuple t exists in R for each combination of tuples t 1 from R 1 and t 2 for R 2 such that: t[A 1, A 2, …., A m ] = t 1 and t[B 1, B 2, ….B n ]= t 2 If R 1 has n 1 tuples and R 2 has n 2 tuples, then R will have n 1 * n 2 tuples

8 Relational Algebra CARTESIAN PRODUCT is a meaningless operation on its own. It can combine related tuples from two relations if followed by the appropriate SELECT operation. Example: Combine each DEPARTMENT tuple with the EMPLOYEE tuple of the manager. DEP_EMP  DEPARTMENT  EMPLOYEE DEP_MANAGER   MGRSSN=SSN (DEP_EMP)

9 Relational Algebra Chapter 5 Natural Joins: More often than we want to take the product of two relations, we find a need to join them by pairing only those tuples that match in some way. The simplest match is the natural join of two relations R and S., denoted R  S.

10 Relational Algebra Chapter 5 Combining Relational Algebra Operations to form Queries: If all we could do was to write single operations on one or two relations as queries, then relational algebra would not be as useful as it is. One can construct expressions of relational algebra by applying operators to subexpressions, using parentheses when necessary to indicate grouping of operands.

11 Relational Algebra Chapter 5 Combining Relational Algebra Operations to form Queries: Consider the Movie relation. Suppose we want to know “What are the titles and years of movies made by Fox that are at least 100 minutes long?”

12 Relational Algebra Chapter 5 One way to compute the answer to this query is the following steps: 1.Select those Movie tuples that have length  100. 2.Select those Movie tuples that have studioName = ‘Fox’. 3.Compute the intersection of (1) and (2). 4.Project the relation form (3) onto attributes title and year. See the Expression tree for a relational algebra expression on Next Slide.

13 Relational Algebra Chapter 5 Relational Algebra Expression representing the above steps:  title, year (Movie)   length >= 100 (Movie)  studioName = ‘Fox’ (Movie) MovieMovie

14 Relational Algebra Chapter 5 The two selection nodes in the expression tree corresponds to steps (1) and (2). The intersection node corresponds to step (3), and the projection node is step (4). Conventional Representation:  title, year (  length >= 100 (Movie)   studioName = ‘Fox’ ( Movie ) ) Alternatively :  title, year (  length >= 100 AND  studioName = ‘Fox’ ( Movie ) )

15 Relational Algebra Chapter 5 Linear Notation for Algebraic Expressions: Represent the previous expression tree - R(t, y, l, i, s, p) :=  length >= 100 (Movie) S(t, y, l, i, s, p) :=  studioName = ‘Fox’ ( Movie ) T(t, y, l, i, s, p) := R  S Answer (title, year) :=  t, i ( T ) => result of final step

16 Relational Algebra Chapter 5 Combining Steps: Represent the previous expression tree - R(t, y, l, i, s, p) :=  length >= 100 (Movie) S(t, y, l, i, s, p) :=  studioName = ‘Fox’ ( Movie ) Answer (title, year) :=  t, i, ( R  S ) => result of final step

17 Relational Algebra Chapter 5 Company Database: EMPLOYEE FNAME MINIT LNAME SSN SEXSALARY DNO John B Smith 123456789M300005 Franklin T Wong 333445555M400005 Alicia J Zelaya 999887777F250004 Jennifer S Wallace 987654321F430004 Ramesh K Narayan 666884444M380005 Joyce A English 453453453F250005 Ahmad V Jabbar 987987987M250004 James E Borg 888665555M550001

18 Relational Algebra Chapter 5 ` DNUMBERLOCATION 1Houston 4Stafford 5Bellaire 5Sugarland 5Houston DEPARTMENT DNAMEDNUMBERMGRSSNMGRSTRDT Carrie Fisher123 Maple St., Holl.F9/9/99 Harrison Ford789 Palm Dr., B. H.M7/7/77 DEPT_LOCTIONS

19 Relational Algebra Retrieve SSN of all employees who either work in DEP5 or directly supervise an employee who works in DEP5. Solution: Use union operation on Result1 and Result2 DEP5_EMPS   DNO = 5 (EMPLOYEE) RESULT1   SSN (DEP5_EMPS ) RESULT2(SSN)   SUPPERSSN (DEP5_EMPS ) RESULT  RESULT1  RESULT2

20 Relational Algebra Chapter 5 - Next Lecture Projection Selection Cartesian Product Natural Joins

21 Relational Algebra Complete Set of Relational Algebra Operations: All the operations discussed so far can be described as a sequence of only the operations SELECT, PROJECT, UNION, SET, DIFFERENCE, and CARTESIAN PRODUCT. Hence, the set { , , , -,  } is called a complete set of relational algebra operations.

22 Relational Algebra Any query language equivalent to these operations is called relationally complete!

23 Relational Algebra NEXT LECTURE SQL


Download ppt "603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems."

Similar presentations


Ads by Google