Download presentation
Presentation is loading. Please wait.
Published byJerome Gilmore Modified over 9 years ago
1
1 Relational Calculus Chapter 4 – Part II
2
2 Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: Relational Algebra : how to compute it, operational, Representing execution plans (inside) · Relational Calculus : what you want, declarative. Influent SQL design (outside)
3
3 Relational Calculus Calculus has variables, constants, comparison ops (,=, , . ), logical connectives ( , ) Quantifiers ( , ) Comes in two flavours: Tuple relational calculus (TRC) : Variables range over tuples. (SQL) Domain relational calculus (DRC). Variables range over domain elements. (QBE) Both formula in TRC and DRC are simple subsets of first- order logic.
4
4 Tuple Relational Calculus (TRC) Tuple variable: variable that takes on tuples of a relation schema as values. Query form: T = tuple variable p(T) = formula that describes T Result = set of all tuples t for which p(t) with T=t evaluates to be true Formula P(T) specified in First Order Logic
5
5 Find all sailors with a rating above 7 Variable S bound to a tuple in relation Sailors
6
6 TRC Formulas Formula: an atomic formula R Rel, R.a op S.b, R.a op constant, costant op R.a Or recursively defined p, p q, p q, p q, where p, q are formulas R(p(R)), where R is a tuple variable R(p(R)), where R is a tuple variable Terms. bind --- the quantifier and are bind to variable R. free --- a variable is free if no quantifier binds to it in a formula
7
7 Semantics of TRC Queries F is an atomic formula R Rel, and R is assigned a tuple in the instance of relation Rel {S|S Sailors} F is a comparison R.a op S.b, R.a op constant, or constant op R.a, and the tuples assigned to R and S have field names R.a and S.b that make the comparison true. F is of the form p, and p is not true; the form p q, and both p and q are true ; the form p q, and one of them is true ; the form p q, and q is true whenever p is true. F is of the form R(p( R )), and there is some assignment of tuples to the free variables in p ( R ), including the variable R that make the formula p( R ) true ; F is of the form R (p ( R )), and there is some assignment of tuples to the free variables in p( R ) that makes the formula p( R ) true no matter what tuple is assigned to R.
8
8 SailorsReserves TRC Example
9
9 Find the names and ages of sailors with rating > 7 P is a Tuple variable with two fields: name and age. - the only fields mentioned in P - P does not range over any relation in the query. { P | S Sailors (S.rating > 7 P.name = S.sname P.age = S.age)} nameage Lubber55.0 rusty35.0 Sailors
10
10 Find the names of the sailors who reserved boat 103 { P | S Sailors R Reserves (R.sid = S.sid R.bid = 103 P.sname = S.sname)} Retrieve all sailor tuples for which there exists a tuple in Reserves having the same value in the sid field and with bid =103 Question: How is the answer tuple looks like? (columns? Rows?) What is difference? { S | S Sailors R Reserves (R.sid = S.sid R.bid = 103)}
11
11 Find the names of the sailors who have reserved a red boat { P | S Sailors R Reserves (R.sid = S.sid P.sname = S.sname B Boats(B.bid = R.bid B.color ='red'))} Retrieve all sailor tuples S for which there exists tuples R in Reserves and B in Boats such that R.sid = S.sid B.bid = R.bid B.color ='red' { P | S Sailors R Reserves B Boats (R.sid = S.sid B.bid = R.bid B.color ='red' P.sname = S.sname)}
12
12 Find the names of the sailors who have reserved all boat Find sailors S such that for all boats B there is a Reserves tuple showing that sailor S has reserved boat B. { P | S Sailors B Boats ( R Reserves (S.sid = R.sid R.bid = B.bid P.sname = S.sname))}
13
13 Find the sailors who have reserved all red boat { P | S Sailors B Boats (B.color = 'red' ( R Reserves (S.sid = R.sid R.bid = B.bid))} Question: How to change the algebraic representation?
14
14 Find the sailors who have reserved all red boat { P | S Sailors B Boats (B.color = 'red' ( R Reserves (S.sid = R.sid R.bid = B.bid))} Logically p q is equivalent to p q (Why?) { P | S Sailors B Boats (B.color 'red' ( R Reserves (S.sid = R.sid R.bid = B.bid))} is rewritten as: to restrict attention to those red boat.
15
15 Domain Relational Calculus Query has the form: v Answer includes all tuples that make the formula be true.
16
16 DRC Formulas Atomic formula: Rel, where Rel is a relation with n variables X op Y X op constant op is one of comparison ops (,=, , . ), Or recursively defined p, p q, p q, p q, where p, q are formulas X(p(X)), where X is a domain variable X(p(X)), where X is a domain variable
17
17 Free and Bound Variables The use of quantifiers and in a formula is said to bind X. A variable that is not bound is free. Let us revisit the definition of a query: v There is an important restriction: the variables x1,..., xn that appear to the left of `|’ must be the only free variables in the formula p(...).
18
18 Find all sailors with a rating above 7 the domain variables I, N, T and A are bound to fields of the same Sailors tuple. that every tuple that satisfies T> 7 is in the answer. Question: Modify to answer: Find sailors who are older than 18 or have a rating under 9, and are called ‘Joe’. Sailors
19
19 Find sailors rating > 7 who’ve reserved boat #103 We have used as a shorthand for Note the use of to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. Reserves Sailors bidbnamecolor 101interlakered 103marinegreen Boats { | Sailors T>7 Ir,Br,D ( Reserves Ir =I )}
20
20 Find sailors rated > 7 who’ve reserved a red boat Observe how the parentheses control the scope of each quantifier’s binding. Rewrite using 'red' as constant BN = red Reserves Sailors bidbnamecolor 101interlakered 103marinegreen Boats
21
21 Find sailors who’ve reserved all boats Find all sailors I such that for each 3-tuple { B, BN, C } either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor I has reserved it. Reserves Sailors bidbnamecolor 101interlakered 103marinegreen Boats
22
22 Algebra vs. Calculus Two formal query languages are equal in the power of expressivness? Algebra Calculus?YES! Calculus Algebra? Unsafe query {S | (S Sailors)}Correct??? Safe TRC --- Dom(Q, I) -answers for Q contains only values that are in Dom(Q, I) - R(p(R)), tuple r contains only constant in Dom(Q,I) - R(p(R)), tuple r contains a constant that is not in Dom(Q,I), then p(r) is true.
23
23 Summary Algebra and safe calculus have same expressive power Relationally complete --- if a query language can express all the queries that can be expressed in relational algebra. Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.