You are on page 1of 26

Lecture 2

!"#$%&'($# *$%$+$," -'."#


AT71.01 Database Design
August 2014
AIT
Dr Chutiporn Anutariya Dr. Chutiporn Anutariya
chutiporn@gmail.com
2
AT71.01 Database Design
!"#$%&' ()*+,#'- ./ !"#$%&' ()*+,#'- ./
8elaLlonal Model ConcepLs
8elaLlonal Model ConsLralnLs and 8elaLlonal 8elaLlonal Model ConsLralnLs and 8elaLlonal
uaLabase Schemas
u d C l 1 l updaLe CperaLlons, 1ransacLlons,
and ueallng wlLh ConsLralnL vlolaLlons
3
AT71.01 Database Design
012345!632 7!812 )!6)194:
4
AT71.01 Database Design
4*' 0'$+#%;&+$ 8+#+ 7;<'$ 4*' 0'$+#%;&+$ 8+#+ 7;<'$
8elaLlonal model
llrsL commerclal lmplemenLaLlons avallable ln early p y
1980s
Pas been lmplemenLed ln a large number of Pas been lmplemenLed ln a large number of
commerclal sysLem
Pl hl l d L k d l Plerarchlcal and neLwork models
receded Lhe relaLlonal model
5
AT71.01 Database Design
3&+#;=> ;? + 8+#+@+A' 3&+#;=> ;? + 8+#+@+A'
1he lnformaLlon lnslde
Lhe daLabase ls organlzed
lnLo Lables lnLo Lables.
6
AT71.01 Database Design
0'$+#%;&+$ 7;<'$ );&B',#A 0'$+#%;&+$ 7;<'$ );&B',#A
uaLabase = collecLlon of Lables
4+@$' of values 4+@$' of values
8ow
8 L ll Ll f l L d d L l 8epresenLs a collecLlon of relaLed daLa values
lacL LhaL Lyplcally corresponds Lo a realworld enLlLy or
relaLlonshlp relaLlonshlp
!"#$%
1 bl d l 1able name and column names
lnLerpreL Lhe meanlng of Lhe values ln each row &''()*"'%
7
AT71.01 Database Design
0'$+#%;&+$ 7;<'$ );&B',#A (B;&#C<D/ 0'$+#%;&+$ 7;<'$ );&B',#A (B;&# <D/
1able name Columns
8ows
8
AT71.01 Database Design
9
AT71.01 Database Design
1E'-B%A'F 2%@-+-> 8G 1E'-B%A'F 2%@-+-> 8G
D fi l f h t bl
Llbrary u8
Define column for each table
book book
member member
10
AT71.01 Database Design
1E'-B%A'F G+&H 8G 1E'-B%A'F G+&H 8G
D fi l f h t bl
8ank u8
Define column for each table
cusLomer cusLomer
accounL accounL
11
AT71.01 Database Design
1E'-B%A'F !&$%&' :#;-' 8G 1E'-B%A'F !&$%&' :#;-' 8G
CnllneSLore u8
Define column for each table
CnllneSLore u8
producL producL
shopplngCarL shopplngCarL
12
AT71.01 Database Design
8;=+%&A 8;=+%&A
8;=+%& u 8;=+%& u
SeL of aLomlc values
3#;=%B
Lach value lndlvlslble
Speclfylng a domaln
8+#+ #>,' speclfled for each domaln 8+#+ #>,' speclfled for each domaln
Lxamples of uomalns:
CA: osslble values of compuLed CA, each musL be a floaLlngpolnL
number beLween 0 and 4 number beLween 0 and 4
SLudenL_ages: osslble ages of sLudenLs ln a unlverslLy, each musL be an
lnLeger value beLween 13 and 80,
rogram of sLudy: 1he seL of names of programs of sLudy ln a unlverslLy rogram_of_sLudy: 1he seL of names of programs of sLudy ln a unlverslLy,
such as CompuLer Sclence, 8uslness AdmlnlsLraLlon, Llberal ArLs.
rogram_of_sLudy_code: 1he seL of programs of sLudy codes, such as CS,
8A, LA. ,
13
AT71.01 Database Design
0'$+#%;& :B*'=+ 0'$+#%;& :B*'=+
0'$+#%;& AB*'=+ +
uenoLed by +(,
1
, ,
2
, ...,,
-
) y (
1
,
2
, ,
-
)
Made up of a relaLlon name + and a llsL of aLLrlbuLes,
,
1
,
2
, ,
1
, ,
2
, .../ ,
-
3##-%@"#' ,
)
name of a role played by some domaln 0 ln Lhe
relaLlon schema +
14
AT71.01 Database Design
1E+=,$'F 0'$+#%;& :B*'=+ :4I8164 1E+=,$'F 0'$+#%;& :B*'=+ :4I8164
A l Ll hl h L l f Ll b L A relaLlon whlch sLores lnformaLlon abouL
unlverslLy sLudenLs, would conLaln seven
aLLrlbuLes descrlblng each sLudenL: aLLrlbuLes descrlblng each sLudenL:
S1uuLn1(name, Ssn, Pome_phone,
Address Cfflce phone Age Cpa) Address, Cfflce_phone, Age, Cpa)
or (wlLh Lhe daLa Lype of each aLLrlbuLe
speclfled) speclfled)
S1uuLn1(name: sLrlng, Ssn: sLrlng,
Pome phone: sLrlng, Address: sLrlng, _p g, g,
Cfflce_phone: sLrlng, Age: lnLeger, Cpa:
real)
15
AT71.01 Database Design
0'$+#%;&A +&< 4",$'A 0'$+#%;&A +&< 4",$'A
0'$+#%;& (or -'$+#%;& A#+#')
SeL of !#",$'A ( 1 ['
1
, '
2
, ..., '
3
} , [
1
,
2
, ,
3
}
Lach -Luple '
Crdered llsL of - values ' 1 45 5 5 6 Crdered llsL of - values ' 1 45
1
, 5
2
, ..., 5
-
6
Lach value 5
)
, 1 ) -, ls an elemenL of dom(,
)
) or ls a
speclal nuLL value speclal nuLL value
16
AT71.01 Database Design
)*+-+B#'-%A#%BA ;? 4+@$'A )*+-+B#'-%A#%BA ;? 4+@$'A
1he order of rows ln a Lable ls noL lmporLanL 1he order of rows ln a Lable ls noL lmporLanL.
17
AT71.01 Database Design
)*+-+B#'-%A#%BA ;? 4+@$'A )*+-+B#'-%A#%BA ;? 4+@$'A
l d l values and nuLLs ln rows
Lach value ln a row ls aLomlc
J$+# -'$+#%;&+$ =;<'$
ComposlLe and mulLlvalued aLLrlbuLes noL allowed
J%-A# &;-=+$ ?;-= assumpLlon
7"$#%K+$"'< +##-%@"#'A
MusL be represenLed by separaLe relaLlons
);=,;A%#' +##-%@"#'A
8epresenLed only by slmple componenL aLLrlbuLes ln baslc
relaLlonal model
18
AT71.01 Database Design
)*+-+B#'-%A#%BA ;? 4+@$'A (B;&#C<D/ )*+-+B#'-%A#%BA ;? 4+@$'A (B;&# <D/
nuLL values
8epresenL Lhe values of aLLrlbuLes LhaL may be p y
unknown or may noL apply Lo a row
Meanlngs for nuLL values Meanlngs for nuLL values
7&$"% "-8-9:-
7&$"% %;)<'< *"' )< -9' &5&)$&*$% 7&$"% %;)<'< *"' )< -9' &5&)$&*$%
,''()*"'% =9%< -9' &##$> '9 '?)< '"#$% @&8&. 5&$"% "-=%A)-%=B
19
AT71.01 Database Design
)*+-+B#'-%A#%BA ;? 4+@$'A (B;&#C<D/ )*+-+B#'-%A#%BA ;? 4+@$'A (B;&# <D/
lnLerpreLaLlon (meanlng) of a Lable
Lach row ln Lhe Lable ls a ?+B# or a parLlcular lnsLance p
of Lhe asserLlon
20
AT71.01 Database Design
012345!632 7!812 )!6:403564:
21
AT71.01 Database Design
0'$+#%;&+$ 7;<'$ );&A#-+%&#A 0'$+#%;&+$ 7;<'$ );&A#-+%&#A
ConsLralnLs
8esLrlcLlons on Lhe acLual values ln a daLabase sLaLe
uerlved from Lhe rules ln Lhe appllcaLlon LhaL Lhe
daLabase represenLs daLabase represenLs
22
AT71.01 Database Design
)+#'L;-%'A ;? 0'$+#%;&+$ 7;<'$ );&A#-+%&#A )+#'L;-%'A ;? 0'$+#%;&+$ 7;<'$ );&A#-+%&#A
M 5 * # < $ @ < # % # % $% %# # % # MD 5&*'-'&# =;<'$@+A'< B;&A#-+%&#A or %=,$%B%# B;&A#-+%&#A
lnherenL ln Lhe daLa model
1he characLerlsLlcs of relaLlons dlscussed earller belong Lo Lhls g
caLegory
Lx: 1he consLralnL LhaL a relaLlon cannoL have dupllcaLe Luples
N :B*'=+ @+A'< B;&A#-+%&#A or 'E,$%B%# B;&A#-+%&#A ND :B*'=+@+A'< B;&A#-+%&#A or 'E,$%B%# B;&A#-+%&#A
Can be dlrecLly expressed ln schemas of Lhe daLa model
Lx: uomaln consLralnLs, key consLralnLs, nuLL value consLralnLs, eLc.
( ) (Lo be dlscussed nexL)
.D 3,,$%B+#%;&@+A'< or A'=+&#%B B;&A#-+%&#A or @"A%&'AA -"$'A
CannoL be dlrecLly expressed ln schemas CannoL be dlrecLly expressed ln schemas
Lxpressed and enforced by appllcaLlon program
23
AT71.01 Database Design
8;=+%& +&< 8+#+ 4>,' );&A#-+%&#A 8;=+%& +&< 8+#+ 4>,' );&A#-+%&#A
u l L l L lf Lh L lLhl h Lh l uomaln consLralnLs speclfy LhaL wlLhln each row, Lhe value
of each column A musL be an aLomlc value from Lhe domaln
dom(A). ( )
1yplcally lnclude:
numerlc daLa Lypes for lnLegers and real numbers
CharacLers
8ooleans
ll d l Lh L l llxedlengLh sLrlngs
varlablelengLh sLrlngs
uaLe, Llme, LlmesLamp uaLe, Llme, LlmesLamp
Money
CLher speclal daLa Lypes
24
AT71.01 Database Design
7;A# IA'?"$ 8+#+ 4>,'A 7;A# IA'?"$ 8+#+ 4>,'A
25
AT71.01 Database Design
8+#+ 4>,'AF 4'E# 8+#+ 4>,'AF 4'E#
26
AT71.01 Database Design
8+#+ 4>,'AF 6"=@'- 8+#+ 4>,'AF 6"=@'-
27
AT71.01 Database Design
8+#+ 4>,'AF 8+#'O4%=' 8+#+ 4>,'AF 8+#'O4%='
28
AT71.01 Database Design
1E'-B%A'F P*%B* 8+#+ 4>,'Q 1E'-B%A'F P*%B* 8+#+ 4>,'Q
29
AT71.01 Database Design
R'> );&A#-+%&#A R'> );&A#-+%&#A
no Lwo rows can have Lhe same comblnaLlon of
values for all key aLLrlbuLes. y
30
AT71.01 Database Design
R'> );&A#-+%&#A R'> );&A#-+%&#A
h h bl l f no Lwo rows can have Lhe same comblnaLlon of
values for all key aLLrlbuLes.
:",'-H'> (:R/
A superkey Sk speclfles a unlqueness consLralnL LhaL A superkey Sk speclfles a unlqueness consLralnL LhaL
no Lwo dlsLlncL Luples ln 8 can have Lhe same value for
Sk.
R'>
Superkey of 8 Superkey of 8
8emovlng any aLLrlbuLe , from C leaves a seL of
aLLrlbuLes C LhaL ls noL a superkey of + any more aLLrlbuLes C LhaL ls noL a superkey of + any more
31
AT71.01 Database Design
R'> );&A#-+%&#A (B;&#C<D/ R'> );&A#-+%&#A (B;&# <D/
key saLlsfles Lwo properLles:
1wo dlsLlncL rows ln a Lable cannoL have ldenLlcal
values for (all) aLLrlbuLes ln key
Mlnlmal superkey Mlnlmal superkey
CannoL remove any aLLrlbuLes and sLlll have unlqueness
consLralnL ln above condlLlon hold consLralnL ln above condlLlon hold
32
AT71.01 Database Design
R'> );&A#-+%&#A (B;&#C<D/ R'> );&A#-+%&#A (B;&# <D/
)+&<%<+#' H'>
8elaLlon schema may have more Lhan one key y y
9-%=+-> H'> of Lhe relaLlon
u l d dld k ueslgnaLed among candldaLe keys
underllne aLLrlbuLe
CLher candldaLe keys are deslgnaLed as "&%S"'
H'>A H'>A
33
AT71.01 Database Design
9-%=+-> R'> );&A#-+%&#A 9-%=+-> R'> );&A#-+%&#A
A prlmary key ls a column ln your Lable LhaL
makes each record unlque.
1he prlmary key ls used Lo unlquely ldenLlfy each record
Whlch means LhaL Lhe daLa ln Lhe prlmary key column can'L
b L d be repeaLed.
Conslder a Lable wlLh Lhe columns shown below. uo you
Lhlnk any of Lhose would make good prlmary keys? Lhlnk any of Lhose would make good prlmary keys?
34
AT71.01 Database Design
);&A#-+%&#A ;& 6I22 T+$"'A );&A#-+%&#A ;& 6I22 T+$"'A
A nuLL value consLralnL speclfles wheLher nuLL
values are or are noL permlLLed. p
Lxample
lf S1uuLn1 L h lld L nuLL lf every S1uuLn1 row musL have a valld, noLnuLL
value for Lhe name aLLrlbuLe, Lhen name of S1uuLn1
l L l d L b 6!4 6I22 ls consLralned Lo be 6!4 6I22.
35
AT71.01 Database Design
);&A#-+%&#A ;& 0'$+#%;&+$ 8+#+@+A' :B*'=+ );&A#-+%&#A ;& 0'$+#%;&+$ 8+#+@+A' :B*'=+
0'$+#%;&+$ <+#+@+A' AB*'=+ D
SeL of relaLlon schemas D 1 [+
1
, +
2
, ..., +
3
} [
1
,
2
, ,
3
}
SeL of lnLegrlLy consLralnLs lC
36
AT71.01 Database Design
1E+=,$'F 0'$+#%;&+$ 8+#+@+A' :B*'=+
)!7936U
37
AT71.01 Database Design
5&#'L-%#>V 0'?'-'&#%+$ 5&#'L-%#>V
< % +&< J;-'%L& R'>A
1&#%#> %&#'L-%#> B;&A#-+%&#
no prlmary key value can be nuLL p y y
0'?'-'&#%+$ %&#'L-%#> (;- J;-'%L& R'>/ B;&A#-+%&#
C l bl ConnecLlng Lwo Lables
Speclfled beLween Lwo Lables
MalnLalns conslsLency among rows ln Lwo Lables
38
AT71.01 Database Design
J;-'%L& R'> );&A#-+%&#A J;-'%L& R'> );&A#-+%&#A
1he lC8LlCn kL? (lk) ls a column ln a Lable LhaL
references Lhe 8lMA8? kL? of anoLher Lable.
Lxample:
Foreign Key
39
AT71.01 Database Design
5&#'L-%#>V 0'?'-'&#%+$ 5&#'L-%#>V
< % ( C< / +&< J;-'%L& R'>A (B;&#C<D/
J;-'%L& H'> rules:
1he aLLrlbuLes ln lk have Lhe same domaln(s) as Lhe ( )
prlmary key aLLrlbuLes k
value of lk ln a row '
1
elLher value of lk ln a row '
1
elLher
occurs as a value of k for some row '
2
/ or
ls nuLL ls nuLL
40
AT71.01 Database Design
5&#'L-%#>V 0'?'-'&#%+$ 5&#'L-%#>V
< % ( C< / +&< J;-'%L& R'>A (B;&#C<D/
ulagrammaLlcally dlsplay referenLlal lnLegrlLy
consLralnLs
ulrecLed arc from each forelgn key Lo Lhe relaLlon lL
references references
All lnLegrlLy consLralnLs should be speclfled on
l l l d b h relaLlonal daLabase schema
41
AT71.01 Database Design
1E+=,$'F 0'?'-'&#%+$ 5&#'L-%#> W J;-'%L& R'>
% );&A#-+%&#A
42
AT71.01 Database Design
!#*'- 4>,'A ;? );&A#-+%&#A !#*'- 4>,'A ;? );&A#-+%&#A
:'=+&#%B %&#'L-%#> B;&A#-+%&#A
May have Lo be speclfled and enforced on a relaLlonal y p
daLabase
use #-%LL'-A and +AA'-#%;&A use #-%LL'-A and +AA'-#%;&A
More common Lo check for Lhese Lypes of consLralnLs
wlLhln Lhe appllcaLlon programs wlLhln Lhe appllcaLlon programs
43
AT71.01 Database Design
!#*'- 4>,'A ;? );&A#-+%&#A (B;&#C<D/ !#*'- 4>,'A ;? );&A#-+%&#A (B;&# <D/
% $ < < % J"&B#%;&+$ <','&<'&B> B;&A#-+%&#
LsLabllshes a funcLlonal relaLlonshlp among Lwo seLs
of aLLrlbuLes E and F
value of E deLermlnes a unlque value of F
:#+#' B;&A#-+%&#A
ueflne Lhe consLralnLs LhaL a valld sLaLe of Lhe ueflne Lhe consLralnLs LhaL a valld sLaLe of Lhe
daLabase musL saLlsfy
4-+&A%#%;& B;&A#-+%&#A 4-+&A%#%;& B;&A#-+%&#A
ueflne Lo deal wlLh sLaLe changes ln Lhe daLabase
44
AT71.01 Database Design
I98341 !910345!6: 4036:3)45!6: 368 I98341 !910345!6:V 4036:3)45!6:V 368
813256X P54Y )!6:403564 T5!2345!6:
45
AT71.01 Database Design
I,<+#' !,'-+#%;&AV 4-+&A+B#%;&AV +&<
$% % * % % $ % 8'+$%&L Z%#* );&A#-+%&# T%;$+#%;&A
CperaLlons of Lhe relaLlonal model can be
caLegorlzed lnLo reLrlevals and updaLes g p
8aslc operaLlons LhaL change Lhe sLaLes of
relaLlons ln Lhe daLabase: relaLlons ln Lhe daLabase:
lnserL
ueleLe
updaLe (or Modlfy) p ( y)
46
AT71.01 Database Design
4*' 5&A'-# !,'-+#%;& 4*' 5&A'-# !,'-+#%;&
rovldes a llsL of aLLrlbuLe values for a new row Lo
be lnserLed lnLo a Lable
Can vlolaLe any of Lhe consLralnLs dlscussed
earller earller
lf an lnserLlon vlolaLes one or more consLralnLs
uefaulL opLlon ls Lo re[ecL Lhe lnserLlon
47
AT71.01 Database Design
4*' 8'$'#' !,'-+#%;& 4*' 8'$'#' !,'-+#%;&
Can vlolaLe only referenLlal lnLegrlLy
lf Lhe row belng deleLed ls referenced by forelgn keys from
oLher row
0'A#-%B#
8e[ecL Lhe deleLlon
)+AB+<'
ropagaLe Lhe deleLlon by deleLlng rows LhaL reference Lhe row
LhaL ls belng deleLed
:'# &"$$ or A'# <'?+"$#
Modlfy Lhe referenclng aLLrlbuLe values LhaL cause Lhe vlolaLlon
48
AT71.01 Database Design
4*' I,<+#' !,'-+#%;& 4*' I,<+#' !,'-+#%;&
lf dl l lb f necessary Lo speclfy a condlLlon on aLLrlbuLes of
relaLlon
SelecL Lhe row (or rows) Lo be modlfled
lf aLLrlbuLe noL parL of a prlmary key nor of a forelgn
key
usually causes no problems
Cnly need Lo check LhaL Lhe new values ls of Lhe correcL
daLa Lype and domaln
updaLlng a prlmary/forelgn key
Slmllar lssues as wlLh lnserL/ueleLe
49
AT71.01 Database Design
:I7730U
50
AT71.01 Database Design
:"==+-> :"==+->
CharacLerlsLlcs dlfferenLlaLe relaLlons from
ordlnary Lables or flles y
Classlfy daLabase consLralnLs lnLo:
l h L d l b d L l L ll lL h lnherenL modelbased consLralnLs, expllclL schema
based consLralnLs, and appllcaLlonbased consLralnLs
ModlflcaLlon operaLlons on Lhe relaLlonal model:
lnserL, ueleLe, and updaLe lnserL, ueleLe, and updaLe
51
AT71.01 Database Design

You might also like