440 likes | 584 Views
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
E N D
The Relational Algebra and Relational Calculus Dr. Mohamed Hegazi
Outlines • Unary Relational Operations • Relational Algebra Operations from Set Theory • Binary relational Operations Dr. Mohamed Hegazi
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
The SELECT Operation Employee: (DNO=3) (EMPLOYEE). Dr. Mohamed Hegazi
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
Employee: (SEX=F) (EMPLOYEE). Dr. Mohamed Hegazi
Employee: (SEX=F AND DNO=1 ) (EMPLOYEE). Dr. Mohamed Hegazi
The PROJECT Operation Employee: ENAME, SALARY,DNO (EMPLOYEE) Dr. Mohamed Hegazi
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
Employee: ID,ENAME, BDATE,SALARY,DNO (EMPLOYEE) Dr. Mohamed Hegazi
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
Relational Algebra Operations The UNION operation RESULT =RESULT1 RESULT2. Dr. Mohamed Hegazi
Relational Algebra Operations • Two union-compatible relations. (b) STUDENT INSTRUCTOR. (c) STUDENT INSTRUCTOR. (d) STUDENT – INSTRUCTOR. • INSTRUCTOR – STUDENT Dr. Mohamed Hegazi
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
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
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
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
Relational Algebra Operations • Two union-compatible relations. (b) STUDENT INSTRUCTOR. (c) STUDENT INSTRUCTOR. (d) STUDENT – INSTRUCTOR. • INSTRUCTOR – STUDENT Dr. Mohamed Hegazi
Binary Relational Operations The CARTESIAN PRODUCT Operation (a) PROJ_DEPT PROJECT × DEPT. (b) DEPT_LOCS DEPARTMENT × DEPT_LOCATIONS. Dr. Mohamed Hegazi
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
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
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
JOIN operation DEPT_MGR DEPARTMENT ⋈MGRSSN=SSN EMPLOYEE . Dr. Mohamed Hegazi
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
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
Employee: Department: * EMPLOTEE ⋈ DNO=DNO DEPARTMENT Dr. Mohamed Hegazi
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
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
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
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
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
Additional Relational Operations (cont’d.) • Group tuples by the value of some of their attributes • Apply aggregate function independently to each group
Recursive Closure Operations • Operation applied to a recursiverelationship between tuples of same type
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:
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