Professional Documents
Culture Documents
Relational Calculus: We Will Occasionally Use This Arrow Notation Unless There Is Danger of No Confusion
Relational Calculus: We Will Occasionally Use This Arrow Notation Unless There Is Danger of No Confusion
Administrivia
Homework 1 due in 1 week
Thursday, Feb 8 10 p.m.
New syllabus on web site
Questions?
Review
Database Systems have both theory and practice
Its a systems course, so we are heavy on the
practice
But our practice has to have theory to back it up
8-)
so we will be looking at both of them in parallel
Practice
Query Optimization
and Execution
Relational Operators
Relational Algebra
Relational Model
Lecture
5
Lectures 3
&4
Lecture
2
DB
Practice
Today
Query Optimization
and Execution
Relational Operators
Relational Algebra
Relational Model
Lecture
5
Lectures 3
&4
Lecture
2
DB
SQL
On
Deck:
Practical
ways of
evaluati
ng SQL
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
DB
Relational Calculus
Formal foundation for Query-by-Example
A first-order logic description of desired result
Only specify desired result, not how to get it
bid
101
103
Sailors
day
10/10/96
11/12/96
Basic operations:
Selection ( )
Projection ( )
Cross-product ( )
Set-difference ( )
Union ( )
bid
101
102
103
104
: gives a subset of rows.
: deletes unwanted columns.
: combine two relations.
Prediction:
These
: tuples in relation
1, but
not 2
relational
operators
: tuples in relation
1 and
2.
Join (
)
Division ( / )
Boats
bname
Interlake
Interlake
Clipper
Marine
color
Blue
Red
Green
Red
Query Optimization
and Execution
Relational Operators
Files and Access Methods
Buffer Management
Disk Space Management
:like but only keep tuples where common fields are equal.
:tuples from relation 1 with matches in relation 2 DB
bid
101
103
Sailors
day
10/10/96
11/12/96
Basic operations:
Selection ( )
Projection ( ) Find
Cross-product ( )
Set-difference ( )
Union ( )
Additional operations:
Intersection ()
Join (
)
Division ( / )
Boats
bid
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
color
Blue
Red
Green
Red
( sname ( ( color=GreenBoats)
Reserves)
)Sailors)
bid
101
103
Sailors
Boats
day
10/10/96
11/12/96
bid
101
102
103
104
bname
Interlake
Interlake
Clipper
Marine
color
Blue
Red
Green
Red
Or better yet:
Find names of sailors whove reserved a green boat
(
( sname
( sid
( bid
( color=GreenBoats)
Reserves)
)
Sailors)
)
Intermission
Some algebra exercises for you to practice with
are out on the class web site
Algebra and calculus exercises make for good
exam questions!
Constants
7, Foo, 3.14159, etc.
Comparison operators
=, <>, <, >, etc.
Logical connectives
- not
and
- or
- implies
- is a member of
Quantifiers
From all that is, find me the set of things that are tuples in the
Sailors relation and whose rating field is greater than 7.
Domain R.C.:
TRC Formulas
An atomic formula is one of the following:
R Rel
R.a op S.b
R.a op constant, where
op is one of, , , ,,
A formula can be:
an atomic formula
p, p q, p q where p and q are formulas
R( p(R)) where variable R is a tuple variable
R( p(R)) where variable R is a tuple variable
(For
every)
Use of
x (P(x)):
only true if P(x) is true for every x in the
universe:
e.g. x ((x.color = Red)
means everything that exists is red
is a logical implication
a b means that if a is true, b must be true
a b is the same as a b
a b is the same as a
b
If a is true, b must be
b
T
a
true!
If a is true and b is
false, the
expression
evaluates to false.
If a is not true, we
dont care about b
The expression is
always true.
Quantifier Shortcuts
x ((x Boats) (x.color = Red))
For every x in the Boats relation, the color must be
Red.
Selection and
Projection
S1
Selection
Find all sailors with rating above 8 S1
S1
sid
28
31
44
58
Projection
Find names and ages of sailors with rating above 8.
S
{S | S1 Sailors(S1.rating > 8
S.sname = S1.sname
S
S.age = S1.age)}
snam
e
age
yuppy
35.0
rusty
35.0
Joins
Find sailors rated > 7 whove
reserved boat #103
S
S
S
R
R
sid
22
58
bid
101
103
day
10/10/96
11/12/96
Joins (continued)
What does this expression compute?
Division
Recall the algebra expression A/B
A value x in A is disqualified if by attaching a y value from B, we obtain an xy
tuple that is not in A. (e.g: only give me A tuples that have a match in B.
{S | SSailors
BBoats (RReserves
Find all sailors S such that
(S.sid = R.sid
For all tuples B in Boats
B.bid = R.bid))}
There is at least one tuple in Reserves
e.g.,
S| S Sailors
Relational Completeness
means
Theory
Relational Calculus
Practice
Query Optimization
and Execution
Relational Operators
Relational Algebra
Relational Model
DB
SQL
DB
Summary
The relational model has rigorously defined query
languages that are simple and powerful.
Algebra and safe calculus have same expressive power
Relational algebra is more operational
useful as internal representation for query evaluation
plans.
theyll be baa-aack.
Relational calculus is more declarative
users define queries in terms of what they want, not in
terms of how to compute it.
Almost every query can be expressed several ways
and thats what makes query optimization fun!