Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.

Similar presentations


Presentation on theme: "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus."— Presentation transcript:

1 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus

2 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 2 Chapter Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra Relational Calculus Tuple Relational Calculus Domain Relational Calculus Example Database Application (COMPANY) Overview of the QBE language (appendix D)

3 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 3 Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) The result of an operation is a new relation, which may have been formed from one or more input relations

4 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 4 Relational Algebra Overview (continued) The algebra operations thus produce new relations These can be further manipulated using operations of the same algebra A sequence of relational algebra operations forms a relational algebra expression The result of a relational algebra expression is also a relation that represents the result of a database query (or retrieval request)

5 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5 The COMPANY Database

6 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6 The following query results refer to this database state

7 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 7 Unary Relational Operations: SELECT The SELECT operation (denoted by  (sigma)) is used to select a subset of the tuples from a relation based on a selection condition. Keeps only those tuples that satisfy the qualifying condition Examples: Select the EMPLOYEEs whose department # is 4:  DNO = 4 (EMPLOYEE) Select the EMPLOYEEs whose salary > $30,000:  SALARY > 30,000 (EMPLOYEE)

8 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8 SELECT Operation Properties The SELECT operation  (R) produces a relation S that has the same schema (same attributes) as R SELECT  is commutative:  (  (R)) =  (  (R)) Because of commutativity, a sequence of SELECT operations may be applied in any order:  (  (  (R)) =  (  (  ( R))) A cascade of SELECT operations may be replaced by a single selection with a conjunction of all the conditions:  (  (  (R)) =  AND AND (R)))

9 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9 Unary Relational Operations: PROJECT PROJECT Operation is denoted by  (pi) This operation keeps certain columns (attributes) from a relation and discards the other columns. Example: To list each employee’s first and last name and salary, the following is used:  LNAME, FNAME,SALARY (EMPLOYEE)

10 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10 PROJECT (cont.) The general form of the project operation is:  (R) is the desired list of attributes from relation R. The project operation removes any duplicate tuples This is because the result of the project operation is still a relation, that is, a set of tuples

11 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 11 PROJECT Operation Properties The number of tuples in the result of projection  (R) is always less or equal to the number of tuples in R If the list of attributes includes a key of R, then the number of tuples in the result of PROJECT is equal to the number of tuples in R PROJECT is not commutative  (  (R) ) =  (R) as long as contains the attributes in

12 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 12 Examples of applying SELECT and PROJECT operations

13 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 13 Single expression versus sequence of relational operations (Example) Task: To retrieve the first name, last name, and salary of all employees who work in department number 5. Solution 1: A single relational algebra expression:  FNAME, LNAME, SALARY (  DNO=5 (EMPLOYEE)) Solution 2: a sequence of operations, giving a name to each intermediate relation: DEP5_EMPS   DNO=5 (EMPLOYEE) RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

14 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 14 Example of applying multiple operations and RENAME  LNAME, FNAME,SALARY (  Dno=5 (EMPLOYEE)) Temp =  Dno=5 (EMPLOYEE) R (First_name, Last_name, Salary) =  LNAME, FNAME,SALARY (Temp) * Notice the implied renaming

15 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 15 Important Note The Select and Project operations produce relations.  Dno  SSN = 123456789 (EMPLOYEE)) results in {(5)}. The schema of the relation is (Dno). The result relation has only one tuple. It is still a relation, not a number.

16 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 16 Unary Relational Operations: RENAME The RENAME operator is denoted by  (rho) In some cases, we may want to rename the attributes of a relation or the relation name or both Useful when a query requires multiple operations Necessary in some cases (see JOIN operation later)

17 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 17 Example of applying multiple operations and RENAME Temp =  Dno=5 (EMPLOYEE) R =  (First_name, Last_name Salary) (  LNAME, FNAME,SALARY (Temp)) * Notice the explicit renaming

18 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 18 Relational Algebra Operations from Set Theory Type Compatibility of operands is required for the binary set operation UNION , (also for INTERSECTION , and SET DIFFERENCE –, see next slides) R1(A1, A2,..., An) and R2(B1, B2,..., Bn) are type compatible if: they have the same number of attributes, and the domains of corresponding attributes are type compatible (i.e. dom(Ai)=dom(Bi) for i=1, 2,..., n). The resulting relation for R1  R2 (also for R1  R2, or R1– R2) has the same attribute names as the first operand relation R1 (by convention)

19 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 19 Example Uses of UNION To retrieve the SSNs of all employees who either work in department 5 (RESULT1 below) or directly supervise an employee who works in department 5 (RESULT2 below) We can use the UNION operation as follows: DEP5_EMPS   DNO=5 (EMPLOYEE) RESULT1   SSN (DEP5_EMPS) RESULT2(SSN)   SUPERSSN (DEP5_EMPS) RESULT  RESULT1  RESULT2 The union operation produces the tuples that are in either RESULT1 or RESULT2 or both

20 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 20 (two type compatible relations) Union IntersectionSTU - INS INS - STU Examples

21 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 21 Troubles with Attribute Names We define the domain Date (mm/dd/yyyy). R1 (Birth_date, SSN), where Birth_date is of domain Date R2 (Death_date, SSN), where Death_date is also of domain Date. R3 = R1  R2 will have the attribute list (Birth_date, SSN), which sounds weird. R3 is best named as R3(BorD_date, SSN) = R1  R2 Or more formally, R3 =  (BorD_date, SSN) (R1  R2)

22 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 22 Exercises Give the SSNs of those managers have dependent(s). Give the SSNs non-manager employees Give the SSNs of employees who are both supervisors and supervisees

23 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 23 Properties of Set Operations Notice that both union and intersection are commutative; that is R  S = S  R, and R  S = S  R Both union and intersection are associative operations; that is R  (S  T) = (R  S)  T (R  S)  T = R  (S  T) The minus operation is not commutative; that is, in general R – S ≠ S – R

24 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 24 Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT CARTESIAN (or CROSS) PRODUCT Operation This operation is used to combine tuples from two relations in a combinatorial fashion. Denoted by R(A1, A2,..., An) x S(B1, B2,..., Bm) Result is a relation Q with degree n + m attributes: Q(A1, A2,..., An, B1, B2,..., Bm), in that order. The resulting relation state has one tuple for each combination of tuples—one from R and one from S. Hence, if R has n R tuples (denoted as |R| = n R ), and S has n S tuples, then R x S will have n R * n S tuples. The two operands do NOT have to be "type compatible”

25 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 25 CARTESIAN PRODUCT (cont.) Example: To find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’ (EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES x DEPENDENT ACTUAL_DEPS   SSN=ESSN (EMP_DEPENDENTS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

26 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 26 Example of applying CARTESIAN PRODUCT

27 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 27 More Exercises List the names of those departments that have at least one female employee whose salary  $100,000. List the Names and SSNs of the managers of the above departments.

28 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 28 JOIN Operator ( ) The sequence of CARTESIAN PRODECT followed by SELECT is used quite commonly to identify and select related tuples from two relations A special operation, called JOIN combines this sequence into a single operation The general form of a join operation on two relations R(A1, A2,..., An) and S(B1, B2,..., Bm) is: R S where R and S can be any relations that result from general relational algebra expressions.

29 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 29 Exercise Use the JOIN operator to find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’ (EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES x DEPENDENT ACTUAL_DEPS   SSN=ESSN (EMP_DEPENDENTS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

30 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 30 Exercises List the names of department managers. Use Cartesian Product and Select Use Join

31 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 31 Binary Relational Operations: EQUIJOIN EQUIJOIN Operation The most common use of join involves join conditions with equality comparisons only Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple. The JOIN seen in the previous example was an EQUIJOIN.

32 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 32 NATURAL JOIN Operations Another variation of JOIN called NATURAL JOIN — denoted by * — was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. because one of each pair of attributes with identical values is superfluous The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations If this is not the case, a renaming operation is applied first.

33 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 33 Exercise Use the Natural JOIN operator to find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’ (EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

34 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 34 Results of Natural JOINs

35 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 35 NATURAL JOIN: Example 2 Because of the same attribute name Dnumber, Natural Join can be applied directly to DEPARTMENT and DEP_LOCATION DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS

36 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 36 NATURAL JOIN (contd.) Notice that all pairs of identical attribute names are compared Example: Q  R(A,B,C,D) * S(C,D,E) The implicit join condition includes each pair of attributes with the same name, “AND”ed together: R.C=S.C AND R.D=S.D Result keeps only one attribute of each such pair: Q(A,B,C,D,E)

37 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 37

38 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 38 Exercise List names and SSNs of those EMPLOYEE who work on some project(s) located in Houston.

39 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 39 Exercise List the names and birth dates of all female dependents born after 1980.

40 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 40 Exercise List the names of all employees who earns salary  $10,000 but does not have a supervisor.

41 Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 41 Exercises List the names of all employees who earns salary  $10,000 but does not supervise anyone.


Download ppt "Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus."

Similar presentations


Ads by Google