Chapter 3: Relational Model
Chapter 3: Relational Model
customer (Customer-schema)
customer
Notation: p(r)
Defined as:
• Relation r A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Relation r: A B C
10 1
20 1
30 1
40 2
A C (r ) A C A C
1
1 1
1 = 1
1 2
2
r s = {t | t r or t s}
For r s to be valid.
Relations r, s:
A B A B
1 2
2 3
1 s
r
r s:
A B
1
2
1
3
r – s = {t | t r and t 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
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s:
A B
1
1
r x s = {t q| t r and q s}
Assume that attributes of r(R) and s(S) are disjoint. (This is,
R S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must
be used.
1 10 +
10 +
2 20 –
r 10 –
s
r x s:
A B C D E
1 10 +
1 19 +
1 20 –
1 10 –
2 10 +
2 10 +
2 20 –
2 10 –
R = (A, B, C, D)
S = (E, B, D)
Result schema – (A, B, C, D, E)
r s is defined as:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s A B C D E
1 a
1 a
1 a
1 a
2 b
rs
Suited to queries that include the phrase “for all”.
Let r and s be relations on schemas R and S respectively
where
R = (A1, …, Am, B1, …, Bn)
S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
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
To see why
r-s,s(r) simply reorders attributes of r
r-s(r-s (r) x s) – r-s,s(r)) gives those tuples t in
r-s (r) such that for some tuple u s, tu r.
Relations r, s: A B B
1
1
2
3 2
1 s
1
1
3
4
6
1
2
r s: A r
Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
r s: A B C
a
a
{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 r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
Find the loan number for each loan of an amount greater than
$1200
{t | s loan (t[loan-number] = s[loan-number]
s [amount] 1200}
{t | s borrower(t[customer-name] = s[customer-name])
v u depositor(t[customer-name] = u[customer-name])
{t | s loan(s[branch-name] = “Perryridge”
v u borrower (u[loan-number] = s[loan-number]
t[customer-name] = u[customer-name])
v v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])))}
{t | s branch(s[branch-city] = “Brooklyn”
u account (s[branch-name] = u[branch-name]
v s depositor (t[customer-name] = s[customer-name]
s[account-number] = u[account-number])))}
{ b, l, a | b, l, a loan a 1200}
Find the names of all customers who have a loan of over $1200
{ c, a | l ( c, l borrower
Generalized Projection
Outer Join
Aggregate Functions
Relation loan
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
loan borrower
loan borrower
loan borrower
loan borrower
7
7
3
10
sum-C
sumc(r)
27
rr–E
where r is a relation and E is a relational algebra query.
account
account – branch-name = “Perryridge” (account)
Delete all loan records with amount in the range of 0 to 50
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.
BRANCH-NAME
(BRANCH-NAME = “Perryridge” (all-customer))