Académique Documents
Professionnel Documents
Culture Documents
Additional Relational-Algebra-Operations
Etended Relational-Algebra-Operations
!ull "alues
name
t%pe2domain
integrit% constraints
Relation 3nstance
$he current 'alues (relation instance) of a relation are speci#ed
b% a table
An element t of r is a tuple, represented b% a row in a table
repetition of information
@rocedural
!on-procedural, or declarati'e
;@ure< languages:
Relational algebra
@rocedural language
Si basic operators
select:
proAect:
union:
set di/erence:
Cartesian product:
rename:
Relation r
A B C D
1
5
12
23
7
7
3
10
A=B ^ D > 5
(r)
A B C D
1
23
7
10
@roAect Operation B Eample
Relation r:
A B C
10
20
30
40
1
1
1
2
A C
1
1
1
2
=
A C
1
1
2
A,C
(r)
Cnion Operation B Eample
Relations r, s:
r s:
A B
1
2
1
A B
2
3
r
s
A B
1
2
1
3
Set Di/erence Operation B
Eample
Relations r, s:
r s:
A B
1
2
1
A B
2
3
r
s
A B
1
1
Cartesian-@roduct Operation B
Eample
Relations r, s:
r x s:
A B
1
2
A B
1
1
1
1
2
2
2
2
C D
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
C D
10
10
20
10
E
a
a
b
b
r
s
Rename Operation
Allo&s us to name, and therefore to refer to, the results of
relational-algebra epressions-
Allo&s us to refer to a relation b% more than one name-
Eample:
x
(E)
returns the epression E under the name X
3f a relational-algebra epression E has arit% n, then
returns the result of epression E under the name X, and &ith the
attributes renamed to
!
,
"
, #$,
n
-
) (
) ,..., , (
2 1
E
n
A A A x
Composition of Operations
r x s
ADC
(r x s)
A B
1
1
1
1
2
2
2
2
C D
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
A B C D E
1
2
2
10
10
20
a
a
b
Ean4ing Eample
branc% (branc%_name, branc%_cit&, assets)
customer (customer_name, customer_street,
customer_cit&)
account (account_number, branc%_name, balance)
loan (loan_number, branc%_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Eample ?ueries
Find all loans of o'er F02GG
Find the loan number for each loan of an amount greater than
$1200
loan_number
(
amount
> 1200
(loan))
Find the names of all customers who have a loan, an account, or both,
from the bank
customer_name
(borrower)
customer_name
(depositor)
Eample ?ueries
Find the names of all customers &ho ha'e a loan at the @err%ridge
branch-
customer_name
(
branch_name = Perryridge
(
borrower.loan_number = loan.loan_number
(borrower x loan)))
customer_name
(depositor)
customer_name
(
branch_name=Perryridge
(
borrower.loan_number = loan.loan_number
(borrower x loan)))
Eample ?ueries
Find the names of all customers &ho ha'e a loan at the @err%ridge
branch-
customer_name
(
loan.loan_number = borrower.loan_number
(
(
branch_name = Perryridge
(loan)) x borrower))
customer_name
(
branch_name = Perryridge
(
borrower.loan_number = loan.loan_number
(borrower x loan)))
Additional Operations
Additional Operations
Set intersection
!atural Aoin
Aggregation
Outer Hoin
Di'ision
Relation r, s:
r s
A B
1
2
1
A B
2
3
r s
A B
2
!atural Hoin Operation B
Eample
Relations r, s:
A B
1
2
4
1
2
C D
a
a
b
a
b
B
1
3
1
2
3
D
a
a
a
b
b
E
r
A B
1
1
1
1
2
C D
a
a
a
a
b
E
r s
Notation: r s
!atural-Hoin Operation
r s is de#ned as:
r$, r$(, r$C, r$D, s$E
(
r$( ) s$(
r$D ) s$D
(r s))
Ean4 Eample ?ueries
Find the largest account balance
Strateg%:
Find those balances that are not the largest
Rename account relation as d so that &e can compare each
account balance &ith all others
Cse set di/erence to #nd those account balances that &ere not
found in the earlier step-
$he *uer% is:
balance
(account) -
account.balance
(
account.balance < d.balance
(account x
d
(account)))
Aggregate Functions and
Operations
Aggregation function ta4es a collection of 'alues and returns a
single 'alue as a result-
avg: a'erage 'alue
min: minimum 'alue
max: maimum 'alue
sum: sum of 'alues
count: number of 'alues
Aggregate operation in relational algebra
E is an% relational-algebra epression
*
!
, *
"
1, *
n
is a list of attributes on &hich to group (can be empt%)
Each +
i
is an aggregate function
Each
i
is an attribute name
) (
) ( , , ( ), ( , , ,
2 2 1 1 2 1
E
n n n
A F A F A F G G G
Aggregate Operation B
Eample
Relation r:
A B
C
7
7
3
10
,
sum(c)
(r)
sum(c )
27
Relation borrower
customer_nameloan_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
Outer Hoin B Eample
Hoin
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
!otation:
Suited to *ueries that include the phrase ;for all<-
9et r and s be relations on schemas R and ' respecti'el%
&here
R D (
0
, 1,
m
, (
0
, 1, (
n
)
' D ((
0
, 1, (
n
)
$he result of r s is a relation on schema
R B ' D (
0
, 1,
m
)
r s D = t I t
R-'
(r) u s ( tu r ) >
.here tu means the concatenation of tuples t and u to
produce a single tuple
r s
Di'ision Operation B Eample
Relations r, s:
r s:
A
B
1
2
A B
1
2
3
1
1
1
3
4
6
1
2
r
s
Another Di'ision Eample
A B
a
a
a
a
a
a
a
a
C D
a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
Relations r, s:
r s:
D
a
b
E
1
1
A B
a
a
C
r
s
Di'ision Operation (Cont-)
@ropert%
9et 3 ) r s
$hen 3 is the largest relation satisf%ing 3 s r
De#nition in terms of the basic algebra operation
9et r(R) and s(') be relations, and let ' R
r s D
R-'
(r ) B
R-'
( (
R-'
(r ) s ) B
R-','
(r ))
$o see &h%
R-','
(r) simpl% reorders attributes of r
R-'
(
R-'
(r ) s ) B
R-','
(r) ) gi'es those tuples t in
R-'
(r ) such that for some tuple u s, tu r-
Ean4 Eample ?ueries
Find the name of all customers &ho ha'e a loan at the ban4 and
the loan amount
Find the names of all customers who have a loan and an account at
bank.
customer_name
(borrower)
customer_name
(depositor)
Query 1
customer_name
(
branch_name = Downtown
(depositor account ))
customer_name
(
branch_name = Uptown
(depositor account))
Query 2
customer_name, branch_name
(depositor account)
temp(branch_name)
({(Downtown ), (Uptown )})
Note that Query 2 uses a constant relation.
Ean4 Eample ?ueries
Find all customers &ho ha'e an account from at least the
;Do&nto&n< and the Cpto&n< branches-
Ean4 Eample ?ueries
customer_name, branch_name
(depositor account)
branch_name
(
branch_city = Brooklyn
(branch))
End of Chapter 2
Formal De#nition
A basic epression in the relational algebra consists of either one of the
follo&ing:
A relation in the database
A constant relation
9et E
!
and E
"
be relational-algebra epressions+ the follo&ing are all
relational-algebra epressions:
E
!
E
"
E
!
B E
"
E
!
E
"
p
(E
!
), 2 is a predicate on attributes in E
!
s
(E
!
), ' is a list consisting of some of the attributes in E
!
x
(E
!
), is the ne& name for the result of E
!
Select Operation
!otation:
p
(r)
p is called the selection predicate
De#ned as:
p
(r) D =t I t r and p(t)>
.here p is a formula in propositional calculus consisting of terms
connected b% : (and), (or), (not)
Each term is one of:
JattributeK op JattributeK or JconstantK
&here op is one of: D, , K, - J-
Eample of selection:
branc%_name)42err&rid,e5
(account)
@roAect Operation
!otation:
&here
!
,
"
are attribute names and r is a relation name-
$he result is de#ned as the relation of k columns obtained b%
erasing the columns that are not listed
Duplicate ro&s remo'ed from result, since relations are sets
Cnion Operation
!otation: r s
De#ned as:
r s D =t I t r or t s>
For r s to be 'alid-
0- r, s must ha'e the same arity (same number of attributes)
2- $he attribute domains must be compatible (eample: 2
nd
column
of r deals &ith the same t%pe of 'alues as does the 2
nd
column of s)
!otation r s
De#ned as:
r s D =t I t r and t s>
Set di/erences must be ta4en bet&een compatible
relations-
!otation r s
De#ned as:
r s D =t 3 I t r and 3 s>
Assume that attributes of r(R) and s(S) are disAoint- ($hat is, R
' D )-
3f attributes of r and s are not disAoint, then renaming must be
used-
Set-3ntersection Operation
!otation: r s
De#ned as:
r s D = t I t r and t s >
Assume:
r, s ha'e the same arit&
attributes of r and s are compatible
!ote: r s D r B (r B s)
Assignment Operation
$he assignment operation () pro'ides a con'enient &a% to epress
comple *ueries-
.rite *uer% as a se*uential program consisting of
a series of assignments
follo&ed b% an epression &hose 'alue is displa%ed as a result of
the *uer%-
Assignment must al&a%s be made to a temporar% relation 'ariable-
Eample: .rite r s as
temp!
R-'
(r )
temp"
R-'
((temp! s ) B
R-','
(r ))
result D temp! B temp"
$he result to the right of the is assigned to the relation 'ariable on
the left of the -
Ma% use 'ariable in subse*uent epressions-
Etended Relational-Algebra-
Operations
Lenerali6ed @roAection
Aggregate Functions
Outer Hoin
Lenerali6ed @roAection
Deletion
3nsertion
Cpdating
the 6
th
attribute of r, if the 6
th
attribute is not updated, or,
if the attribute is to be updated F
i
is an epression, in'ol'ing
onl% constants and the attributes of r, &hich gi'es the ne&
'alue for the attribute
) (
, , , ,
2 1
r r
l
F F F
Cpdate Eamples