Vous êtes sur la page 1sur 96

Chapter 2: Relational Model

Chapter 2: Relational Model


Structure of Relational Databases
Fundamental Relational-Algebra-Operations

Additional Relational-Algebra-Operations

Etended Relational-Algebra-Operations
!ull "alues

Modi#cation of the Database


Eample of a Relation
Attribute $%pes

Each attribute of a relation has a name


$he set of allo&ed 'alues for each attribute is called the domain
of the attribute
Attribute 'alues are (normall%) re*uired to be atomic+ that is,
indi'isible

E-g- the 'alue of an attribute can be an account number,


but cannot be a set of account numbers

Domain is said to be atomic if all its members are atomic


$he special 'alue null is a member of e'er% domain

$he null 'alue causes complications in the de#nition of man%


operations

.e shall ignore the e/ect of null 'alues in our main


presentation and consider their e/ect later
Relation Schema
Formall%, gi'en domains D
0
, D
2
, 1- D
n
a relation r is a subset of
D
0
D
2
1 D
n
$hus, a relation is a set of n-tuples (a
0
, a
2
, 1, a
n
) &here each a
i

D
i

Schema of a relation consists of


attribute de#nitions

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

Order of tuples is irrele'ant (tuples ma% be stored in an


arbitrar% order)
Jones
Smith
Curry
Lindsay
customer_name
Main
North
North
Park
customer_street
Harrison
Rye
Rye
Pittsfeld
customer_city
customer
attributes
(or columns)
tuples
(or rows)
Database
A database consists of multiple relations
3nformation about an enterprise is bro4en up into parts, &ith
each relation storing one part of the information
E-g-
account : information about accounts
depositor : &hich customer o&ns &hich account
customer : information about customers
$he customer Relation
$he depositor Relation
.h% Split 3nformation Across
Relations5

Storing all information as a single relation such as


bank(account_number, balance, customer_name, --)
results in

repetition of information

e-g-,if t&o customers o&n an account (.hat gets repeated5)

the need for null 'alues

e-g-, to represent a customer &ithout an account

!ormali6ation theor% (Chapter 7) deals &ith ho& to design


relational schemas
8e%s
9et 8 R

K is a superkey of R if 'alues for K are su:cient to identif% a uni*ue


tuple of each possible relation r(R)

b% ;possible r < &e mean a relation r that could eist in the


enterprise &e are modeling-

Eample: =customer_name, customer_street> and


=customer_name>
are both super4e%s of Customer, if no t&o customers can possibl%
ha'e the same name

3n real life, an attribute such as customer_id &ould be used


instead of customer_name to uni*uel% identif% customers, but
&e omit it to 4eep our eamples small, and instead assume
customer names are uni*ue-
8e%s (Cont-)

K is a candidate key if K is minimal


Eample: =customer_name> is a candidate 4e% for Customer,
since it is a super4e% and no subset of it is a super4e%-

Primary key: a candidate 4e% chosen as the principal means of


identif%ing tuples &ithin a relation

Should choose an attribute &hose 'alue ne'er, or 'er% rarel%,


changes-
E-g- email address is uni*ue, but ma% change
Foreign 8e%s

A relation schema ma% ha'e an attribute that corresponds to the


primar% 4e% of another relation- $he attribute is called a foreign
key-
E-g- customer_name and account_number attributes of depositor
are foreign 4e%s to customer and account respecti'el%-
Onl% 'alues occurring in the primar% 4e% attribute of the
referenced relation ma% occur in the foreign 4e% attribute of
the referencing relation.
Schema Diagram
?uer% 9anguages

9anguage in &hich user re*uests information from the database-


Categories of languages

@rocedural

!on-procedural, or declarati'e
;@ure< languages:

Relational algebra

$uple relational calculus


Domain relational calculus

@ure languages form underl%ing basis of *uer% languages that


people use-
Relational Algebra

@rocedural language
Si basic operators

select:

proAect:
union:

set di/erence:

Cartesian product:

rename:

$he operators ta4e one or t&o relations as inputs and produce


a ne& relation as a result-
Select Operation B Eample

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

Can build epressions using multiple operations


Eample:
ADC
(r x s)

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

amount > 1200


(loan)

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-

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.

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

All abo'e, other than aggregation, can be epressed using basic


operations &e ha'e seen earlier
Set-3ntersection Operation B
Eample

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

9et r and s be relations on schemas R and ' respecti'el%-


$hen, r s is a relation on schema R ' obtained as follo&s:
Consider each pair of tuples t
r
from r and t
s
from s-
3f t
r
and t
s
ha'e the same 'alue on each of the attributes in R
', add a tuple t to the result, &here
t has the same 'alue as t
r
on r
t has the same 'alue as t
s
on s
Eample:
R D (, (, C, D)
' D (E, (, D)
Result schema D (, (, C, D, E)

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

?uestion: .hich aggregate operations cannot be


epressed using basic relational operations5
Aggregate Operation B
Eample
Relation account grouped b% branc%-name:
branch_name
,
sum(balance)
(account)
branch_nameaccount_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
Aggregate Functions (Cont-)

Result of aggregation does not ha'e a name


Can use rename operation to gi'e it a name

For con'enience, &e permit renaming as part of aggregate


operation
branch_name
,
sum(balance) as sum_balance
(account)
Outer Hoin

An etension of the Aoin operation that a'oids loss of information-


Computes the Aoin and then adds tuples form one relation that
does not match tuples in the other relation to the result of the
Aoin-

Cses null 'alues:

null signi#es that the 'alue is un4no&n or does not eist


All comparisons in'ol'ing null are (roughl% spea4ing) false b%
de#nition-
.e shall stud% precise meaning of comparisons &ith nulls
later
Outer Hoin B Eample
Relation loan

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

Left Outer Join


loan borrower
Outer Hoin B Eample
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

Question: can outerjoins be expressed using basic relational


algebra operations
!ull "alues

3t is possible for tuples to ha'e a null 'alue, denoted b% null,


for some of their attributes

null signi#es an un4no&n 'alue or that a 'alue does not eist-

$he result of an% arithmetic epression in'ol'ing null is null$

Aggregate functions simpl% ignore null 'alues (as in S?9)


For duplicate elimination and grouping, null is treated li4e an%
other 'alue, and t&o nulls are assumed to be the same (as in
S?9)
!ull "alues
Comparisons &ith null 'alues return the special truth 'alue: unknown
3f .alse &as used instead of unknown, then not ( / 0)
&ould not be e*ui'alent to 1) 0
$hree-'alued logic using the truth 'alue unknown:
OR: (unknown or true) D true,
(unknown or .alse) D unknown
(unknown or unknown) ) unknown
A!D: (true and unknown) ) unknown,
(.alse and unknown) ) .alse,
(unknown and unknown) ) unknown
!O$: (not unknown) ) unknown
3n S?9 ;2 is unknown< e'aluates to true if predicate 2 e'aluates
to unknown
Result of select predicate is treated as .alse if it e'aluates to
unknown
Di'ision Operation

!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)

customer_name, loan_number, amount


(borrower loan)

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

Find all customers &ho ha'e an account at all branches located


in Eroo4l%n cit%-

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

Eample: $o eliminate the branc%_name attribute of account



account_number, balance
(account)
) (
, , ,
2 1
r
k
A A A

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)

Eample: to #nd all customers &ith either an account or a loan



customer_name
(depositor)
customer_name
(borrower)
Set Di/erence Operation

!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-

r and s must ha'e the same arit%


attribute domains of r and s must be compatible
Cartesian-@roduct Operation

!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

Etends the proAection operation b% allo&ing arithmetic functions


to be used in the proAection list-

E is an% relational-algebra epression


Each of +
0
, +
2
, 1, +
n
are are arithmetic epressions in'ol'ing
constants and attributes in the schema of E-
Li'en relation credit_in.o(customer_name, limit, credit_balance),
#nd ho& much more each person can spend:

customer_name, limit credit_balance


(credit_in.o)
) ( ,..., ,
2 1
E
n
F F F

Modi#cation of the Database

$he content of the database ma% be modi#ed using the


follo&ing operations:

Deletion
3nsertion

Cpdating

All these operations are epressed using the assignment


operator-
Deletion

A delete re*uest is epressed similarl% to a *uer%, ecept


instead of displa%ing tuples to the user, the selected
tuples are remo'ed from the database-
Can delete onl% &hole tuples+ cannot delete 'alues on
onl% particular attributes
A deletion is epressed in relational algebra b%:
r r B E
&here r is a relation and E is a relational algebra *uer%-
Deletion Eamples

Delete all account records in the @err%ridge branch-

Delete all accounts at branches located in Needham.


r
1


branch_city = Needham
(account branch )
r
2

account_number
,

branch_name, balance
(r
1
)
r
3

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
amount 0and amount 50
(loan)
account account
branch_name = Perryridge
(account )
3nsertion
$o insert data into a relation, &e either:

specif% a tuple to be inserted

&rite a *uer% &hose result is a set of tuples to be inserted


in relational algebra, an insertion is epressed b%:
r r E
&here r is a relation and E is a relational algebra epression-
$he insertion of a single tuple is epressed b% letting E be a
constant relation containing one tuple-
3nsertion Eamples

3nsert information in the database specif%ing that Smith has


F02GG in account A-M7N at the @err%ridge 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
(
branch_name = Perryridge
(borrower loan))
account account
loan_number, branch_name, 200
(r
1
)
depositor depositor
customer_name, loan_number
(r
1
)
Cpdating
A mechanism to change a 'alue in a tuple &ithout charging all
'alues in the tuple
Cse the generali6ed proAection operator to do this tas4
Each +
i
is either

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

Ma4e interest pa%ments b% increasing all balances b% O percent-

Pay all accounts with balances over $10,000 6 percent interest


and pay all others 5 percent
account
account_number, branch_name, balance * 1.06
(
BAL > 10000
(account ))

account_number, branch_name, balance * 1.05
(
BAL 10000
(account))
account
account_number, branch_name, balance * 1.05
(account)
Figure 2-N- $he branc% relation
Figure 2-P: $he loan relation
Figure 2-7: $he borrower
relation
Figure 2-M
Result of
branchQname D ;@err%ridge<
(loan)
Figure 2-0G:
9oan number and the amount
of the loan
Figure 2-00: !ames of all
customers &ho ha'e either an
account or an loan
Figure 2-02:
Customers &ith an account
but no loan
Figure 2-0N: Result of borrower
IRI loan
Figure 2-0S
Figure 2-0O
Figure 2-0P
Figure 2-07
9argest account balance in the
ban4
Figure 2-0T: Customers &ho
li'e on the same street and in
the same cit% as Smith
Figure 2-0M: Customers &ith
both an account and a loan at
the ban4
Figure 2-2G
Figure 2-20
Figure 2-22
Figure 2-2N
Figure 2-2S: $he credit_in.o
relation
Figure 2-2O
Figure 2-2P: $he pt_works
relation
Figure 2-27
$he pt_works relation after
regrouping
Figure 2-2T
Figure 2-2M
Figure 2-NG
$he emplo&ee and .t_works
relations
Figure 2-N0
Figure 2-N2
Figure 2-NN
Figure 2-NS

Vous aimerez peut-être aussi