Successfully reported this slideshow.
Upcoming SlideShare
×

# 2red reviewrelation

528 views

Published on

nnv

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### 2red reviewrelation

1. 1. Review of the Relational Model Acknowledgements • These slides were written by Richard T. Snodgrass (University of Arizona, SIGMOD chair), Christian S. Jensen (Aalborg University), and Curtis Dyreson (Utah State University). • Kristian Torp (Aalborg University) converted the slides from island presents to Powerpoint. CS 6800 Utah State University The Relational Model R-2Overview Sets• The Relational Model (RM) • A set is an unordered collection of distinct objects. Relations Examples Properties of relations {3, 4, a} is a set {3, 4, a} is the same set as {a, 4, 3}• Relational Algebra (RA) {4, 4} is not a set Operators x Complete set – projection, selection, Cartesian product, difference, • Set operations include union Intersection, e.g., {3, 4, a} I {b, 4} = {4} x Convenient – intersection, theta join, equijoin, natural join, semijoin, Union, e.g., {3, 4, a} U {b, 4} = {3, 4, a, b} division Difference, e.g., {3, 4, a} - {b, 4} = {3, a} Example queries Limitations• DRC and TRCThe Relational Model R-3 The Relational Model R-4
2. 2. Domains and Attributes RelationsDefinition: A domain is a set of values of some "type" Definition: Given sets A1, A2, ... , An a relation r is a Positive integers = {1, 2, 3, 4, …} subset of their Cartesian product. Alphanumeric characters = {‘a’, ‘b’, …, ‘Z’, ‘0’, …, ‘9’} r ⊆ A1 × A2 × ... × An• A common database restriction: A domain is atomic. • r is a set of n-tuples (a1, a2, ... , an) where ai ∈ Ai The following programming types are atomic domains. integer, char, float, varchar Definition: R(A1, A2, ..., An) is the schema of relation r. Structs/records are a composite domain. A1, A2, ... , An are domains; their names are attributes struct { a: int, b: char } record_definition R is the name of the relationDefinition: An attribute is the name of a domain. • Notation: r(R) is a relation on the relation schema R.The Relational Model R-5 The Relational Model R-6Example, The < Relation Relations as Tables• Let O be the domain {1, 3} and E be {0, 2}. • Relations can be depicted as tables (approx.) O × E = {(1,0), (1,2), (3,0), (3,2)} • ×(O,E) where O = {1,3} and E = {0,2} × O E 1 0 1 0 3 2 1 0 1 2 3 0 3 2 3 2• <(O,E) is the schema, O, E are attributes, {1,3} and • <(O,E) {0,2} are domains O < E = {(1,2)} ⊆ O × E 1 0 < O E 1 2 1 0 3 2 3 2The Relational Model R-7 The Relational Model R-8
3. 3. Tuples Characteristics of RelationsDefinition: An element t of a relation r is called a tuple. • Tuples in a relation are unorderd. • Example: The following two relations have the same• We refer to component values of a tuple t by t[Ai] = vi information content. (the value of attribute Ai for tuple t). Alternatively, use ‘dot’ notation, e.g., t.Ai is the ith attribute of tuple t Name Age Name Age• t[Ai, ... , Ak] refers to the subtuple of t containing the Pat 1 Sue 3 values of attributes Ai, ... , Ak respectively. Fred 2 Pam 4• Table metaphor Sue 3 Fred 2 Tuple is a row Pam 4 Pat 1 Attribute is a columnThe Relational Model R-9 The Relational Model R-10Characteristics of Relations (cont.) Characteristics of Relations (cont.)• Attributes in a tuple/relation are ordered. • Values in a tuple are atomic (indivisible). But, we can change the order at will (to be shown in future). • A value cannot be a structure, record, or relation Example: The following relations do not have the same Example: All atomic values (string or integer). information. Name Age Pat 1 Name Age Age Name Fred 2 Pat 1 1 Pat Fred 2 2 Fred Example: The following is not a relation, Name is not atomic Sue 3 3 Sue Name Age Pam 4 4 Pam First Last 1 Joe Doe First Last 2 Sue DoeThe Relational Model R-11 The Relational Model R-12
4. 4. Degree and Cardinality Relational Algebra Ord CID Date Salesperson • Five primary operators (complete) 102 42 860207 Johnson Selection: σ 103 39 860211 Strong Projection: π 104 24 860228 Boggs 105 42 860303 Strong Union: ∪ 107 34 860308 West Tuples (or records, rows) 108 46 860308 Wset [Cardinality is # of them] Difference: – 110 24 860312 Boggs Cartesian Product: × 111 21 860318 Strong 112 29 860320 Clark • Convenient derived operators (closed) degree/arity Intersection: ∩ Theta join nθ, equijoin n= , and natural join n Semijoins: left l and right r Relational division: ÷The Relational Model R-13 The Relational Model R-14RA Operations Produce Relations Selection, Formal Description σP(r) = • P is a formula in propositional calculus, dealing with terms ofName Age Name Age Name the form: joe 4 sue 10 sue attribute (or constant) = attribute (or constant) sue 10 sam 9 sam sam 9 operation1 operation2 attribute ≠ attribute attribute < attribute attribute ≤ attribute attribute ≥ attribute attribute > attributeNotation: operation2( operation1(R) ) term ∧ term (Note: ∧ is AND) term ∨ term (Note: ∨ is OR) ¬(term)The Relational Model R-15 The Relational Model R-16
5. 5. Projection, Formal Description Switching Columns πX(r) = • Assume r is the following• Let X = {Ai1, Ai2 , ... , Ain} Name Age• The result is a relation of n columns obtained by Pat 1 Fred 2 keeping the columns that are specified. Sue 3• Example A B C Pam 4 r: 1 y ω 3 x γ • To switch columns use projection 2 y β Age Name 1 Pat πA,C (r): A C πB (r): B πAge,Name (r) = 2 Fred 1 ω y 3 Sue 3 γ x 4 Pam 2 βThe Relational Model R-17 The Relational Model R-18Union, Formal Description Difference, Formal Description r∪s= r–s=• Assume r and s are union-compatible r and s have the same arity. The attributes of r and s are same over domains. • Assume r and s are union-compatible.• Example • Example A B C A B C A B C A B C r: 1 y ω s: 4 w ζ r: 1 y ω s: 4 w ζ 3 x γ 3 x γ 3 x γ 3 x γ 2 y β 2 y β r∪s: A B C A B C 1 y ω r–s: 1 y ω 3 x γ 2 y β 2 y β 4 w ζThe Relational Model R-19 The Relational Model R-20
6. 6. Cartesian Product Intersection, Formal Description r×s= r∩s= • Assume r and s are union-compatible• The schema of the resulting relation is R S • Example A B C D Ar: 1 y ω s: “Tom” 1 3 x γ “Eric” 2 A B C A B C 2 y β r: 1 y ω s: 4 w ζ 3 x γ 3 x γ r×s: A 1 B y C ω D “Tom” A 1 2 y β 3 x γ “Tom” 1 2 y β “Tom” 1 A B C 1 y ω “Eric” 2 r∩s: 3 x γ 3 x γ “Eric” 2 2 y β “Eric” 2The Relational Model R-21 The Relational Model R-22Joins Theta join, Formal Description• Joins are Cartesian products coupled with selections r nθ s = and projections.• Theta join • Example r nθ s = σθ (r × s) r: A 1 B y C ω s: D “Tom” E 3 Example: r nA < E s = σA < E (r × s) 0 x γ “Eric” 1• Equijoin 2 y β A theta join in which θ is an equality predicate A B C D E Example: r nA = E s = σA = E (r × s) r n A<E s : 1 y ω “Tom” 3• Natural join n 0 x γ “Tom” 3 0 x γ “Eric” 1• Semijoins 2 y β “Tom” 3 Left semijoin l • Equijoin Right semijoin r A theta join in which θ is an equality predicate Example: r nA = E sThe Relational Model R-23 The Relational Model R-24
7. 7. Natural Join Semijoins r n s= r ls =• Schema of result is R∪S • The result has the same schema as the left-hand• Let t be a tuple in the result. argument, r. t[R] has the same value as a tuple tR ∈ r. • Example t[S] has the same value as a tuple ts ∈ s. A B C• Example r: 1 y ω s: D “Tom” B y A B C r: 1 y ω s: D “Tom” B y 3 x γ 2 y β 3 x γ “Eric” x 2 y β rls: A B C A B C D 1 y ω rns: 1 y ω “Tom” 2 y β 3 x γ “Eric” 2 y β “Tom” • Right semijoin: r r s =The Relational Model R-25 The Relational Model R-26Relational Division Relational Division, cont. r ÷ s = { t | ∀u ∈s (t u ∈ r)} • Let r and s be relations on schemes R and S respectively, where• Example R = (A1, A2, ..., An, B1, B2, ..., Bn,) A B C S = (B1, B2, ..., Bn,) B r: 1 y ω s: y 3 x γ z 2 y β • The result of r divided by s is a relation on scheme 1 z ω R ÷ S = (A1, A2, ..., An) r÷s: A 1 C ω • R ÷ S = πR – S (r) – πR – S ((πR – S (r) × s) – r)The Relational Model R-27 The Relational Model R-28
8. 8. Renaming Operator Limitations of the Algebra• Find the film(s) with the highest rental price. • Cant do arithmetic. Find the rental price assuming a 10% increase.• We need a renaming operator: ρName • Cant do aggregates. How many films has each customer reserved?• Alternative formulation: Find the film(s) with a rental • Cant handle “missing” data. price for which no other rental price is higher. Make a list of the films, along with who reserved it, if applicable. • Cant perform transitive closure. For a partof(Part, ConstituentPart) relation, find all parts in the car door. πTitle(Film) – πF2.Title (σFilm.RentalPrice > F2.RentalPrice (Film×ρF2(Film))) • Cant sort, or print in various formats. Print a reserved summary, sorted by customer name. • Cant modify the database. Increase all \$3.25 rentals to \$3.50.The Relational Model R-29 The Relational Model R-30Extending the Projection Operator Aggregates• The generalized projection operator allows expressions AGagg1, ..., aggk (r) = in addition to column names in the subscript. t a1 ... ak | t ∈ r ∧ a1 = agg1 ({u | u ∈ r ∧ u[G] = t[G]})• Find the rental price, assuming a 10% increase. .... ak = aggk ({u | u ∈ r ∧ u[G] = t[G]}) πTitle, RentalPrice*1.1(Film) • Appends new attributes. • Each aggregate function aggi decides which attribute to aggregate over. • The grouping attributes A are optional.The Relational Model R-31 The Relational Model R-32
9. 9. Outer Joins Outer Join Example• In a regular equijoin or natural join, tuples in r or s that do not having matching tuples in the other relation do not appear in the result. A B C r: 1 y ω s: D “Tom” B y• The outer joins retain these tuples, and place nulls in the missing 3 x γ “Eric” x attributes. 2 z β “Melanie” w• Left outer join: r L s = r n s ∪ ((r – (r l s)) × (null, ..., null))• Right outer join: r N s: A B C D r R s = r n s ∪ ((null, ..., null) × (s – (s l r))) 1 y ω “Tom” 3 x γ “Eric”• Full outer join: 2 z β Null r N s = r n s ∪ ((r – (r l s)) × (null, ..., null)) Null w Null “Melanie” ∪ ((null, ..., null) × (s - (s l r)))The Relational Model R-33 The Relational Model R-34Relational Completeness Overview• All the operators can be expressed in terms of the five • Relational Calculus basic operators: σ, π, −, ×, ∪ Tuple relational calculus x Safety Domain relational calculus• This set is called a complete set of relational algebraic x Equivalence operators. Completeness and expressiveness• Any query language that is at least as powerful as these operators is termed (query) relationally complete.The Relational Model R-35 The Relational Model R-36
10. 10. Relational Calculus Relational Calculus, cont.• The relational algebra is procedural, specifying a • Two forms of calculi sequence of operations to derive the desired results.• Relational calculus is based on first-order predicate calculus.• Relational calculus is more declarative, specifying Tuple Relational Calculus (TRC) what is desired.• The expressive power of the two languages is identical. This implies that relational calculus is relationally complete. Domain Relational Calculus (DRC)• Many commercial relational query languages are based on the relational calculus.• The implementations are based on the relational algebra.The Relational Model R-37 The Relational Model R-38Tuple Relational Calculus Syntax of Tuple Relational Calculus• A tuple variable ranges over tuples of a particular {t1 .A1, t2.A2, ... , tj.Aj | P (t1, t2 , ... , tj)} relation.• Example: List the information about expensive films. • t1, t2 , ... , tj are tuple variables. {t | t ∈ Film ∧ t[RentalPrice] > 4}• t ∈ Film specifies the range relation Film for the tuple • Each tk is an tuple of the relation over which it ranges. variable t.• Each tuple satisfying t[RentalPrice] > 4 is retrieved.• The entire tuple is retrieved. • P is a predicate, which is made up of atoms, of the following types.• List the titles of expensive films. tk ∈ R or R (tk) identifies R as the range of tk {t | x ∈ Film (x[RentalPrice] = t[RentalPrice] Atoms from predicate calculus: ∧, ∨, ¬,∀, ∃ ∀ ∧ x[RentalPrice] > 4)}The Relational Model R-39 The Relational Model R-40
11. 11. Safety Domain Relational Calculus• It is possible to write tuple calculus expressions that • Each query is an expression of the form generate infinite relations. { 〈x1, x2, ... , xn〉 |P (x1, x2, ... , xn)}• {t | ¬ t ∈ r} results in an infinite relation if the domain of any attribute of relation r is infinite. • P is a formula similar to the formula in the predicate• We wish to ensure that an expression in relational calculus. calculus yields only a finite number of tuples.• The domain of a tuple relational calculus expression is the set of all values that either appear as constant values in the expression or that exist in any tuple of the relations referenced in the expression.• An expression is safe if all values in its result are from the domain of the expression.The Relational Model R-41 The Relational Model R-42Summary Expressive Power• Relational algebra • Theorem: The following four languages define the same class of Objects are relations (sets of n-tuples). functions. Relational algebra expressions• Tuple relational calculus Safe relational tuple calculus formulas Variables range over relations (sets of tuples). Safe relational domain calculus formulas Each variable is associated with an individual tuple.• Domain relational calculus • Proof: relational algebra ≤ (safe) tuple relational calculus Variables range over domains (sets of values). x Show via induction that every relational algebra expression has a counterpart Each variable is associated with an individual value. in the tuple relational calculus. tuple relational calculus ≤ domain relational calculus• Misnamed domain relational calculus ≤ relational algebra Tuple relational calculus and Value relational calculus Relational relational calculus and Domain relational calculus • Corollary: All four languages are relationally complete.The Relational Model R-43 The Relational Model R-44
12. 12. Completeness Completeness, Example• Theorem: The tuple relational calculus is complete. • List the titles of all reserved films. πTitle (σFilm,FilmID = Reserved.FilmID(Film × Reserved)) πTitle (Film n Reserved)• Proof: Selection σP(r) ⇒ { t | t ∈ r ∧ P(t) } Projection πX(r) ⇒ {t | s ∈ r (s[X] = t[X] )} • Replace projection: Union r∪s⇒{t|t∈r∨t∈s} Difference r – s ⇒ { t | t ∈ r ∧ ¬t ∈ s } Cartesian product r×s⇒ {t q|t∈r∧ q∈s} {t.Title | t ∈ σFilm,FilmID = Reserved.FilmID Induct on length of the algebraic expression. (Film × Reserved)}The Relational Model R-45 The Relational Model R-46Completeness, Example, Cont. Equivalence of Expressive Power• Replace selection: • Theorem: The relational algebra is as expressive as the (safe) tuple relational calculus. {t.Title | t ∈ (Film × Reserved) ∧ t[Film.FilmID] = t[Reserved.FilmID] } • (Informal) Proof: by induction on the number of operators in the calculus predicate• Replace the Cartesian product: {t.Title. | q ∈ Film ∧ r ∈ Reserved ∧ q.FilmID = r.FilmID ∧ t = q r }The Relational Model R-47 The Relational Model R-48