Title: Relational Calculus
1Relational Calculus
?
- CS 186, Fall 2003, Lecture 6
- RG, Chapter 4
?
We will occasionally use this arrow notation
unless there is danger of no confusion. Ronald
Graham Elements of Ramsey Theory
2Administrivia
- There were some typos in the MRU portion of the
HW1 assignment, it has been corrected. - Part 1 of HW1 due Sunday
- Part 2 due in 1 week.
3Review The Big Picture
- Databases are cool
- Theory data modelling, relational algebra
- Practical Buffer Pools Management, File Indexing
- Today
- Theory Relational Calculus
- Practical SQL (time permitting)
- Next Week
- Practical SQL, Query Processing
4Review Query Languages
- High level query langs important aspect of DBMSs
- Less work for user asking query
- More opportunities for optimization
- Already discussed SQL, Relational Algebra
- Rel. Algebra theoretical foundation for SQL
- Higher level than programming language
- but still must specify steps to get desired
result - Relational Calculus higher level language
- Formal foundation for Query-by-Example
- Only specify desired result, not how to get it
- A first-order logic description of desired result
5Today Relational Calculus
- High-level, first-order logic description
- English
- Find all sailors with a rating above 7
- More logical English
- From the universe of all things, find me the set
of things that happen to be tuples in the Sailors
relation and whose rating field is a number
greater than 7. - Relational Calculus (TRC)
- S S ?Sailors ? S.rating gt 7
6So what is Relational Calculus?
- A formal, logical description, of what you want
from the database.
7Relational Calculus
- Comes in two flavors
- Tuple relational calculus (TRC)
- Domain relational calculus (DRC)
- English Example Find all sailors with a rating
above 7 - Tuple R.C. From the universe of all things, find
me the set of things that happen to be tuples in
the Sailors relation and whose rating field is a
number greater than 7. - S S ?Sailors ? S.rating gt 7
- Domain R.C. From the universe of all things,
find me S, N, R, and A, where S is an integer, N
is a string, R is an integer greater than 7, and
A is a floating point number, and ltS, N, R, Agt is
a tuple in the Sailors relation. - ltS,N,R,Agt ltS,N,R,Agt ?Sailors ? R gt 7
8Relational Calculus (cont.)
- Calculus has
- variables
- TRC Variables range over (i.e., get bound to)
tuples. Like SQL. - DRC Variables range over domain elements (
field values). Like Query-By-Example (QBE) - constants, e.g.7, Foo, 3.14159, etc.
- comparison ops, e.g. , ltgt, lt, gt, etc.
- logical connectives
- ? - not
- ? and
- - or
- - implies
- ? - is a member of
- quantifiers
- ?X(p(X)) p(X) must be true for every X
- ?X(p(X)) p(X) is true for some X
- Both TRC and DRC are simple subsets of
first-order logic. Well focus on TRC here
9Tuple Relational Calculus
- Query has the form T p(T)
- p(T) denotes a formula in which tuple variable T
appears. - Answer is the set of all tuples T for which the
formula p(T) evaluates to true. - Formula is recursively defined
- start with simple atomic formulas (get tuples
from relations or make comparisons of values) - build bigger and better formulas using the
logical connectives.
10TRC Formulas
- An Atomic formula is one of the following
- R ? Rel
- R.a op S.b
- R.a op constant
- op is one of
- A formula can be
- an atomic formula
- where p and q are
formulas - where variable R is a tuple
variable - where variable R is a tuple
variable
11Free and Bound Variables
- The use of quantifiers ?X and ?X in a formula
is said to bind X in the formula. - A variable that is not bound is free.
- Let us revisit the definition of a query
- T p(T)
- There is an important restriction
- the variable T that appears to the left of
must be the only free variable in the formula
p(T). - in other words, all other tuple variables must be
bound using a quantifier.
12Use of ?
- ?x (P(x)) - is only true if P(x) is true for
every x in the universe - Usually
- ?x ((x ? Boats) ? (x.color Red)
- ? logical implication,
- a ? b means that if a is true, b must be true
- a ? b is the same as ?a ? b
13a ? b is the same as ?a ? b
b
- If a is true, b must be true!
- If a is true and b is false, the implication
evaluates to false. - If a is not true, we dont care about b
- The expression is always true.
T F
T
F
T
a
T
T
F
14Quantifier Shortcuts
- ?x ((x ? Boats) ? (x.color Red))
- can also be written as
- ?x ? Boats(x.color Red)
- ?x ((x ? Boats) ? (x.color Red))
- can also be written as
- ?x ? Boats(x.color Red)
15Selection and Projection
- Find all sailors with rating above 7
- Find names and ages of sailors with rating above
7. - Note S is a tuple variable of 2 fields (i.e.
S is a projection of Sailors) - only 2 fields are ever mentioned and S is never
used to range over any relations in the query.
S S ?Sailors ? S.rating gt 7
S ?S1 ?Sailors(S1.rating gt 7
? S.sname S1.sname
? S.age S1.age)
16Joins
- Find sailors rated gt 7 whove reserved boat 103
- Note the use of ? to find a tuple in Reserves
that joins with the Sailors tuple under
consideration.
S S?Sailors ? S.rating gt 7 ?
?R(R?Reserves ? R.sid S.sid ?
R.bid 103)
17Joins (continued)
S S?Sailors ? S.rating gt 7 ?
?R(R?Reserves ? R.sid S.sid ?
?B(B?Boats ? B.bid R.bid
? B.color red))
S S?Sailors ? S.rating gt 7 ?
?R(R?Reserves ? R.sid S.sid ?
R.bid 103)
Find sailors rated gt 7 whove reserved a red boat
Find sailors rated gt 7 whove reserved boat 103
- Observe how the parentheses control the scope of
each quantifiers binding. - This may look cumbersome, but its not so
different from SQL!
18Division (makes more sense here???)
Find sailors whove reserved all boats (hint,
use ?)
S S?Sailors ? ?B?Boats (?R?Reserves
(S.sid R.sid
? B.bid R.bid))
- Find all sailors S such that for all tuples B in
Boats there is a tuple in Reserves showing that
sailor S has reserved B.
19Exercises
- Find the names of sailors whove reserved boat
103
N ?S ?Sailors (S.name N.name ?
?R ? Reserves(S.sid R.sid
? R.bid 103))
20Exercises
- Find the names of sailors whove reserved any red
boat
N ?S ?Sailors (S.name N.name ? ?R ?
Reserves (S.sid R.sid ? ?B ?
Boats(B.color Red ? B.bid R.bid)))
21Exercises
- Find sailors whove reserved a red boat or a
green boat
S S ?Sailors ? (?R ? Reserves (S.sid
R.sid ? ?B ? Boats(B.bid R.bid ?
(B.color Red ? B.color Green
))))
22Exercises
- Find sailors whove reserved a red boat and a
green boat
S S ?Sailors ? ((?R ? Reserves (S.sid
R.sid ? ?B ? Boats(B.color Red ?
B.bid R.bid))) ? (?R ? Reserves
(S.sid R.sid ? ?B ? Boats(B.color
Green ? B.bid R.bid))))
23Exercises
- Find sailors whove reserved all red boats
S S?Sailors ? ?B ? Boats(B.color
red ? ?R(R?Reserves ? S.sid
R.sid ? B.bid R.bid))
S S?Sailors ? ?B ? Boats (B.color ?
red ? ?R(R?Reserves ? S.sid
R.sid ? B.bid R.bid))
24Unsafe Queries, Expressive Power
- ? syntactically correct calculus queries that
have an infinite number of answers! Unsafe
queries. - e.g.,
- Solution???? Dont do that!
- Expressive Power (Theorem due to Codd)
- every query that can be expressed in relational
algebra can be expressed as a safe query in DRC /
TRC the converse is also true. - Relational Completeness Query language (e.g.,
SQL) can express every query that is expressible
in relational algebra/calculus. (actually, SQL
is more powerful, as we will see)
25Summary
- The relational model has rigorously defined query
languages simple and powerful. - Relational algebra is more operational
- useful as internal representation for query
evaluation plans. - Relational calculus is non-operational
- users define queries in terms of what they want,
not in terms of how to compute it. (Declarative) - Several ways of expressing a given query
- a query optimizer should choose the most
efficient version. - Algebra and safe calculus have same expressive
power - leads to the notion of relational completeness.