dr. mohamed hegazi1 the relational algebra and relational calculus

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

Upload: andrew-paul

Post on 06-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 1

The Relational Algebra and

Relational Calculus

Page 2: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 2

• Unary Relational Operations

• Relational Algebra Operations from Set Theory

• Binary relational Operations

Outlines

Page 3: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 3

The relational algebra is a set of operators that The relational algebra is a set of operators that take and return relationstake 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

Page 4: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 4

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO1001 Ahmed 1984 Sohar M 800 1002 31002 Ali 1980 Sohar M 1100 1004 31003 Hind 1988 Muscat F 500 1002 11004 Omer 1978 Sohar M 1200 1008 11005 Salwa 1985 Sohar F 600 1002 31006 Issa 1981 Muscat M 1000 1004 11007 Noura 1986 Muscat F 500 1002 1

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO1001 Ahmed 1984 Sohar M 800 1002 31002 Ali 1980 Sohar M 1100 1004 31005 Salwa 1985 Sohar F 600 1002 3

Employee:

(DNO=3) (EMPLOYEE).

The The SELECTSELECT Operation Operation

Page 5: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 5

• 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 ))

The SELECT Operation

Page 6: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 6

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO1001 Ahmed 1984 Sohar M 800 1002 31002 Ali 1980 Sohar M 1100 1004 31003 Hind 1988 Muscat F 500 1002 11004 Omer 1978 Sohar M 1200 1008 11005 Salwa 1985 Sohar F 600 1002 31006 Issa 1981 Muscat M 1000 1004 11007 Noura 1986 Muscat F 500 1002 1

ID ENMAE BDATE

ADRESS SEX SALARY SUPID DNO

1003 Hind 1988 Muscat F 500 1002 11005 Salwa 1985 Sohar F 600 1002 31007 Noura 1986 Muscat F 500 1002 1

Employee:

(SEX=F) (EMPLOYEE).

Page 7: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 7

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO1001 Ahmed 1984 Sohar M 800 1002 31002 Ali 1980 Sohar M 1100 1004 31003 Hind 1988 Muscat F 500 1002 11004 Omer 1978 Sohar M 1200 1008 11005 Salwa 1985 Sohar F 600 1002 31006 Issa 1981 Muscat M 1000 1004 11007 Noura 1986 Muscat F 500 1002 1

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO1003 Hind 1988 Muscat F 500 1002 11007 Noura 1986 Muscat F 500 1002 1

Employee:

(SEX=F AND DNO=1 ) (EMPLOYEE).

Page 8: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 8

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO

1001 Ahmed 1984 Sohar M 800 1002 3

1002 Ali 1980 Sohar M 1100 1004 3

1003 Hind 1988 Muscat F 500 1002 1

1004 Omer 1978 Sohar M 1200 1008 1

1005 Salwa 1985 Sohar F 600 1002 3

1006 Issa 1981 Muscat M 1000 1004 1

1007 Noura 1986 Muscat F 500 1002 1

ENMAE SALARY DNO

Ahmed 800 3

Ali 1100 3

Hind 500 1

Omer 1200 1

Salwa 600 3

Issa 1000 1

Noura 500 1

Employee:

ENAME, SALARY,DNO (EMPLOYEE)

The PROJECT Operation

Page 9: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 9

• 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

The PROJECT Operation

Page 10: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 10

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO

1001 Ahmed 1984 Sohar M 800 1002 3

1002 Ali 1980 Sohar M 1100 1004 3

1003 Hind 1988 Muscat F 500 1002 1

1004 Omer 1978 Sohar M 1200 1008 1

1005 Salwa 1985 Sohar F 600 1002 3

1006 Issa 1981 Muscat M 1000 1004 1

1007 Noura 1986 Muscat F 500 1002 1

ID ENMAE BDATE SALARY DNO

1001 Ahmed 1984 800 3

1002 Ali 1980 1100 3

1003 Hind 1988 500 1

1004 Omer 1978 1200 1

1005 Salwa 1985 600 3

1006 Issa 1981 1000 1

1007 Noura 1986 500 1

Employee:

ID,ENAME, BDATE,SALARY,DNO (EMPLOYEE)

Page 11: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 11

ID ENMAE BDATE ADRESS SEX SALARY SUPID DNO

1001 Ahmed 1984 Sohar M 800 1002 3

1002 Ali 1980 Sohar M 1100 1004 3

1003 Hind 1988 Muscat F 500 1002 1

1004 Omer 1978 Sohar M 1200 1008 1

1005 Salwa 1985 Sohar F 600 1002 3

1006 Issa 1981 Muscat M 1000 1004 1

1007 Noura 1986 Muscat F 500 1002 1

ENMAE SALARY

Ahmed 800

Ali 1100

Salwa 600

Employee:

ENAME, SALARY((DNO=3) (EMPLOYEE))

Find the name and salary of all employees who work on department number 3 (DNO=3)

The result :

Page 12: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 12

RESULT =RESULT1 RESULT2.

Relational Algebra OperationsRelational Algebra OperationsThe The UNION operationUNION operation

Page 13: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 13

(a) Two union-compatible relations. (b) STUDENT INSTRUCTOR.(c) STUDENT INSTRUCTOR. (d) STUDENT – INSTRUCTOR. (e) INSTRUCTOR – STUDENT

Relational Algebra OperationsRelational Algebra Operations

Page 14: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 14

• 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

Relational Algebra Operations

Page 15: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 15

• 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

The UNION operation

Page 16: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 16

• 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

The INTERSECTION operation

Page 17: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 17

• 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 not commutative

The SET DIFFERENCE Operation

Page 18: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 18

(a) Two union-compatible relations. (b) STUDENT INSTRUCTOR.(c) STUDENT INSTRUCTOR. (d) STUDENT – INSTRUCTOR. (e) INSTRUCTOR – STUDENT

Relational Algebra OperationsRelational Algebra Operations

Page 19: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 19

(a) PROJ_DEPT PROJECT × DEPT. (b) DEPT_LOCS DEPARTMENT × DEPT_LOCATIONS.

The CARTESIAN PRODUCT OperationBinary Relational Operations

Page 20: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 20

• 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|

The The CARTESIAN PRODUCT CARTESIAN PRODUCT OperationOperation

Page 21: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 21

• 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:R3 = R1 ⋈ <join condition> R2

• The degree of resulting relation is degree(R1) + degree(R2)

Binary Relational Operations

Page 22: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 22

• 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

Page 23: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 23

DEPT_MGR DEPARTMENT ⋈ MGRSSN=SSN EMPLOYEE .

JOIN operationJOIN operation

Page 24: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 24

• 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

Page 25: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 25

• 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

The NATURAL JOIN Operation

Page 26: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 26

ID ENMAE SALARY DNO DNO DNMAE

1001 Ahmed 800 3 3 Operation

1002 Ali 1100 3 3 Operation

1003 Hind 500 1 1 Administration

1004 Omer 1200 1 1 Administration

1005 Salwa 600 3 3 Operation

1006 Issa 1000 1 1 Administration

1007 Noura 500 1 1 Administration

ID ENMAE SALARY DNO

1001 Ahmed 800 3

1002 Ali 1100 3

1003 Hind 500 1

1004 Omer 1200 1

1005 Salwa 600 3

1006 Issa 1000 1

1007 Noura 500 1

Employee:

EMPLOTEE ⋈ DNO=DNO DEPARTMENT

DNO DNMAE1 Administration2 Personal 3 Operation

Department:

*

Page 27: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

Page 28: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Dr. Mohamed Hegazi 28

• 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

Page 29: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

Page 30: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Operations of Relational Algebra

Page 31: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Operations of Relational Algebra (cont’d.)

Page 32: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

Page 33: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus
Page 34: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

Page 35: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Additional Relational Operations (cont’d.)

• Group tuples by the value of some of their attributes – Apply aggregate function independently to

each group

Page 36: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus
Page 37: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Recursive Closure Operations

• Operation applied to a recursive relationship between tuples of same type

Page 38: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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:

Page 39: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

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

Page 40: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Examples of Queriesin Relational Algebra

Page 41: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Examples of Queriesin Relational Algebra (cont’d.)

Page 42: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Examples of Queriesin Relational Algebra (cont’d.)

Page 43: Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus

Notation for Query Graphs