Sunteți pe pagina 1din 62

# EECS 317 Data Management and

Information Processing
Fall 2012
Goce Trajcevski
Dept. of EECS
Northwestern University
Tel: 847-491-7069
Email: goce@eecs.northwestern.edu
1
Part 5: Relational Data
Formal Foundations
2
Manipulating Relational Data: Topics Outline
First-Order Logic
A little side-track, but needed for foundations
Relational Algebra
Procedural Aspects (albeit with declarative flavor)
May think as foundation of SQL
Touch upon
Relational Calculus
What vs. How (i.e., more declarative)
Tuple Relational Calculus
Domain Relational Calculus
Query By Example (QBE)
Microsoft Access
3
First Order Logic (FOL) AKA Predicate
Calculus
The antique example (hypothetical syllogism):

All men are mortal.
Socrates is a man.
Therefore, Socrates is mortal.
Lets try predicate calculus
p: All men are mortal.
q: Socrates is a man.
r: Socrates is mortal.

So, we have the argument
(p . q) r
Hmm not quite a tautology (which
could be used to define valid rules of inference)

Maybe ( p q) r ???
How about if we try something like:

Something is a man It is mortal
Something is Socrates It is a man

Something is Socrates It is mortal
But then, how do we check the truth/falsity of p: Something is a man ???
4
First Order Logic (FOL) AKA Predicate
Calculus
Maybe what we want to express, exceeds the limit of the propositional
calculus as a formalization-tool?
Indeed, what previous example shows is that we need an ability to reason for all
Predicate Logic:
Builds upon propositional logic (e.g., uses the same operators , .. v. ,)
Adds variables (ranging over some domains as a source of values for the
respective constants) AND predicates (describing some property) instead of
simple propositions
Specifically:
We have something like P(X,Y) stating that the variables X and Y are participating
in some property P
AND, IN ADDITION, THERE ARE QUANTIFIERS:
For All: X[Q(X)] for all X (form whatever domain), the property Q
(whatever it denotes) holds
There Exists: -Z[R(Z)] there exists (some, at least one) Z (from whatever
domain), for which the property Z (whatever it specifies) holds.
NOTE: if need to say there exists Exactly One, use -!Y[.]; also,
[ and ] denote, what is called a scope of the quantifier
5
First Order Logic (FOL) AKA Predicate
Calculus
Example:
With this formalism, we can say:
Let H denote the property is human (i.e., H(X) means X is human)
Let M denote the property is mortal (i.e., M(Y) means Y is mortal)
Now, we can say:
X[H(X) M(X)]
And the property is human is observed for the specific constant o
Then, by ground instantiation we can infer is mortal also holds for o
In other words, now we have (X[H(X) M(X)] . H(o)) M(o)
And simply substitute Socrates as a sample-o
Comment:
Observe yet another deficiency of the propositional calculus:
If we try to list certain property for different individual, wed need many, many,
variables:
p: John has an umbrella; q: Tom has an umbrella; r: Jim has an umbrella
As opposed to simply: U(X), and we agree that:
U denotes has an umbrella
X ranges over people (well, their names)
6
First Order Logic (FOL) AKA Predicate Calculus
Essence:
So, the key observations are that for every formula P(X,Y,Z), we need something
called interpretation which, in other words, means:
What is the meaning of P as a property;
What are the sources (i.e., domains) for each of X, Y and Z
HOW DO WE DETECT WHAT IS TRUE vs. WHAT IS FALSE
Crux:
Once the above are defined, then we can straightforwardly apply the rules of
evaluating operations, just like propositional logic, e.g.,
X-Y-Z[P(X,Y,Z) (Q(X,Y) . R(X,b,Z))]
Example:
Let Num denote the property is a number
Then, the formula: XY[(Num(X) . Num(Y)) ((X+Y)
2
< 0)]
Simply states:
For all x and all y, if x and y are numbers then the square of their sum is not
negative
7
First Order Logic (FOL) AKA Predicate
Calculus
Example the order of quantification:
For every number, there exists an additive inverse:
X-Y[Num(X) . Num(Y) . (X+Y = 0)]
NOTE: the order of quantification DOES MATTER:
-XY[Num(X) . Num(Y) . (X+Y = 0)] would mean that there exists some X such
that for every other possible Y, if you add X to it, the sum is 0
Example limits (without recursion):
Let DF(X,Y) denote the predicate direct flight between X and Y.
Let CN1(Z,W) denote the predicate Z and W are connected with at most one
stop-over
Then, observe:
XY[DF(X,Y) v (-Z [DF(X,Z) . DF(Z,Y)]) CN1(X,Y)]
Example the role of the interpretation:
XY-Z[X = Z*Y]
True for Real numbers
False for Integers
The true-math of FOL is that it provides some laws for quantifiers and
predicates, regardless of their assignment via particular interpretation
E.g., P(X,Y) may denote X and Y are Apostles, or X and Y are prime-
divisors of each other or X and Y are presidential-nominees or,
Regardless, it will always be the case that XY[P(X,Y) (P(X,Y)]

8
First Order Logic (FOL) AKA Predicate
Calculus
So, why all this side-tracking:
Well, quite frankly, in our settings, R(A,B,C,D) is a relation R with attributes
A, B, C, and D and, in practice:
Each of A,B,C,D comes from some domain;
R will denote some meaningful property among them
Most importantly: the truth/falsity will be evaluated based on the ground
values (instances) of the actual tuples in the actual relation
This is the foundation for the Relational Databases in terms of modeling AND
querying, as well as a formal tool for all the languages that we are about to
see
Historic Note:
The true hope of the mathematicians at the end of the nineteenth century was that that all
of mathematics could be made purely formal and symbolic in this way, most seriously
attempted in Principia Mathematica (Whitehead and Russell, 1910, mathematics
symbolic language). The dream was that a purely formal procedure can be developed for
checking the truth of mathematical statements and producing proofs. This dream was cut
short by Gdel's incompleteness theorem (1931), which effectively showed the
impossibility of any such procedure. Nonetheless, the language that mathematicians still
use in writing down their work is a somewhat less formal version of the predicate
calculus
BTW: theres 2
nd
Order Logic (quantifying over the predicates too, not only
the variables)
9
Relational Algebra
More procedural than declarative
Basic operators
select: o
project: I
union:
set difference:
Cartesian product: x
rename: p
The operators take one or two relations as inputs and produce a new
relation as a result.
10
Relational Algebra: Selection
Relation r
A B C D
o
o
|
|
o
|
|
|
1
5
12
23
7
7
3
10
o
A=B ^ D > 5
(r)
A B C D
o
|
o
|
1
23
7
10
o
p
(r)
p is called the selection predicate
Defined as:

o
p
(r) = {t | t e r and p(t)}

Where p is a formula in propositional calculus
consisting of terms connected by : . (and), v
(or), (not)
Each term is one of:
<attribute> op <attribute> or
<constant>
where op is one of: =, =, >, >. <. s

Example of selection:

o
branch_name=Perryridge
(account)
11
Relational Algebra: Projection
Relation r:
A B C
o
o
|
|
10
20
30
40
1
1
1
2
A C
o
o
|
|
1
1
1
2
=
A C
o
|
|
1
1
2
I
A,C
(r)
where A
1
, A
2
are attribute names and r is a relation
name.
The result is defined as the relation of k columns
obtained by erasing the columns that are not listed
Duplicate rows removed from result, since relations
are sets
Example: To eliminate the branch_name attribute of
account

I
account_number, balance
(account)

) (
, , ,
2 1
r
k
A A A
I
12
Relational Algebra: Union
r s:
A B
o
o
|
1
2
1
A B
o
|
2
3
r
s
A B
o
o
|
|
1
2
1
3
r s = {t | t e r or t e s}

MUSTs:
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (corresponding
columns MUST be of a same type)

Find all customers with either an account or a loan
I
customer_name
(depositor) I
customer_name

(borrower)
13
Relational Algebra: Set Difference
r
s:
A B
o
o
|
1
2
1
A B
o
|
2
3
A B
o
|
1
1
r s
Defined as:
r s = {t | t e r and t e s}

Set differences must be taken between compatible
relations.
r and s must have the same arity
attribute domains of r and s must be compatible

14
Relational Algebra: Cartesian-Product
r x s:
A B
o
o
o
o
|
|
|
|
1
1
1
1
2
2
2
2
C D
o
|
|

o
|
|

10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
A B
o
|
1
2
r
C D
o
|
|

10
10
20
10
E
a
a
b
b
s
r x s
r x s = {t q | t e r and q e s}
Assume that attributes of r(R)
and s(S) are disjoint. (That
is, R S = C).
Otherwise, some renaming
must be used.
Example:
15
Relational Algebra: Composition of Operators
Recall r x s
A B
o
o
o
o
|
|
|
|
1
1
1
1
2
2
2
2
C D
o
|
|

o
|
|

10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
A B C D E
o
|
|
1
2
2
o
|
|
10
10
20
a
a
b
Then, o
A=C
(r x s)

16
Relational Algebra: Renaming
Enables referring to a given relation by > 1 name (similarly for
attributes).
Syntax: p
x
(E)
return the expression E under the name X
If a relational-algebra expression E has arity n, then

returns the result of expression E under the name X, and with the
attributes renamed to A
1
, A
2
, ., A
n
.

) (
) ,..., , (
2 1
E
n
A A A x
p
17
Relational Algebra: Few Examples
branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)
Consider a relation with the following schema:
18
Relational Algebra: Few Examples
Find all loans of over \$1200

Find the loan number for each loan of an amount greater than
\$1200

o
amount > 1200
(loan)

I
loan_number
(o
amount

> 1200
(loan))

Find the names of all customers who have a loan, an account, or
both, from the bank
I
customer_name
(borrower) I
customer_name
(depositor)

19
Relational Algebra: Few Examples
Find the names of all customers who have a loan at the Perryridge
branch.
Find the names of all customers who have a loan at the Perryridge branch
but do not have an account at any branch of the bank.
I
customer_name
(o
branch_name = Perryridge

(o
borrower.loan_number = loan.loan_number
(borrower x loan) ) )
I
customer_name
(depositor)
I
customer_name
(o
branch_name=Perryridge
(o
borrower.loan_number = loan.loan_number
(borrower x
loan)))
NOTE: here we have to navigate between
different tables
20
Relational Algebra: Few Examples
Find the largest account balance
Strategy:
Find those balances that are not the largest
Rename account relation as d so that we can compare each
account balance with all others
Use set difference to find those account balances that were not
found in the earlier step.
The query is:

I
balance
(account) - I
account.balance

(o
account.balance < d.balance
(account x
r
d
(account)))
Practice HW - write the Relational Algebra expression for the following query:
Find the names of all customers who have a loan at the Perryridge branch
21
Relational Algebra: touch of Formality
A basic expression in the relational algebra consists of either one of the
following:
A relation in the database
A constant relation
Let E
1
and E
2
be relational-algebra expressions; the following are all
relational-algebra expressions:
E
1
E
2

E
1
E
2

E
1
x E
2

o
p
(E
1
), P is a predicate on attributes in E
1

I
s
(E
1
), S is a list consisting of some of the attributes in E
1

p
x
(E
1
), x is the new name for the result of E
1
22
(expressive) power to the relational algebra, but simplify common
queries.
Set intersection
Natural join
Division
Assignment
23
Relational Algebra: Set-Intersection
r s = { t | t e r and t e s }
Need:
r, s have the same arity
attributes of r and s are compatible
Note this is additional operation because: r s = r (r s)
A B
o
o
|
1
2
1
r
A B
o
|
2
3
s
Example:
A B
o 2
r s
24
Relational Algebra: Natural-Join
Definition:
Let r and s be relations on schemas R and S respectively.
Then, r s is another relation, for which:
The schema is R S, and the tuples are obtained as follows:
Consider each pair of tuples t
r
from r and t
s
from s.
If t
r
and t
s
have the same value on each of the attributes in R S, add a
tuple t to the result, where
t has the same value as t
r
on r
t has the same value as t
s
on s
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
I
r.A, r.B, r.C, r.D, s.E
(o
r.B = s.B
.
r.D = s.D
(r x s))
Notation: r s
25
Natural Join Operation Example
Natural-Join
Example
A B
o
o
o
o
o
1
1
1
1
2
C D
o
o

|
a
a
a
a
b
E
o

o

o
A B
o
|

o
o
1
2
4
1
2
C D
o

|

|
a
a
b
a
b
r
B
1
3
1
2
3
D
a
a
a
b
b
E
o
|

o
e
s
r s
NOTES:
1. There is no replication of the
common-attributes (e.g., B,D)
2. In effect, the Natural-Join is an
abbreviation for:

-Calculate r x s;
-Select all the tuples that have equal
values on corresponding attributes
that appear in both relations of the
input.
-Project all the non-common attributes
PLUS a single-projection of common
ones

26
Relational Algebra: Few Notes on Join
The Natural-Join that we discussed is a most common type, however there
are several variations of the Join operation
The first one is, so called, equi-join
The name of the attributes in two relations need not be same (must be of same
type, though)
In the context of the previous example, we may Join r and s on the attributes
C and E, which is denoted as:
r s
r.C = s.E
A B
o
|

o
o
1
2
4
1
2
C D
o

|

|
a
a
b
a
b
r
B
1
3
1
2
3
D
a
a
a
b
b
E
o
|

o
e
s
A B C D B D E
o 1 o a 1 a o
| 2 a 1 a
o 1 a 1 a
4 | b 3 a |
o 2 | b 3 b |
27
Relational Algebra: Few Notes on Join
Note1: The equi-join, unlike the natural one, does NOT make a single-copy of
the joining attributes, nor does it eliminate the redundancy in terms of other
same-name attributes (well, it would not be correct, right?)
The most general statement of the join is:
Input: two relations R1 and R1
Input: Joining predicate (AKA O (Theta) join where O denotes an arbitrary
condition expressible in first-order logic/relational_algebra)

Output: a new relation which consists of all the merged-tuples from R1 and R1,
satisfying the joining predicate

Example:
r s
r.B < s.B
Practice HW: what is the output for the example above?
28
Relational Algebra: Division

Tailored toward queries which specify a property that needs
to include an entire-relation in it.
Let r and s be relations on schemas R and S respectively
where
R = (A
1
, , A
m
, B
1
, , B
n
)
S = (B
1
, , B
n
)
The result of r s is a relation on schema
R S = (A
1
, , A
m
)
r s = { t | t e I
R-S
(r) . u e s ( tu e r ) }
Where tu means the concatenation of tuples t and u to produce a
single tuple

r s
29
Relational Algebra: Division
Consider, for
example the
relations r, s:
Now, for r s:
o

a
a
A B C

A B
o
o
o
|
|

a
a
a
a
a
a
a
a
C D
o

|
a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
r
D
a
b
E
1
1
s
1. Need to exclude the common attributes
2. Need to preserve (those portions of!!!)
the tuples from r that match every tuple of s
Correlate it with the thinking of regular numeric division
30
Relational Algebra: Division
Property
Let q = r s
Then q is the largest relation satisfying q x s _ r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S _ R

r s = I
R-S
(r ) I
R-S
( ( I
R-S
(r ) x s ) I
R-S,S
(r ))

To see why
I
R-S,S
(r) simply reorders attributes of r

I
R-S
(I
R-S
(r ) x s ) I
R-S,S
(r) ) gives those tuples t in

I
R-S
(r ) such that for some tuple u e s, tu e r.

31
Relational Algebra: Assignment Operation
The assignment operation () provides a convenient way to
express complex queries.
Write query as a sequential program consisting of
a series of assignments
followed by an expression whose value is displayed as a result of the
query.
Assignment must always be made to a temporary relation variable.
Example: Write r s as
temp1

I
R-S
(r )
temp2 I
R-S
((temp1 x s ) I
R-S,S
(r ))
result = temp1 temp2
The result to the right of the is assigned to the relation-variable on
the left of the .
May use such variable in subsequent expressions.
Textbook example (Ch. 6.1.6)
32
Relational Algebra: Some Practice
Queries
Find the names of all customers who have a loan and an account at
bank.
Find the name of all customers who have a loan at the bank and the
loan amount
Find all customers who have an account from at least the
Downtown and the Uptown branches.
Find all customers who have an account at all branches located in
Brooklyn.
33
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single
value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra

E is any relational-algebra expression
G
1
, G
2
, G
n
is a list of attributes on which to group (can be empty)
Each F
i
is an aggregate function
Each A
i
is an attribute name
) (
) ( , , ( ), ( , , ,
2 2 1 1 2 1
E
n n n
A F A F A F G G G
0
34
Aggregate Operation Example
Relation r:
A B
o
o
|
|
o
|
|
|
C
7
7
3
10
g
sum(c)
(r)
sum(c )
27
35
Aggregate Operation Example
Relation account grouped by branch-name:
branch_name
g
sum(balance)
(account)
branch_name account_number balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch_name sum(balance)
Perryridge
Brighton
Redwood
1300
1500
700
36
Aggregate Functions (Cont.)
Result of aggregation does not have a name
Can use rename operation to give it a name
For convenience, we permit renaming as part of aggregate operation

branch_name
g
sum(balance) as sum_balance
(account)
37
Relational Algebra: Few last words on
joinOuter Join
An extension of the join operation that avoids loss of information.
Recall: not all the tuples from R1 x R2 will appear in the output of a join
operation
Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does not exist
All comparisons involving null are (roughly speaking) false by definition
38
Relational Algebra: Outer Joins
Relation loan
Relation borrower
customer_name loan_number
Jones
Smith
Hayes
L-170
L-230
L-155
3000
4000
1700
loan_number amount
L-170
L-230
L-260
branch_name
Downtown
Redwood
Perryridge
39
Relational Algebra: Outer Join Example
Join

loan borrower
loan_number amount
L-170
L-230
3000
4000
customer_name
Jones
Smith
branch_name
Downtown
Redwood
Jones
Smith
null
loan_number amount
L-170
L-230
L-260
3000
4000
1700
customer_name branch_name
Downtown
Redwood
Perryridge
Left Outer Join
loan borrower
40
Outer Join Example
loan_number amount
L-170
L-230
L-155
3000
4000
null
customer_name
Jones
Smith
Hayes
branch_name
Downtown
Redwood
null
loan_number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer_name
Jones
Smith
null
Hayes
branch_name
Downtown
Redwood
Perryridge
null
Full Outer Join
loan borrower
Right Outer Join
loan borrower
41
Recall: Null Values
It is possible for tuples to have a null value, denoted by null, for some
of their attributes
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is null.
Aggregate functions simply ignore null values
For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same (as in SQL)
42
Relational Algebra: Modification of the
Database
The content of the database may be modified using the following
operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment operator.
43
Recall the schema:
branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)
44
Relational Algebra: Deletion
A delete request is expressed similarly to a query, except instead
of displaying tuples to the user, the selected tuples are removed
from the database.
Can delete only whole tuples; cannot delete values on only
particular attributes
A deletion is expressed in relational algebra by:
r r E
where r is a relation and E is a relational algebra query.
45
Relational Algebra: Deletion Examples
Delete all account records in the Perryridge branch.
Delete all accounts at branches located in Needham.
r
1
o
branch_city = Needham
(account branch )
r
2
I
account_number,

branch_name, balance
(r
1
)
r
3
I
customer_name, account_number
(r
2
depositor)
account account r
2

depositor depositor r
3
Delete all loan records with amount in the range of 0 to 50
loan loan o
amount > 0 and amount s 50
(loan)
account account o
branch_name = Perryridge
(account )

46
Relational Algebra: Insertion
To insert data into a relation, we either:
specify a tuple to be inserted
write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a constant
relation containing one tuple.
47
Relational Algebra: Insertion Examples
Insert information in the database specifying that Smith has \$1200 in
account A-973 at the Perryridge branch.
Provide as a gift for all loan customers in the Perryridge
branch, a \$200 savings account. Let the loan number serve
as the account number for the new savings account.
account account {(A-973, Perryridge, 1200)}
depositor depositor {(Smith, A-973)}
r
1
(o
branch_name = Perryridge
(borrower loan))
account account I
loan_number, branch_name, 200
(r
1
)
depositor depositor I
customer_name, loan_number
(r
1
)
48
Relational Algebra: Updating
A mechanism to change a value in a tuple without charging all values
in the tuple
Use the generalized projection operator to do this task

Each F
i
is either
the I
th
attribute of r, if the I
th
attribute is not updated, or,
if the attribute is to be updated F
i
is an expression, involving only
constants and the attributes of r, which gives the new value for the attribute
) (
, , , ,
2 1
r r
l
F F F
I
49
Relational Algebra: Update Examples
Make interest payments by increasing all balances by 5 percent.
Pay all accounts with balances over \$10,000 6 percent interest
and pay all others 5 percent
account I
account_number, branch_name, balance * 1.06
(o
BAL > 10000
(account ))
I
account_number, branch_name, balance * 1.05
(o
BAL s 10000
(account))

account I
account_number, branch_name, balance * 1.05
(account)
50
Tuple Relational Calculus
A nonprocedural query language, where each query is of the form
{t | P (t ) }
It is the set of all tuples t such that predicate P is true for t
t is a tuple variable, t [A ] denotes the value of tuple t on attribute A
t e r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
51
Tuple Relational Calculus = Predicate Calculus
Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., <, s, =, =, >, >)
3. Set of connectives: and (.), or (v) not ()
4. Implication (): x y, if x if true, then y is true
x y x v y
5. Recall, quantifiers:
- t e r (Q (t )) there exists a tuple in t in relation r
such that predicate Q (t ) is true
t e r (Q (t )) Q is true for all tuples t in relation r
52
Tuple Relational Calculus: Example Queries
Find the loan_number, branch_name, and amount for loans of over
\$1200
Find the loan number for each loan of an amount greater than \$1200

{t | - s e loan (t [loan_number ] = s [loan_number ] . s [amount ] > 1200)}

Notice that a relation on schema [loan_number ] is implicitly defined by
the query

{t | t e loan . t [amount ] > 1200}
53
Tuple Relational Calculus : Example Queries
Find the names of all customers having a loan, an account, or both at
the bank
{t | -s e borrower ( t [customer_name ] = s [customer_name ])
. -u e depositor ( t [customer_name ] = u [customer_name] )
Find the names of all customers who have a loan and an account
at the bank
{t | -s e borrower ( t [customer_name ] = s [customer_name ])
v -u e depositor ( t [customer_name ] = u [customer_name ])
54
Tuple Relational Calculus: Example Queries
Find the names of all customers having a loan at the Perryridge branch
{t | -s e borrower (t [customer_name ] = s [customer_name ]
. -u e loan (u [branch_name ] = Perryridge
. u [loan_number ] = s [loan_number ]))
. not -v e depositor (v [customer_name ] =
t [customer_name ])}
Find the names of all customers who have a loan at the
Perryridge branch, but no account at any branch of the bank
{t | -s e borrower (t [customer_name ] = s [customer_name ]
. -u e loan (u [branch_name ] = Perryridge
. u [loan_number ] = s [loan_number ]))}
55
Tuple Relational Calculus: Example Queries
Find the names of all customers having a loan from the Perryridge
branch, and the cities in which they live
{t | -s e loan (s [branch_name ] = Perryridge
. -u e borrower (u [loan_number ] = s [loan_number ]
. t [customer_name ] = u [customer_name ])
. - v e customer (u [customer_name ] = v [customer_name ]
. t [customer_city ] = v [customer_city ])))}

Historic HW practice: express this query using Relational Algebra
56
Tuple Relational Calculus: Example Queries
Find the names of all customers who have an account at all branches
located in Brooklyn:
{t | - r e customer (t [customer_name ] = r [customer_name ]) .
( u e branch (u [branch_city ] = Brooklyn
- s e depositor (t [customer_name ] = s [customer_name ]
. - w e account ( w[account_number ] = s [account_number ]
. ( w [branch_name ] = u [branch_name ]))))}
Historic HW practice: express this query using Relational Algebra
57
Tuple Relational Calculus: Safety
It is possible to write tuple calculus expressions that generate infinite
relations.
For example, { t | t e r } results in an infinite relation if the domain
of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable
expressions to safe expressions.
An expression {t | P (t )} in the tuple relational calculus is safe if every
component of t appears in one of the relations, tuples, or constants that
appear in P
NOTE: this is more than just a syntax condition.
E.g. { t | t [A] = 5 v true } is not safe --- it defines an infinite set with
attribute values that do not appear in any relation or tuples or constants
in P.
58
Domain Relational Calculus
A nonprocedural query language equivalent in power to the tuple
relational calculus
Each query is an expression of the form:

{ < x
1
, x
2
, , x
n
> | P (x
1
, x
2
, , x
n
)}

x
1
, x
2
, , x
n

represent domain variables
P represents a formula similar to that of the predicate calculus

59
Domain Relational Calculus: Example Queries
Find the loan_number, branch_name, and amount for loans of over
\$1200

Find the names of all customers who have a loan from the
Perryridge branch, and the amount of that loan:
{< c, a > | - l (< c, l > e borrower . -b (< l, b, a > e loan .
b = Perryridge))}
{< c, a > | - l (< c, l > e borrower . < l, Perryridge, a > e loan)}

{< c > | - l, b, a (< c, l > e borrower . < l, b, a > e loan . a > 1200)}
Find the names of all customers who have a loan of over \$1200
{< l, b, a > | < l, b, a > e loan . a > 1200}
60
Domain Relational Calculus: Example Queries
Find the names of all customers having a loan, an account, or both at
the Perryridge branch:
{< c > | - s,n (< c, s, n > e customer) .
x,y,z (< x, y, z > e branch . y = Brooklyn)
- a,b (< x, y, z > e account . < c,a > e depositor)}
Find the names of all customers who have an account at all
branches located in Brooklyn:
{< c > | - l ( < c, l > e borrower
. - b,a (< l, b, a > e loan . b = Perryridge))
v - a (< c, a > e depositor
. - b,n (< a, b, n > e account . b = Perryridge))}
61
Domain Relational Calculus: Safety of
Expressions
The expression:
{ < x
1
, x
2
, , x
n
> | P (x
1
, x
2
, , x
n
)}

is safe if all of the following hold:
1. All values that appear in tuples of the expression are values from dom
(P ) (that is, the values appear either in P or in a tuple of a relation
mentioned in P ).
2. For every there exists subformula of the form - x (P
1
(x )), the
subformula is true if and only if there is a value of x in dom (P
1
such
that P
1
(x ) is true.
3. For every for all subformula of the form
x
(P
1
(x )), the subformula
is true if and only if P
1
(x ) is true for all values x from dom (P
1
).
62