1 / 21

Relational Algebra – Part 2

Relational Algebra – Part 2. Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa. Relational Algebra Operators. Relational Operators: Selection σ Projection Π Cartesian product x Join Union  Difference - Intersection ∩

jase
Download Presentation

Relational Algebra – Part 2

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. Relational Algebra – Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa

  2. Relational 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.

  3. Relational Algebra Operators

  4. Cartesian 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.

  5. Cartesian Product Example

  6. θ -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 FS = {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).

  7. θ -Join Example

  8. Types 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.pnoProj • 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.

  9. Equijoin Example

  10. Natural join Example

  11. Join Practice Questions

  12. Outer 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.

  13. Outer Join Operators Full outer join Left Outer Join Right Outer Join Join Unmatched rows of the left table Matched rows using the join condition Unmatched rows of the right table

  14. Right Outer Join Example

  15. Division 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).

  16. Division Example

  17. Division Operation – Example 2 • Relations R, S: A B B            1 2 3 1 1 1 3 4 6 1 2 1 2 S • R S: A R  

  18. A Third Division Example • Relations R, S: A B C D E D E         a a a a a a a a         a a b a b a b b 1 1 1 1 3 1 1 1 a b 1 1 S R • R S: A B C   a a  

  19. Operator 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.

  20. 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 - T2

  21. Conclusion • 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 relations.

More Related