Review
Theoretical Query Languages
Relational Algebra
1.
2.
3.
4.
5.
6.
•
•
SELECT ( σ )
PROJECT ( π )
UNION ( )
SET DIFFERENCE ( – )
CARTESIAN PRODUCT ( )
RENAME ( ρ )
RA: gives semantics to practical query languages
Above set: minimal relational algebra
will look at some redundant (but useful!) operators today
Review
Express the following query in the RA:
Find the names of customers who have both accounts and loans
T1 ρT1 (cname2, lno) (borrower)
T2 depositor T1
T3 σcname = cname2 (T2)
Result π cname (T3)
Above sequence of operators (ρ, , σ) very common.
Motivates additional (redundant) RA operators.
Relational Algebra
Redundant Operators
1. Natural Join ( )
2. Division ( )
3. Generalized Projection (π)
4. Outer Joins (
•
)
5. Update ( ) (we’ve already been
using)
• Redundant: Above can be expressed in terms of minimal RA
e.g. depositor borrower =
π …(σ…(depositor ρ…(borrower)))
• Added as convenience
Natural Join
Notation: Relation1 Relation2
Idea: combines ρ, , σ
A B C D
1
2
2
3
α
α
α
β
r
+
+
10
10
20
10
E
B
D
‘a
’
‘a
’
‘b
’
‘c
depositor
’
α
α
β
β
10
20
10
10
s
≡
A B C D
=
1
2
2
3
3
borrower
α
α
α
β
β
+
+
+
1
0
1
0
2
0
1
0
1
0
E
‘a
’
‘a
’
‘a
’
‘b
’
‘c
’
πcname,acct_no,lno (σcname=cname2 (depositor ρt(cname2,lno) (borrower)))
Division
Notation: Relation1 Relation2
Idea: expresses “for all” queries
A B
α
α
α
β
γ
γ
γ
γ
δ
δ
1
2
3
1
1
3
4
6
1
r2
B
1
2
A
=
α
δ
s
Query: Find values for A in r
which have corresponding B
values for all B values in s
Division
Another way to look at it: and
17 3 = 5
The largest value of i such
that: i 3 ≤ 17
Relational Division
A B
α 1
α 2
α 3
β 1
γ 1
γ 3
γ 4
γ 6
δ 1
δ r2
B
1
2
s
A
=
α
δ
t
The largest value of t such that:
( t s r )
Division
A More Complex Example
A B C D E
α
α
α
β
β
γ
γ
γ
a α a 1
a γ a 1
a γ b 1
a γ a 1
a γ b 3
a γ a 1
a γ b 1
a βr b 1
D E
a 1
b 1
s
=
A B C
?
α a γ
γ a γ
t
Generalized Projection
Notation: p e ,…,e (Relation)
1
n
e1,…,en can include arithmetic expressions – not just attributes
Example
credit =
cname
limit
balance
Jones
Turner
5000
3000
2000
2500
Then…
π cname, limit - balance (credit) =
cname
limit-balance
Jones
Turner
3000
500
Outer Joins
Motivation:
loan =
bname
lno
Downtown
Redwood
Perry
L-170
L-230
L-260
amt
cname
lno
3000
borrower =
4000
1700
Jones
Smith
Hayes
L-170
L-230
L-155
=
loan
borrower =
Join result loses…
any record of Perry
any record of Hayes
bname
lno
amt
cname
Downtown
Redwood
L-170
L-230
3000
4000
Jones
Smith
Outer Joins
bname
loan =
lno
amt
Downtown L-170 3000 borrower =
Redwood L-230 4000
Perry
L-260 1700
cname
lno
Jones
Smith
Hayes
L-170
L-230
L-155
1. Left Outer Join ( )
• preserves all tuples in left relation
loan
borrower
=
bname
lno
amt
cname
Downtown
Redwood
Perry
L-170
L-230
L-260
3000
4000
1700
Jones
Smith
┴
┴ = NULL
Outer Joins
bname
lno
amt
cname
loan =
Downtown L-170 3000
Redwood L-230 4000
Perry
L-260 1700
borrower = Jones
Smith
Hayes
lno
L-170
L-230
L-155
2. Right Outer Join ( )
• preserves all tuples in right relation
loan
borrower =
bname
lno
amt
cname
Downtown L-170 3000
Redwood L-230 4000
┴
L-155
┴
Jones
Smith
Hayes
┴ = NULL
Outer Joins
bname
lno
amt
loan = Downtown L-170 3000 borrower =
Redwood
Perry
L-230 4000
L-260 1700
cname
lno
Jones
Smith
Hayes
L-170
L-230
L-155
3. Full Outer Join ( )
• preserves all tuples in both relations
loan
borrower =
bname
lno
amt
cname
Downtown
Redwood
Perry
┴
L-170
L-230
L-260
L-155
3000
4000
1700
┴
Jones
Smith
┴
Hayes
┴ = NULL
Update
Notation: Identifier Query
Common Uses:
1. Deletion: r r – s
e.g., account account – σbname=Perry (account)
(deletes all Perry accounts)
2. Insertion: r r s
e.g., branch branch {(Waltham, Boston, 7M)}
(inserts new branch with
bname = Waltham, bcity = Boston, assets = 7M)
e.g., depositor depositor (ρtemp (cname,acct_no) (borrower))
(adds all borrowers to depositors, treating lno’s as acct_no’s)
3. Update: r πe1,…,en (r)
e.g., account πbname,acct_no,bal*1.05 (account)
(adds 5% interest to account balances)
Another Theoretical Query
Language
Two flavors:
Relational Calculus
• Tuple relational calculus (TRC)
• Domain relational calculus (DRC)
Logic-based query language
({x | … }, , , , , , , …)
More declarative than RA
RA: πlno (σamt > 1000 (loan))
Procedural
1.
2.
Select loan tuples with amt > 1000
Project the result of 1 on lno
TRC: {t | s loan (t [lno] = s [lno] s [amt] > 1000) }
Non-procedural
• No order of evaluation implied
• Basis for SQL
Bank Database
Account
bname
Downtown
Mianus
Perry
R.H.
Brighton
Redwood
Brighton
Branch
acct_no
A-101
A-215
A-102
A-305
A-201
A-222
A-217
Depositor
balanc
e
500
700
400
350
900
700
750
cname
acct_no
Johnson
Smith
Hayes
Turner
Johnson
Jones
Lindsay
A-101
A-215
A-102
A-305
A-201
A-217
A-222
Customer
cname
cstreet
ccity
Jones
Smith
Hayes
Curry
Lindsay
Turner
Williams
Adams
Johnson
Glenn
Brooks
Green
Main
North
Main
North
Park
Putnam
Nassau
Spring
Alma
Sand Hill
Senator
Walnut
Harrison
Rye
Harrison
Rye
Pittsfield
Stanford
Princeton
Pittsfield
Palo Alto
Woodside
Brooklyn
Stanford
bname
bcity
assets
Downtown
Redwood
Perry
Mianus
R.H.
Pownel
N. Town
Brighton
Brooklyn
Palo Alto
Horseneck
Horseneck
Horseneck
Bennington
Rye
Brooklyn
9M
2.1M
1.7M
0.4M
8M
0.3M
3.7M
7.1M
Borrower
cname
lno
Jones
Smith
Hayes
Jackson
Curry
Smith
Williams
Adams
L-17
L-23
L-15
L-14
L-93
L-11
L-17
L-16
Loan
bname
lno
amt
Downtown
Redwood
Perry
Downtown
Mianus
R.H.
Perry
L-17
L-23
L-15
L-14
L-93
L-11
L-16
1000
2000
1500
1500
500
900
1300
Tuple Relational Calculus
Some Queries
1. Find loans for amounts > $1200
{t | t loan t[amt] > 1200}
Basic Form: {x | P(x)}
• set comprehension: “the set of all x such that P(x) is true”
• x: tuple variable
Result
• logic contained in predicate (P)
1.
2.
3.
t loan
t [amt] > 1200
(1) (2) (equivalent to σ (2) (loan))
bname
lno
amt
Redwood
Perry
Downtown
Perry
L23
L15
L14
L16
2000
1500
1500
1300
Tuple Relational Calculus
Predicates
Given {x | P(x)}, what can P(x) be?
1. Simple predicate (, =, ≠, <, >, ≤, ≥)
e.g., t loan
e.g., t [amt] > 1200
2. Compound predicate (, , , )
e.g., (t loan) t [amt] > 1200
e.g., (t [bname] = “Downtown”)
e.g., (t [bname] = “Downtown”) t [amt] > 1200
( OR, AND, NOT)
Tuple Relational Calculus
Predicates
Given {x | P(x)}, what can P(x) be?
3. Quantified Predicates (, )
(a) Existential Quantification ()
t r (Q (t))
• true if there exists some tuple in r (t) such that Q(t) is true
• e.g., s loan (s [lno] = “L-17”)
(b) Universal Quantification ()
t r (Q (t))
• true if for all tuples in r (t), Q(t) is true
• e.g., s loan (s [amt] > 100)
Tuple Relational Calculus
More Queries
2. {t | t loan s loan (s [amt] > t [amt])}
A. Returns everything in loan except for (Redwood, L-23, 2000)
3. {t | t loan s loan (s [amt] > t [amt])}
A. Returns
Q. Express a
bnam
e
lno
amt
Mianu L-93 500
s
TRC query
to find the
largest loan
A. {t | t loan s loan (s [amt] < t [amt])}
OR
{t | t loan ( s loan (s [amt] > t [amt]))}
Tuple Relational Calculus
Projection Queries
σ : Find loans for amts > 1200
{t | t loan (t [amt] > 1200)}
t | t loan indicates that t has same structure as tuples in loan
π: Find loan numbers for all loans for amts > 1200
{t | s loan (t [lno] = s [lno] s [amt] > 1200)}
No predicate of form: t relation
t consists of attributes used in set comprehension with t (i.e., lno)
Result =
lno
L-23
L-15
L-14
L-16
Tuple Relational Calculus
Projection Queries
Q. Find names and cities of branches with assets > $3M.
A. {t | s branch
(t [branch] = s [branch] t [bcity] = s [bcity] s [assets] > 3M)}
bname
Result =
bcity
Downtown Brooklyn
R.H.
Horsenec
k
N.Town
Rye
Brighton
Brooklyn
Tuple Relational Calculus
Join Queries
Find the names of customers w/ loans at the Perry branch.
Answer has form {t | P(t)}.
Strategy for determining P(t):
1. What tables are involved?
borrower (s), loan (u)
2. What are the conditions?
(a) Projection:
(b) Join:
(c) Selection:
t [cname] = s [cname]
s [lno] = u [lno]
u [bname] = “Perry”
Tuple Relational Calculus
Join Queries
Find the names of customers w/ loans at the Perry branch.
A. {t | s borrower (P(t,s))} such that:
P(t,s)
Q(t,s,u)
t [cname] = s [cname] u loan (Q(t,s,u))
s [lno] = u [lno] u [bname] = “Perry”
OR
{t | s borrower (
t [cname] = s [cname]
u loan (s [lno] = u [lno] u [bname] = “Perry”))}
unfolded version (either is ok)
Tuple Relational Calculus
Join Queries
Q. Find loan numbers of loans held at branches in Brooklyn.
1. Tables involved
loan (s), branch (u)
2. Conditions
(a) Projection
t [lno] = s [lno]
(b) Join
s [bname] = u [bname]
(c) Selection
u [bcity] = “Brooklyn”
A. {t | s loan (P(t,s))} such that:
P(t,s) t [lno] = s [lno] u branch (Q(t,s,u))
Q(t,s,u) s [bname] = u [bname] u [bcity] = “Brooklyn”
Tuple Relational Calculus
Join Queries
Q. Find the names and cities of customers having a loan
from the Perry branch
1. Tables involved
borrower (s), customer (u), loan (v)
2. Conditions
(c) Selection
(a) Projection
v [bname] = “Perry”
t [cname] = s [cname]
t [ccity] = u [ccity]
(b) Join
s [cname] = u [cname]
s [lno] = v [lno]
A. {t | s borrower (P(t,s))}
P(t,s)
Q(t,s,u)
R(t,s,u,v)
t [cname] = s [cname] u customer (Q(t,s,u))
t [ccity] = u [ccity] s [cname] = u [cname] v loan (R(t,s,u,v))
s [lno] = v [lno] v [bname] = “Perry”
Tuple Relational Calculus
Implication ()
Resembles if … then
p q : true if p being true always means q is also true
pq ≡ pq
Example
{t | t loan P(t) Q(t) }
P(t)
Q(t)
Result =
≡ t [bname] = “Perry”
≡ t [amt] > 1000
bname
lno
amt
Redwood
Downtown
Mianus
R.H
Perry
Perry
L23
L14
L93
L-
200
0
150
0
500
900
130
P(t)
Q(t)
Tuple Relational Calculus
Implication ()
Often is used with to express “for all” queries
e.g., Find names of customers who have an account at all branches
located in Brooklyn
Connection of all to implies
Rewording of example:
Find names of customers for whom the following property hold:
For every branch, if the branch in located in Brooklyn, this implies
that the customer has an account at that branch.
Tuple Relational Calculus
Implication (cont.)
Q. Find names of customers for whom the following
property holds: For every branch, if the branch is
located in Brooklyn, this implies that the
customer has an account at that branch.
A. {t | s branch (s [bcity] = “Brooklyn” P(t,s))}
What is P(t,s)?
Tuple Relational Calculus
Implication (cont.)
Q. Find names of customers for whom the following
property holds: For every branch, if the branch is
located in Brooklyn, this implies that the
customer has an account at that branch.
A. {t | s branch (s [bcity] = “Brooklyn” P(t,s))}
1. Tables involved
branch (s), depositor (u), account (v)
2. Conditions
(a) Implication
s [bcity] = “Brooklyn”
(b) Projection
t [cname] = u [cname]
(c) Join
s [bname] = v [bname]
u [acct_no] = v [acct_no]
(d) Selection -
Tuple Relational Calculus
Implication (cont.)
Q. Find names of customers for whom the following
property holds: For every branch, if the branch is
located in Brooklyn, this implies that the
customer has an account at that branch.
A. {t | s branch (s [bcity] = “Brooklyn” P(t,s))}
P(t,s)
≡ u depositor (Q(t,s,u))
Q(t,s,u) ≡ t [cname] = u [cname] v account (R(t,s,u,v))
R(t,s,u,v) ≡ s [bname] = v [bname] u [acct_no] = v [acct_no]
Domain Relational Calculus
Atoms & Formulas
Let
Di be a domain variable
c be a domain constant
be a comparison
operator
Atoms
• r(D1, D2, …, Dn)
• Di Dj
• Di c
Let F, F1 and F2 be formulas
Formulas
• (F)
• not F
• F1 and F2
• F1 or F2
Let D be free* in F(D)
• (exists D) F(D)
• (forall D) F(D)
* a variable is free in a
formula if it is not
quantified by exists or
forall
Domain Relational Calculus
Valid Expression
{ D1, …, Dn | F (D1, …, Dn) }
is a valid DRC expression if it has only the
variables appearing to the left of the vertical bar |
free in F.
Any other variable appearing in F must be bound.
free vs. bound variables
• free (global): variable is not explicitly quantified
• bound (free): variable is declared explicitly
through quantification and its scope is the
quantified formula
Domain Relational Calculus
Relational Completeness
condition (r):
{ R1, …, Rn | r(R1, …, Rn) and condition}
pai,…,aj(r):
{ Ri, …, Rj | r(R1, …, Ri, …, Rj, …, Rn)}
r s:
{ D1, …, Dn | r(D1, …, Dn) or s(D1, …, Dn) }
r - s:
{ D1, …, Dn | r(D1, …, Dn) and not s(D1, …, Dn) }
q×r:
{ Q1, …, Qm, R1, …, Rn | q(Q1, …, Qm) and r(R1, …,
Rn) }
Tuple Relational Calculus
Syntax Summary
{ T1, …, Tn | F (T1, …, Tn) }
• F describes the properties of the data to be
retrieved.
• The output schema of F is given by the tuple
variables
T1, …, Tn that act as global variables in F.
Tuple Relational Calculus
Atoms & Formulas
Let
T and Ti be tuple variables
aj be an attribute
c be a domain constant
be a comparison
operator
Atoms
• r(T)
• Ti.am Tj.an
• T.ai c
Let F, F1 and F2 be formulas
Formulas
• (F)
• not F
• F1 and F2
• F1 or F2
Let T be free* in F(T)
• (exists T) F(T)
• (forall T) F(T)
* a variable is free in a
formula if it is not
quantified by exists or
forall
Tuple Relational Calculus
Valid Expression
{ T1, …, Tn | F (T1, …, Tn) }
is a valid TRC expression if it has only the
variables appearing to the left of the vertical bar |
free in F.
Any other variable appearing in F must be bound.
free vs. bound variables
• free (global): variable is not explicitly quantified
• bound (free): variable is declared explicitly
through quantification and its scope is the
quantified formula
Tuple Relational Calculus
Relational Completeness
condition (r):
{ R| r(R) and condition}
pai…,aj(r):
{ R.ai, …, R.aj | r(R)}
r s:
{ T | r(T) or s(T) }
r - s:
{ T | r(T) and not s(T) }
q×r:
{ Q, R | q(Q) and r(R) }
Introduction
- Relational algebra is procedural: it specifies the procedure to be
followed in order to get the answer to the query.
- Relational calculus is declarative: it describes (declares) the answer
to the query without specifying how to get it.
- Relational calculus strongly resembles First Order Predicate Logic,
or simply first order logic.
- There are two variants of relational calculus:
- Tuple relational calculus (TRC)
- Domain relational calculus (DRC)
TUPLE RELATIONAL CALCULUS
- A query statement in TRC is a set declaration having the form:
{ P first-order logic formula}
- This is to be read as ‘the set of tuple variables, P, for which the
specified first order logic formula is true’.
- Thus a TRC query is a request (to the DBMS) to produce a set of
tuples corresponding to the tuples of the relational answer in
SQL.
- Example
Given the following query:
(Q11) Find all sailors with a rating above 7.
The TRC statement of this query is
{S S Sailors S.rating > 7}.
SYNTAX AND SEMANTICS OF TRC
• The syntax and semantics of TRC is that
of first-order logic. It is stated quite
precisely in the text and there is no need
to repeat it here. Instead we shall
examine a few query applications.
QUERY Q12
(Q12) Find the names and ages of sailors with a rating above 7.
{P ∃S Sailors (S.rating > 7 P.name = S.name P.age = S.age)}
Remarks
1. The fact that the tuple variable P occurs with two attributes (using
the dot notation) means that solely these two attributes are
required in the answer relation.
2. The symbols used are the usual first-order logic symbols:
∀: for all
∃: there exists
⋀ : and
⋁: or
¬ : not
⇒ : implies
QUERIES 1,2,7,9,14
The TRC statements for these queries are
pretty well self explanatory, especially
with the added English statements of how
to read them.
DOMAIN RELATIONAL
CALCULUS (1)
- The form of a DRC query is as follows:
{<X1, X2, … , Xn> logical DRC formula}
signifying that the system must construct (and output) a set of all
the tuples which satisfy the stated logical DRC formula in terms
of the n attributes X1, X2, … ,Xn. Thus, the answer is a relational
instance with attributes X1, X2, … , Xn, these attributes
corresponding to those of some of the relations in the database.
- Again, the approach used by the system is left unspecified.
- The Syntax and the semantics of the DRC are explicitly and
precisely described in the text.
DOMAIN RELATIONAL
CALCULUS (2)
Example:
(Q11) Find all sailors with a rating above 7.
{ < I, N, T, A > <I, N, T, A > ∈ Sailors ⋀ T > 7 }
Other queries are illustrated and described in the text with all
necessary explanation.
EXPRESSIVE POWER OF
ALGEBRA AND CALCULUS( 1)
Safety
- Certain queries stated in the relational calculus may lead to
answers which contain an infinite number of tuples (or at least as
many as the system can handle).
Example:
Consider the TRC query {S ¬(S ∈ Sailors)}. Since there is a quasiinfinite number of tuples that can be created with the attributes of
sailors, the answer is (quasi)-infinite.
- A query which yields a (quasi)-infinite answer is said to be unsafe,
and, of course, should not be allowed by the system.
- It is possible to define a safe formula in TRC (see text, section 4.4).
LabSessie 2
Tuple Relational Calculus
• A query in a tuple relational calculus is expressed as
{t| P(t)}
(set of all tuples t such that predicate P is true for t)
• Query with constructs “or”, “and”, “there exists”
t r(Q(t))
(there exists a tuple t in relation such that predicate Q(t) is true)
Tuple Relational Calculus
• Query with the construct “implies”
PQ
(if P is true, then Q must be true)
• Query with the construct “for all”
t r(Q(t))
(Q is true for all tuples t in relation r)
{t| P(t)}
t r(Q(t))
Tuple Relational Calculus
• Relations
• V(d, k): visits(drinker, kroeg) V(d, k)
• S(k,b): servers(kroeg, bier)
S(k,b)
• L(d,b): likes(drinker, bier)
L(d,b)
• Example (one relation)
– We want to have the drinkers and the pubs for visitors
of the pub ‘Café’:
{t|tV t[k]=‘Café’}
Tuple Relational Calculus
• Example (one relation)
– If we want only the drinker attribute rather than all the
attribute of the V relation:
{t| s V ( t[d] = s[d] s[k]=‘Café’)}
V(d, k)
S(k,b)
L(d,b)
• Example (two relations)
– Fin the names of drinkers that likes Duvel
{t| s V ( t[d] = s[d]
V(d, k)
S(k,b)
u L ( s[d] = u[d]
L(d,b)
u[b]=‘Duvel’ ))}
Tuple Relational Calculus
• Example (union)
S
L
– If we want all beers (server or liked)
{t | s S ( t[b] = s[b])
u L ( t[b] = u[b]) }
• Example (intersection)
S
V(d, k)
S(k,b)
L(d,b)
L
– If we want the beers that are both served and liked
{t | s S ( t[b] = s[b])
u L ( t[b] = u[b]) }
Functional Dependency
• Canonical Cover
– Definition
A canonical cover Fc for F is a set of dependencies
such that F logically implies all dependencies in Fc,
and Fc logically implies all dependencies in F.
– Fc is a minimal set of the functional dependencies that has the
same closure as a given set of functional dependencies
– Application: reducing the effort spent in checking for constraint
violation
Functional Dependency
• Canonical Cover
– Computing Canonical Cover
Fc=F
Repeat
• Use the union rule to replace any dependencies in Fc
of the form X Y and X Z with X YZ
• Find a functional dependency X Y in Fc with an
extraneous attribute either in X or in Y
• If an extraneous attribute is found:
delete it from X Y
Until Fc does not change
Extraneous attribute
Attribute of a functional
dependency that can be
removed without
changing the closure of
the set of functional
dependencies
Note
The test for
extraneous
attributes is done
using Fc, not F
Functional Dependency
• Computing Canonical Cover
– Exercise
F={BCD A, BC E, A F, F G, C D, A G}
• Fc = F
• Union rule: A F, A G then A FG
• Fc = {BCD A, BC E, F G, C D, A FG}
Functional Dependency
• Computing Canonical Cover
– Exercise
• Fc = {BCD A, BC E, F G, C D, A FG}
• D is an extraneous attribute in BCD A
– To prove F |- (F -{BCD A}) {BC A}
– Proof
C D (given)
C CD (augmentation)
BC BCD (augmentation)
BCD A (given)
BC A (transitivity)
• Fc = {BC A, BC E, F G, C D, A FG}
Functional Dependency
• Computing Canonical Cover
– Exercise
• Fc = {BC A, BC E, F G, C D, A FG}
• G is an extraneous attribute in A FG
– To prove: (F - {A FG}) {A F} |- F
– Proof:
F G (given)
F FG (augmentation)
A F (given in F)
A FG (transitivity)
• Fc = {BC A, BC E, F G, C D, A F}
• Union rule: BC A, BC E then BC AE
• Fc = {BC AE, F G, C D, A F}
© Copyright 2026 Paperzz