1 / 43

The Relational Algebra and Relational Calculus

The Relational Algebra and Relational Calculus. Outlines. Unary Relational Operations Relational Algebra Operations from Set Theory Binary relational Operations. Unary Relational Operations. The relational algebra is a set of operators that take and return relations

tarika
Télécharger la présentation

The Relational Algebra and Relational Calculus

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Relational Algebra and Relational Calculus Dr. Mohamed Hegazi

  2. Outlines • Unary Relational Operations • Relational Algebra Operations from Set Theory • Binary relational Operations Dr. Mohamed Hegazi

  3. Unary Relational Operations The relational algebra is a set of operators that take and return relations • Unary operations take one relation,and return one relation: • SELECT operation • PROJECT operation • Sequences of unary operations • RENAME operation Dr. Mohamed Hegazi

  4. The SELECT Operation Employee: (DNO=3) (EMPLOYEE). Dr. Mohamed Hegazi

  5. The SELECT Operation • It selects a subset of tuples from a relation that satisfy a SELECT condition. • The SELECT operation is denoted by: <Selection condition> (R) • The degree of the relation resulting from a SELECT operation is the same as that of R • For any selection-condition c, we have | <c> (R) |  | R | • The SELECT operation is commutative, that is, <c1> (<c2> ( R )) = <c2> (<c1> ( R )) Dr. Mohamed Hegazi

  6. Employee: (SEX=F) (EMPLOYEE). Dr. Mohamed Hegazi

  7. Employee: (SEX=F AND DNO=1 ) (EMPLOYEE). Dr. Mohamed Hegazi

  8. The PROJECT Operation Employee: ENAME, SALARY,DNO (EMPLOYEE) Dr. Mohamed Hegazi

  9. The PROJECT Operation • The PROJECT operation , selects certain columns from a relation and discards the columns that are not in the PROJECT list • The PROJECT operation is denoted by: <attribute list> ( R ) • Where attribute-list is a list of attributes from the relation R • The degree of the relation resulting from a PROJECT operation is equal to the number of attributes in attribute-list. • The PROJECT operation is not commutative Dr. Mohamed Hegazi

  10. Employee: ID,ENAME, BDATE,SALARY,DNO (EMPLOYEE) Dr. Mohamed Hegazi

  11. Employee: Find the name and salary of all employees who work on department number 3 (DNO=3) ENAME, SALARY((DNO=3) (EMPLOYEE)) The result : Dr. Mohamed Hegazi

  12. Relational Algebra Operations The UNION operation RESULT =RESULT1  RESULT2. Dr. Mohamed Hegazi

  13. Relational Algebra Operations • Two union-compatible relations. (b) STUDENT  INSTRUCTOR. (c) STUDENT  INSTRUCTOR. (d) STUDENT – INSTRUCTOR. • INSTRUCTOR – STUDENT Dr. Mohamed Hegazi

  14. Relational Algebra Operations • Two relations R1 and R2 are said to be union compatible if they have the same degree and all their attributes (correspondingly) have the same domain. • The UNION, INTERSECTION, and SET DIFFERENCE operations are applicable on union compatible relations • The resulting relation has the same attribute names as the first relation Dr. Mohamed Hegazi

  15. The UNION operation • The result of UNION operation on two relations, R1 and R2, is a relation, R3, that includes all tuples that are either in R1, or in R2, or in both R1 and R2. • The UNION operation is denoted by: R3 = R1 R2 • The UNION operation eliminates duplicate tuples Dr. Mohamed Hegazi

  16. The INTERSECTION operation • The result of INTERSECTION operation on two relations, R1 and R2, is a relation, R3, that includes all tuples that are in both R1 and R2. • The INTERSECTION operation is denoted by: R3 = R1 R2 • The both UNION and INTERSECTION operations are commutative and associative operations Dr. Mohamed Hegazi

  17. The SET DIFFERENCE Operation • The result of SET DIFFERENCE operation on two relations, R1 and R2, is a relation, R3, that includes all tuples that are in R1 but not in R2. • The SET DIFFERENCE operation is denoted by: R3 = R1 – R2 • The SET DIFFERENCE (or MINUS) operation is notcommutative Dr. Mohamed Hegazi

  18. Relational Algebra Operations • Two union-compatible relations. (b) STUDENT  INSTRUCTOR. (c) STUDENT  INSTRUCTOR. (d) STUDENT – INSTRUCTOR. • INSTRUCTOR – STUDENT Dr. Mohamed Hegazi

  19. Binary Relational Operations The CARTESIAN PRODUCT Operation (a) PROJ_DEPT  PROJECT × DEPT. (b) DEPT_LOCS  DEPARTMENT × DEPT_LOCATIONS. Dr. Mohamed Hegazi

  20. The CARTESIAN PRODUCT Operation • This operation (also known as CROSS PRODUCT or CROSS JOIN) denoted by: R3 = R1 R2 • The resulting relation, R3, includes all combined tuples from two relations R1 and R2 • Degree (R3) = Degree (R1) + Degree (R2) • |R3| = |R1|  |R2| Dr. Mohamed Hegazi

  21. Binary Relational Operations • An important operationfor any relational database is the JOIN operation, because it enables us to combine related tuples from two relations into single tuple • The JOIN operation is denoted by: R3 = R1⋈<join condition> R2 • The degree of resulting relation is degree(R1) + degree(R2) Dr. Mohamed Hegazi

  22. The JOIN Operation • The difference between CARTESIAN PRODUCT and JOIN is that the resulting relation from JOIN consists only those tuples that satisfy the join condition • The JOIN operation is equivalent to CARTESIAN PRODUCT and then SELECT operation on the result of CARTESIAN PRODUCT operation, if the select-condition is the same as the join condition Dr. Mohamed Hegazi

  23. JOIN operation DEPT_MGR  DEPARTMENT ⋈MGRSSN=SSN EMPLOYEE . Dr. Mohamed Hegazi

  24. The EQUIJOIN Operation • If the JOIN operation has equality comparison only (that is, = operation), then it is called an EQUIJOIN operation • In the resulting relation on an EQUIJOIN operation, we always have one or more pairs of attributes that have identical values in everytuples Dr. Mohamed Hegazi

  25. The NATURAL JOIN Operation • In EQUIJOIN operation, if the two attributes in the join condition have the same name, then in the resulting relation we will have two identical columns. In order to avoid this problem, we define the NATURAL JOIN operation • The NATURAL JOIN operation is denoted by: R3 = R1 *<attribute list> R2 • In R3 only one of the duplicate attributes from the list are kept Dr. Mohamed Hegazi

  26. Employee: Department: * EMPLOTEE ⋈ DNO=DNO DEPARTMENT Dr. Mohamed Hegazi

  27. Variations of JOIN: The EQUIJOIN and NATURAL JOIN (cont’d.) • Join selectivity • Expected size of join result divided by the maximum size nR* nS • Inner joins • Type of match and combine operation • Defined formally as a combination of CARTESIAN PRODUCT and SELECTION

  28. A Complete Set of Relational Algebra • The set of relational algebra operations {σ, π, , -, x } • Is a complete set. That is, any of the other relational algebra operations can be expressed as a sequence of operations from this set. • For example: R  S = (R  S) – ((R – S)  (S – R)) Dr. Mohamed Hegazi

  29. The DIVISION Operation • Denoted by ÷ • Example: retrieve the names of employees who work on all the projects that ‘John Smith’ works on • Apply to relations R(Z) ÷ S(X) • Attributes of R are a subset of the attributes of S

  30. Operations of Relational Algebra

  31. Operations of Relational Algebra (cont’d.)

  32. Notation for Query Trees • Query tree • Represents the input relations of query as leaf nodes of the tree • Represents the relational algebra operations as internal nodes

  33. Additional Relational Operations • Generalized projection • Allows functions of attributes to be included in the projection list • Aggregatefunctions and grouping • Common functions applied to collections of numeric values • Include SUM, AVERAGE, MAXIMUM, and MINIMUM

  34. Additional Relational Operations (cont’d.) • Group tuples by the value of some of their attributes • Apply aggregate function independently to each group

  35. Recursive Closure Operations • Operation applied to a recursiverelationship between tuples of same type

  36. OUTER JOIN Operations • Outer joins • Keep all tuples in R, or all those in S, or all those in both relations regardless of whether or not they have matching tuples in the other relation • Types • LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN • Example:

  37. The OUTER UNION Operation • Take union of tuples from two relations that have some common attributes • Not union (type) compatible • Partially compatible • All tuples from both relations included in the result • Tut tuples with the same value combination will appear only once

  38. Examples of Queriesin Relational Algebra

  39. Examples of Queriesin Relational Algebra (cont’d.)

  40. Examples of Queriesin Relational Algebra (cont’d.)

  41. Notation for Query Graphs

More Related