Unformatted text preview:

Relational Algebra – Part 2Relational Algebra OperatorsSlide 3Cartesian ProductCartesian Product Exampleθ -Joinθ -Join ExampleTypes of JoinsEquijoin ExampleNatural join ExampleJoin Practice QuestionsOuter JoinsOuter Join OperatorsRight Outer Join ExampleDivision OperatorDivision ExampleDivision Operation – Example 2A Third Division ExampleOperator PrecedenceComplete Set of Relational Algebra OperatorsConclusionRelational Algebra – Part 2Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of IowaRelational Algebra OperatorsRelational Operators:Selection σProjection ΠCartesian product xJoin Union Difference -Intersection ∩Division Summarize Note that relational algebra is the foundation of ALL relational database systems. SQL gets translated into relational algebra.Relational Algebra OperatorsCartesian ProductThe Cartesian product of two relations R (of degree k1) and S (of degree k2) is:R x S = {t | t [A1,…,Ak1]  R and t [Ak1+1,…,Ak1+k2]  S}The result of R x S is a relation of degree (k1 + k2) and consists of all (k1 + k2)-tuples where each tuple is a concatenation of one tuple of R with one tuple of S.The cardinality of R x S is |R| * |S|.The Cartesian product is also known as cross product.Cartesian Product Exampleθ -JoinTheta (θ) join is a derivative of the Cartesian product. Instead of taking all combinations of tuples from R and S, we only take a subset of those tuples that match a given condition F:R F S = {t | t [A1,…,Ak1]  R and t [Ak1+1 ,…,Ak1+k2]  S and F(t) is true}whereR, S are relations, t is any tuple F(t) is a formula defined as that of selection.Note that R F S = σF(R × S).θ -Join ExampleTypes of JoinsThe θ-Join is a general join in that it allows any expression in the condition F. However, there are more specialized joins that are frequently used.A equijoin only contains the equality operator (=) in formula F.e.g. WorksOn WorksOn.pno = Proj.pno ProjA natural join over two relations R and S denoted by R * S is the equijoin of R and S over a set of attributes common to both R and S.It removes the “extra copies” of the join attributes.Normally assumes the attributes have the same name in both relations.Equijoin ExampleNatural join ExampleJoin Practice QuestionsOuter JoinsOuter joins are used in cases where performing a join "loses“ some tuples of the relations. These are called dangling tuples.There are three types of outer joins:Left outer join – R S - The output contains all tuples of R that match with tuples of S. If there is a tuple in R that matches with no tuple in S, the tuple is included in the final result and is padded with nulls for the attributes of S. Right outer join - R S - The output contains all tuples of S that match with tuples of R. If there is a tuple in S that matches with no tuple in R, the tuple is included in the final result and is padded with nulls for the attributes of R.Full outer join – R S - All tuples of R and S are included in the result whether or not they have a matching tuple in the other relation.Right Outer JoinOuter Join OperatorsLeft Outer Join JoinMatched rows using the join conditionUnmatched rows of the left tableUnmatched rows of the right tableFull outer joinRight Outer Join ExampleDivision OperatorThe division operator on relations R and S, denoted as R ÷ S produces a relation that consists of the set of tuples from R defined over the attributes C that match the combination of every tuple in S, where C is the set of attributes that are in R but not in S.For the division operation to be defined the set of attributes of S must be a subset of the attributes of R.The division operator is used when you want to determine if all combinations of a relationship are present.E.g. Return the list of employees who work on all the projects that 'John Smith' works on.Note that R ÷ S = ΠR-S(R)−ΠR-S ((ΠR-S (R)×S)− R).Division ExampleDivision Operation – Example 2Relations R, S:R  S:AB12A B12311134612RSA Third Division ExampleA BaaaaaaaaC DaabababbE11113111Relations R, S:R  S:DabE11A BaaCRSOperator PrecedenceJust like mathematical operators, the relational operators have precedence.The precedence of operators from highest to lowest is:unary operators - σ, ΠCartesian product and joins - x, *, intersection, division - ∩, ÷union and set difference - , -Parentheses can be used to changed the order of operations.Complete Set ofRelational Algebra OperatorsIt has been shown that the relational operators {σ, Π, ×, , -} form a complete set of operators.That is, any of the other operators can be derived from a combination of these 5 basic operators.Examples:Intersection - R ∩ S ≡ R  S − ((R - S)  (S - R))We have also seen how a join is a combination of a Cartesian product followed by a selection.Division operator: R(Z) ÷ S(X) where X Z and Y = Z - X:T1 ← ΠY(R)T2 ← ΠY((S × T1) -R)T ← T1 - T2ConclusionThe relational model represents data as relations which are sets of tuples. Each relational schema consists of a set of attribute names which represent a domain.The relational model has several forms of constraints to guarantee data integrity including:domain, entity integrity and referential integrity constraintsKeys are used to uniquely identify tuples in relations.Relational algebra is a set of operations for answering queries on data stored in the relational model.The 5 basic relational operators are: {σ, Π, ×, , -}.By combining relational operators, queries can be answered over the base


View Full Document

UNCA CSCI 343 - Relational Algebra

Download Relational Algebra
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Relational Algebra and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Relational Algebra 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?