Acknowledgements TRC and DRC • 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 R-2

Overview Relations

• Review he Relational Model (RM) and Algebra Definition : Given sets A1, A 2, ... , An a r is a • Tuple Relational Calculus (TRC) subset of their Cartesian product. ⊆ × × × • Domain Relational Calculus (DRC) r A1 A2 ... An ∈ • r is a set of n-tuples (a1, a 2, ... , an) where ai Ai

Definition : R(A1, A 2, ..., An) is the schema of relation r.

 A1, A 2, ... , An are domains ; their names are attributes

 R is the name of the relation

• Notation: r (R) is a relation on the relation schema R.

The Relational Model R-3 The Relational Model R-4

Relations as Tables Tuples • Relations can be depicted as tables (approx.) Definition : An element t of a relation r is called a tuple . • ×(O,E) where O = {1,3} and E = {0,2} × • We refer to component values of a tuple t by t[Ai] = vi O E (the value of attribute A for tuple t). 1 0 i  Alternatively, use ‘dot’ notation, e.g., t.A is the ith attribute 1 0 1 2 i 3 0 of tuple t 3 2 3 2 • t[Ai, ... , Ak] refers to the subtuple of t containing the • <(O,E) values of attributes Ai, ... , Ak respectively. • metaphor 1 0 < O E  Tuple is a row 1 2 3 2  Attribute is a

The Relational Model R-5 The Relational Model R-6 Characteristics of Relations Characteristics of Relations (cont.) • Tuples in a relation are unorderd. • Attributes in a tuple/relation are ordered.

• Example: The following two relations have the same  But, we can change the order at will (to be shown in future).

information content.  Example: The following relations do not have the same information.

Name Age Age Name Name Age Name Age Pat 1 1 Pat Pat 1 Sue 3 Fred 2 2 Fred Fred 2 Pam 4 Sue 3 3 Sue Sue 3 Fred 2 Pam 4 4 Pam Pam 4 Pat 1

The Relational Model R-7 The Relational Model R-8

Characteristics of Relations (cont.) Degree and Cardinality • Values in a tuple are atomic (indivisible). Ord CID Date Salesperson • A value cannot be a structure, record, or relation 102 42 860207 Johnson 103 39 860211 Strong Example : All atomic values (string or integer). 104 24 860228 Boggs Name Age 105 42 860303 Strong 107 34 860308 West Tuples (or records, rows) Pat 1 [Cardinality is # of them] 108 46 860308 Wset Fred 2 110 24 860312 Boggs 111 21 860318 Strong Example : The following is not a relation, Name is not atomic 112 29 860320 Clark Name Age degree/arity First Last 1 Joe Doe

First Last 2 Sue Doe

The Relational Model R-9 The Relational Model R-10

Relational Algebra Extended • Five primary operators (complete) • Outer Joins σ  Selection:  Left: r L s  Projection: π  Right: r R s  Union: ∪  Full: r N s  Difference: – • Aggregation  Cartesian Product: × (r) G agg 1, ..., agg k • Convenient derived operators (closed)

 Intersection: ∩ n n n  Theta join θ, equijoin = , and natural join

 Semijoins: left l and right r

 Relational division: ÷

The Relational Model R-11 The Relational Model R-12 RA Operations Produce Relations Relational Completeness • All the operators can be expressed in terms of the five basic operators: σ, π, −−−,− ×××,× ∪∪∪ Name Age Name Age Name joe 4 sue 10 sue sue 10 sam 9 sam • This set is called a complete set of relational algebraic sam 9 operation1 operation2 operators.

• Any that is at least as powerful as these operators is termed (query) relationally complete . Notation: operation2( operation1( R ) )

• The algebra is closed .

The Relational Model R-13 The Relational Model R-14

Overview Relational Calculus • Relational Calculus • The relational algebra is procedural , specifying a

 Tuple relational calculus sequence of operations to derive the desired results. ◆ Safety • Relational calculus is based on first-order predicate

 Domain relational calculus calculus. ◆ Equivalence • Relational calculus is more declarative , specifying  Completeness and expressiveness what is desired. • The expressive power of the two languages is identical.

 This implies that relational calculus is relationally complete. • Many commercial relational query languages are based on the relational calculus. • The implementations are based on the relational algebra.

The Relational Model R-15 The Relational Model R-16

Relational Calculus, cont. Tuple Relational Calculus • Two forms of calculi • A tuple variable ranges over tuples of a particular relation. • Example: List the information about expensive films. { ∧  Tuple Relational Calculus (TRC) t | Film(t) t.RentalPrice > 4} • Film(t) specifies the range relation Film for the tuple variable t.  Domain Relational Calculus (DRC) • Each tuple satisfying t.RentalPrice > 4 is retrieved. • The entire tuple is retrieved. • List the titles of expensive films.

{t.Title | Film(t) ∧ t.RentalPrice > 4}

The Relational Model R-17 The Relational Model R-18 Syntax of Tuple Relational Calculus Video Store Queries, cont. {t .A , t .A , ... , t .A | P (t , t , ... , t )} • List the outrageously priced films (over $4 or 1 1 2 2 j j 1 2 j under $1).

• t , t , ... , t are tuple variables. 1 2 j {t | Film(t) ∧ (t.RentalPrice > 4 ∨ t.RentalPrice < 1)} • Each tk is an tuple of the relation over which it ranges. • List the ID numbers of the films that are expensive and have been reserved. • P is a predicate, which is made up of atoms, of the following types. {t.FilmID | Film(t) ∧ (t.RentalPrice > 4  R (tk) identifies R as the range of tk ∧ (∃r (Reserved(r) ∧ t.FilmID = r.FilmID ))}

 Atoms from predicate calculus: ∧∧∧, ∨∨∨, ¬¬¬,∀∀∀, ∃∃∃

The Relational Model R-19 The Relational Model R-20

Video Store Queries, cont. Video Store Queries, cont. • List the IDs of the expensive films that have not been • List the titles of all reserved films. reserved.

{f.Title | ∃r (Film(f) ∧ Reserved(r) ∧ f.FilmID = r.FilmID )} {t.FilmID | Film(t) ∧ (t.RentalPrice > 4 ∧ ¬∃ r (Reserved(r) ∧ t.FilmID = r.FilmID ))} • List the customers who have reserved a film.

{c.Name | ∃r (Customer(c) ∧ Reserved(r) ∧ c.CustomerID = r.CustomerID )}

The Relational Model R-21 The Relational Model R-22

Video Store Queries, cont. Video Store Queries, cont. • List the customers who have reserved expensive films. • List the customers who have reserved all the foreign films. {c.Name | ∃r ∃f (Customer(c) ∧ Reserved(r) ∧ Film(f) ∧ f.RentalPrice > 4 ∧ c.CustomerID = r.CustomerID {c.Name | (Customer(c) ∧ r.FilmID = f.FilmID )} ∧ ∀f (Film(f) ∧ f.Kind = “F” ⇒ (∃r (Reserved(r) ∧ • List the streets of customers who have reserved foreign r.FilmID = f.FilmID ∧ films. r.CustomerID = c.CustomerID ))} {c.Street | ∃r ∃f (Customer(c) ∧ Reserved(r) ∧ Film(f) ∧ f.Kind = “F” ∧ c.CustomerID = r.CustomerID ∧ r.FilmID = f.FilmID )} a ⇒ b is shorthand for ¬a ∨ b.

The Relational Model R-23 The Relational Model R-24 Video Store Queries, cont. Safety • Find the film(s) with the highest rental price. • It is possible to write tuple calculus expressions that • Alternative formulation: Find the film(s) with a rental generate infinite relations. ¬ ∈ price for which no other rental price is higher. • {t | t r} results in an infinite relation if the domain of any attribute of relation r is infinite. • We wish to ensure that an expression in relational calculus yields only a finite number of tuples. { ∧ ¬∃ f.FilmID | Film(f) f2 (Film(f2) • The domain of a tuple relational calculus expression is ∧ f2.RentalPrice > f.RentalPrice )} 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-25 The Relational Model R-26

Domain Relational Calculus Video Store Queries • Each query is an expression of the form • List the outrageously priced films (over $4 or under $1). { 〈x1, x 2, ... , xn〉 |P (x1, x 2, ... , xn)} • P is a formula similar to the formula in the predicate {〈T〉 | ∃F ∃R ∃K ( 〈F, T, R, K 〉 ∈ Film calculus. ∧ (R > 4 ∨ R < 1))} • List the information on the expensive films. • List the ID numbers of the films that are expensive and have been reserved. { 〈F, T, R, K 〉 | 〈F, T, R, K 〉 ∈ Film ∧ R > 4}

• List the titles of the expensive films. {〈F〉 | ∃T ∃R ∃K (〈F, T, R, K 〉 ∈ Film ∧ R > 4 ∧ ∃I ∃D (〈 I, F, D 〉 ∈ Reserved ))} { 〈T〉 | ∃F ∃R ∃K ( 〈F, T, R, K 〉 ∈ Film ∧ R > 4)}

The Relational Model R-27 The Relational Model R-28

Video Store Queries, cont. Video Store Queries, cont. • List the IDs of the expensive films that have not been • List the titles of all reserved films. reserved. {〈T〉 | ∃F,R,K (〈F, T, R, K 〉 ∈ Film ∧ ∃I,D (〈 I, F, D 〉 ∈ Reserved ))}

{〈F〉 | ∃T,R,K (〈F, T, R, K 〉 ∈ Film ∧ R > 4 ∧ ¬∃ I ¬∃ D (〈 I, F, D 〉 ∈ Reserved ))} • List the customers who have reserved a film.

{〈N〉 | ∃I,S,C,T (〈I, N, S, C, T 〉 ∈ Customer ∧ ∃F,D (〈I, F, D 〉 ∈ Reserved ))}

The Relational Model R-29 The Relational Model R-30 Video Store Queries, cont. Video Store Queries, cont. • List the customers who have reserved expensive films. • List the customers who have reserved all the foreign films. {〈N〉 | ∃I,S,C,T (〈I, N, S, C, T 〉 ∈ Customer ∧ ∃F,D (〈I, F, D 〉 ∈ Reserved ∧ (( ∃T,R,K (〈F, T, R, K 〉 ∈ Film ∧ R > 4)))} { ∃ ∈ • List the streets of customers who have reserved foreign 〈N〉 | I,S,C,T (〈 I, N, S, C, T 〉 Customer ∧ ∀ ∃ ∈ films. F T,R (〈F, T, R,“F”〉 Film ⇒ (∃D (〈 I, F, D 〉 ∈ Reserved ))} {〈S〉 | ∃I,N,C,T (〈I, N, S, C, T 〉 ∈ Customer ∧ ∃F,D (〈 I, F, D 〉 ∈ Reserved ∧ (( ∃T,R (〈F, T, R,“F”〉 ∈ Film )))}

The Relational Model R-31 The Relational Model R-32

Video Store Queries, cont. Summary • Find the film(s) with the highest rental price. • Relational algebra • Alternative formulation: Find the film(s) with a rental  Objects are relations (sets of n-tuples). price for which no other rental price is higher. • Tuple relational calculus

 Variables range over relations (sets of tuples).

 Each variable is associated with an individual tuple. {〈T〉 | ∃F,R,K (〈F, T, R, K 〉 ∈ Film • Domain relational calculus ∧ ¬∃ ∈ ( F2,T 2,R 2,K 2 (〈F2,T 2,R 2,K 2〉 Film  Variables range over domains (sets of values). ∧ R2 > R))}  Each variable is associated with an individual value. • Misnamed

 Tuple relational calculus and Value relational calculus

 Relational relational calculus and Domain relational calculus

The Relational Model R-33 The Relational Model R-34

Expressive Power Completeness • Theorem: The following four languages define the same class of • Theorem: The tuple relational calculus is complete. functions.

 Relational algebra expressions

 Safe relational tuple calculus formulas  Safe relational domain calculus formulas • Proof:

 Selection σ (r) ⇒ { t | t ∈ r ∧ P(t) } Proof: P • π ∈  Projection X(r) ⇒ { t[X] | t r }  relational algebra ≤ (safe) tuple relational calculus ∪ ∈ ∨ ∈ ◆ Show via induction that every relational algebra expression has a counterpart  Union r s ⇒ { t | t r t s } in the tuple relational calculus.  Difference r – s ⇒ { t | t ∈ r ∧ ¬t ∈ s }  tuple relational calculus ≤ domain relational calculus  Cartesian product r × s ⇒ { t  q | t ∈ r ∧ q ∈ s }  domain relational calculus ≤ relational algebra  Induct on length of the algebraic expression. • Corollary: All four languages are relationally complete.

The Relational Model R-35 The Relational Model R-36 Completeness, Example Completeness, Example, Cont. • List the titles of all reserved films. • Replace selection:

π σ × ∈ × Title ( Film,FilmID = Reserved.FilmID (Film Reserved)) {t.Title | t (Film Reserved ) π n ∧ Title (Film Reserved) t[Film.FilmID ] = t[Reserved.FilmID ] }

• Replace projection: • Replace the Cartesian product:

{t.Title | t ∈ σ {t.Title . | q ∈ Film ∧ r ∈ Reserved Film,FilmID = Reserved.FilmID ∧ ∧ (Film × Reserved)} q.FilmID = r.FilmID t = q  r }

The Relational Model R-37 The Relational Model R-38

Equivalence of Expressive Power • Theorem: The relational algebra is as expressive as the (safe) tuple relational calculus.

• (Informal) Proof: by induction on the number of operators in the calculus predicate

 ∃X(...) ⇒ π(...)

 X ∨ Y ⇒ π(X × Y)

 ¬P(r) ⇒ U - r σ  P(r) ⇒ P (...)

 X ∧ Y ⇒ ¬(¬ X ∨ ¬ Y)

 ∀X(P(r)) ⇒ ¬ ∃X(¬ P(r))

The Relational Model R-39