Unit 3
Unit 3
Customer-schema =
E.g.
(customer-name, customer-street,
customer-city)
r(R) is a relation on the relation schema R
attributes
(or columns)
customer-name
customer-streetcustomer-city
customer
• Relation r A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Notation: p(r)
p is called the selection predicate
Defined as:
Relation r: A B C
10 1
20 1
30 1
40 2
A C A C
A,C (r)
1 1
1 = 1
1 2
2
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 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
1
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 C D E
1 10 a
10 a
2 20 b
r 10 b
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S) are disjoint.
(That is,
R S = ).
If attributes of r(R) and s(S) are not disjoint, then
renaming must be used.
A=C(r x s)
A B C D E
1 10 a
2 20 a
2 20 b
Database System Concepts 3.27 ©Silberschatz, Korth and Sudarshan
Rename Operation
Allows us to name, and therefore to refer to, the
results of relational-algebra expressions.
Allows us to refer to a relation by more than one
name.
Example:
x (E )
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
x (A1, A2, …, An) (E)
returns the result of expression E under the name X,
and with the
attributes renamed to A1, A2, …., An.
customer-name (branch-name=“Perryridge”
(borrower.loan-number = loan.loan-number(borrower x
loan))) –
customer-name(depositor)
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
balance(account) - account.balance
Set intersection
Natural join
Division
Assignment
r s
rs
A B
2
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)
r s = { t | t R-S(r) 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
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
CN(BN=“Downtown”(depositor account))
CN(BN=“Uptown”(depositor account))
Query 2
Generalized Projection
Outer Join
Aggregate Functions
F1, F2, …, Fn (E )
E is any relational-algebra expression
Each of F1, F2, …, Fn are are arithmetic expressions
involving constants and attributes in the schema of
E.
Given relation credit-info(customer-name, limit,
credit-balance), find how much more each person
can spend:
customer-name, limit – credit-balance (credit-info)
A B C
7
7
3
10
sum-C
g sum(c) (r)
27
branch-nameaccount-number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700
Relation loan
loan-numberbranch-name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-nameloan-number
Jones L-170
Smith L-230
Hayes L-155
loan Borrower
rr–E
where r is a relation and E is a relational algebra
query.
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
{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
{t | s loan(s[branch-name] = “Perryridge”
u borrower (u[loan-number] = s[loan-number]
t [customer-name] = u[customer-name])
v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])
Find the names of all customers who have a loan of over $1200
{ c | l ({ c, l borrower
b,a( l, b, a loan b = “Perryridge”))
a( c, a depositor
b,n( a, b, n account b = “Perryridge”))}