Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.

Similar presentations


Presentation on theme: "Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus."— Presentation transcript:

1 Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

3 Dr. Mohamed Hegazi3 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 Unary Relational Operations

4 Dr. Mohamed Hegazi4 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1005Salwa1985SoharF60010023 Employee:  (DNO=3) (EMPLOYEE). The SELECT Operation

5 Dr. Mohamed Hegazi5 It selects a subset of tuples from a relation that satisfy a SELECT condition. The SELECT operation is denoted by:  (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 |  (R) |  | R | The SELECT operation is commutative, that is,  (  ( R )) =  (  ( R )) The SELECT Operation

6 Dr. Mohamed Hegazi6 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1003Hind1988MuscatF50010021 1005Salwa1985SoharF60010023 1007Noura1986MuscatF50010021 Employee:  (SEX=F) (EMPLOYEE).

7 Dr. Mohamed Hegazi7 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1003Hind1988MuscatF50010021 1007Noura1986MuscatF50010021 Employee:  (SEX=F AND DNO=1 ) (EMPLOYEE).

8 Dr. Mohamed Hegazi8 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 ENMAESALARYDNO Ahmed8003 Ali11003 Hind5001 Omer12001 Salwa6003 Issa10001 Noura5001 Employee:  ENAME, SALARY,DNO (EMPLOYEE) The PROJECT Operation

9 Dr. Mohamed Hegazi9 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:  ( 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 The PROJECT Operation

10 Dr. Mohamed Hegazi10 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 IDENMAEBDATESALARYDNO 1001Ahmed19848003 1002Ali198011003 1003Hind19885001 1004Omer197812001 1005Salwa19856003 1006Issa198110001 1007Noura19865001 Employee:  ID,ENAME, BDATE,SALARY,DNO (EMPLOYEE)

11 Dr. Mohamed Hegazi11 IDENMAEBDATEADRESSSEXSALARYSUPIDDNO 1001Ahmed1984SoharM80010023 Ali1980SoharM110010043 1003Hind1988MuscatF50010021 1004Omer1978SoharM120010081 1005Salwa1985SoharF60010023 1006Issa1981MuscatM100010041 1007Noura1986MuscatF50010021 ENMAESALARY Ahmed800 Ali1100 Salwa600 Employee:  ENAME, SALARY (  (DNO=3) (EMPLOYEE)) Find the name and salary of all employees who work on department number 3 (DNO=3) The result :

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

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

14 Dr. Mohamed Hegazi14 Two relations R 1 and R 2 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 Relational Algebra Operations

15 Dr. Mohamed Hegazi15 The result of UNION operation on two relations, R 1 and R 2, is a relation, R 3, that includes all tuples that are either in R 1, or in R 2, or in both R 1 and R 2. The UNION operation is denoted by: R 3 = R 1  R 2 The UNION operation eliminates duplicate tuples The UNION operation

16 Dr. Mohamed Hegazi16 The result of INTERSECTION operation on two relations, R 1 and R 2, is a relation, R 3, that includes all tuples that are in both R 1 and R 2. The INTERSECTION operation is denoted by: R 3 = R 1  R 2 The both UNION and INTERSECTION operations are commutative and associative operations The INTERSECTION operation

17 Dr. Mohamed Hegazi17 The result of SET DIFFERENCE operation on two relations, R 1 and R 2, is a relation, R 3, that includes all tuples that are in R 1 but not in R 2. The SET DIFFERENCE operation is denoted by: R 3 = R 1 – R 2 The SET DIFFERENCE (or MINUS) operation is not commutative The SET DIFFERENCE Operation

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

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

20 Dr. Mohamed Hegazi20 This operation (also known as CROSS PRODUCT or CROSS JOIN) denoted by: R 3 = R 1  R 2 The resulting relation, R 3, includes all combined tuples from two relations R 1 and R 2 Degree (R 3 ) = Degree (R 1 ) + Degree (R 2 ) |R 3 | = |R 1 |  |R 2 | The CARTESIAN PRODUCT Operation

21 Dr. Mohamed Hegazi21 An important operation for 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: R 3 = R 1 ⋈ R 2 The degree of resulting relation is degree(R 1 ) + degree(R 2 ) Binary Relational Operations

22 Dr. Mohamed Hegazi22 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 The JOIN Operation

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

24 Dr. Mohamed Hegazi24 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 every tuples The EQUIJOIN Operation

25 Dr. Mohamed Hegazi25 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: R 3 = R 1 * R 2 In R 3 only one of the duplicate attributes from the list are kept The NATURAL JOIN Operation

26 Dr. Mohamed Hegazi26 IDENMAESALARYDNO DNMAE 1001Ahmed80033Operation 1002Ali110033Operation 1003Hind50011Administration 1004Omer120011Administration 1005Salwa60033Operation 1006Issa100011Administration 1007Noura50011Administration IDENMAESALARYDNO 1001Ahmed8003 1002Ali11003 1003Hind5001 1004Omer12001 1005Salwa6003 1006Issa10001 1007Noura5001 Employee: EMPLOTEE ⋈ DNO=DNO DEPARTMENT DNODNMAE 1Administration 2Personal 3Operation Department: *

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

28 Dr. Mohamed Hegazi28 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)) A Complete Set of Relational Algebra

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

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

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

36

37 Recursive Closure Operations Operation applied to a recursive relationship between tuples of same type

38 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:

39 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

40 Examples of Queries in Relational Algebra

41 Examples of Queries in Relational Algebra (cont’d.)

42

43 Notation for Query Graphs


Download ppt "Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus."

Similar presentations


Ads by Google