100% found this document useful (1 vote)
184 views

DBMS - Model - QP - Answers (1)

Uploaded by

Roshan Anegundi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
184 views

DBMS - Model - QP - Answers (1)

Uploaded by

Roshan Anegundi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

ModelQuest

ionPaper
-DBMS

Q1.a)
Compar
eDBMSandear
lyf
il
esystemsbr
ingi
ngoutt
hemaj
oradvantagesof
t
he database approach.
(6)

Dr
awbacksofFi
lesy
stem

Datar edundancy:Dataredundancy r
efer
st othe dupli
cat
ion ofdat
a.Dat
a
redundancyoft
enleadst
ohigherst
oragecost
sandpooraccessti
me.

Datainconsistency:Dat aredundancyl eadst odatainconsistency,letstakethe


sameex amplet hatwehav etakenabov e,ast udenti
senrol
ledf ortwocour sesand
wehav estudentaddr essstor
edtwice,nowl etssaystudentrequest stochangehis
addr
ess, i
ftheaddr essischangedatonepl aceandnotonal lther ecordst
hent hi
s
canleadtodatai nconsist
ency.

Dat
aI sol
ati
on:Becausedat
aarescat t
eredinvar
iousfi
les,andfil
esmaybei n
di
ff
erentfor
mats,
wr i
ti
ngnewappl
icat
ionprogr
amst or
etr
ievetheappropr
iat
edat
a
i
sdiff
icul
t.

Dependencyonappl
icat
ionpr
ogr
ams:Changi
ngf
il
eswoul
dleadt
ochangei
n
appl
icat
ionpr
ogr
ams.

DataSecuri
ty:Datashouldbesecuredfr
om unaut
hori
sedaccess,forexamplea
studenti
nacol l
egeshouldnotbeabletoseethepayr
olldetai
lsoftheteacher
s,
suchkindofsecuri
tyconst
rai
ntsar
edif
fi
cultt
oappl
yinfi
leprocessi
ngsystems.

Advant
agesofUsi
ngt
heDBMSAppr
oach

1.Cont
rol
li
ngRedundancy
:

Dataredundancy(suchast endstooccuri
nthe"f
il
eprocessi
ng"approach)leadst
o
wastedstoragespace,dupli
cati
onofeffor
t(whenmulti
plecopiesofadatum need
tobeupdated),andahi gherl
ikel
ihoodoft
hei
ntr
oduct
ionofinconsi
stency.

Ontheotherhand,redundancycanbeusedt oi
mproveper
for
manceofquer i
es.
I
ndexes,f
orex ample,areent
ir
elyredundant
,buthelptheDBMS in pr
ocessi
ng
quer
iesmorequickl
y.

A DBMSshoul dprovi
det hecapabi
li
tytoaut
omati
cal
lyenf
orcet
her
ulet
hatno
i
nconsi
stenci
esareint
roducedwhendatai
supdat
ed.
2.Rest
ri
cti
ngUnaut
hor
izedAccess:

A DBMSshoul dprovi
deasecurit
yandaut hor
izat
ionsubsystem,whi
cht
heDBA
usestocreat
eaccountsandt
ospecifyaccountr
estri
cti
ons.Then,t
heDBMSshoul
d
enfor
cetheserest
ri
cti
onsaut
omaticall
y.

3.Pr
ovi
dingPer
sist
entSt
oragef
orPr
ogr
am Obj
ect
s:

Obj
ect-orient
eddat
abasesystemsmakei teasi
erf
orcompl
exrunt
imeobject
s(e.g.
,
l
ist
s,trees)tobesavedi nsecondar
yst or
agesoast osurv
ivebeyondprogram
t
erminati
onandtoberetr
ievableatalat
erti
me.

4.Provi
ding St
orage St
ruct
ures and Sear
ch Techni
ques f
orEf
fi
cientQuer
y
Pr
ocessi
ng:

Databasesystemsmustpr ovidecapabili
ti
esforeffi
cientl
yexecuti
ngquer
iesand
updates.The quer y processing and opt i
mizat
ion modul e of t
he DBMS is
responsi
bleforchoosi
nganef fi
cientquer
yexecuti
onpl anforeachquer
ybasedon
theexist
ingstor
agestructures.

5.Pr
ovi
dingBackupandRecover
y:

ADBMSmustpr ovidefaci
li
tiesforrecoveri
ngfrom hardwareorsof t
war efai
lures.
Thebackupandr ecoverysubsystem oftheDBMSi sresponsi
bleforr ecov
ery.The
recov
erysubsystem couldensuret hatthetr
ansact
ionisresumedf rom thepointat
whichitwasint
erruptedsot hati
tsfulleff
ectisr
ecordedinthedatabase.

6.Pr
ovi
dingMul
ti
pleUserI
nter
faces:

Manyt ypesofuser swit


hv ary
inglevel
soft echni
calknowl
edgeuseadat abase,a
DBMSshoul dprovi
deav ari
etyofuseri
nterf
aces.Forexampl
e,querylanguagesfor
casualuser s,programming language int
erf
aces forappl
icat
ion programmer s,
formsand/ orcommandcodesf orparametri
cusers,menu-dri
v eninter
facesfor
standal
oneusers.

7.Repr
esent
ingCompl
exRel
ati
onshi
psAmongDat
a:

ADBMSmusthav et
hecapabil
it
yt or epresentav
ariet
yofcomplexr
elati
onshi
ps
amongthedata,todef
inenew r elat
ionshipsastheyari
se,andtoretr
ieveand
updat
erel
ateddat
aeasi
lyandef
ficientl
y.

8.Enf
orci
ngI
ntegr
it
yConst
rai
nts:

Mostdat
abaseappl
icat
ionshav
ecer
tai
nint
egr
it
yconst
rai
ntst
hatmusthol
dfort
he
dat
a.
Thesi
mpl
estt
ypeofi
ntegr
it
yconst
rai
nti
nvol
vesspeci
fyi
ngadat
aty
pef
oreach
dat
ait
em.

Amorecompl extypeofconstr
aintt
hatf
requent
lyoccur
sinvolv
esspeci
fyi
ngt
hata
r
ecordi
nonef il
emustber el
atedtorecordsinotherfi
les.Thi
sisknownasa
r
efer
ent
iali
ntegri
tyconstr
aint
.

Anot
herty
peofconstrai
ntspecif
iesuni
quenessondat
ait
em v
alues,t
hisi
sknown
asakeyoruni
quenessconstr
aint.

9.Per
mit
ti
ngI
nfer
enci
ngandAct
ionsvi
aRul
es:

I
nadeductiv
edat abasesy
stem,onemayspeci
fydecl
arat
iver
ulest
hatal
low t
he
dat
abaset
oinfernewdata!

E.g.,
Figureoutwhichst
udentsar
eonacademicpr
obat
ion.Suchcapabi
li
ti
eswould
taket heplaceofapplicat
ionprogr
amsthatwouldbeusedt o ascert
ainsuch
i
nf or
mationother
wise.

Acti
vedat
abasesystemsgoonest
epf ur
therbyal
lowi
ng"
act
iver
ules"t
hatcanbe
usedtoi
nit
iat
eact
ionsaut
omati
cal
ly.

Q1b)
Wit
haneatbl
ockdi
agr
am,
expl
aint
hear
chi
tect
ureofat
ypi
calDBMS.(
10)

TheThr
ee-SchemaAr
chi
tect
ure

Thegoalofthethree-schemaar
chi
tect
urei
stosepar
atet
heuserappl
icat
ions
fr
om t
hephysi
caldatabase

I
nthi
sar
chi
tect
ure,
schemascanbedef
inedatt
hef
oll
owi
ngt
hreel
evel
s:

1.Thei
nter
nall
evelhasani
nter
nalschema,

-descr
ibest
hephy
sicalst
oragest
ruct
ureoft
hedat
abase.

-usesaphy
sicaldat
amodelanddescr
ibest
hecompl
etedet
ail
sofdat
ast
orage
andaccesspat
hsf
ort
hedat
abase.

2.Theconcept
uall
evelhasaconcept
ualschema,

-descr
ibest
hest
ruct
ureoft
hewhol
edat
abasef
oracommuni
tyofuser
s

-hi
desthedet
ail
sofphy si
calst
oragestr
ucturesandconcentrat
esondescr
ibi
ng
ent
it
ies,
dat
aty
pes,r
elat
ionshi
ps,
useroperat
ions,andconst
rai
nts

-represent
ati
onaldat
amodeli
susedt
odescr
ibet
heconcept
ualschemawhena
databasesyst
em is

I
mpl
ement
ed.

-Thi
simpl
ementat
ionconcept
ualschemai
sof
tenbasedonaconcept
ualschema
desi
gni
nahigh-l
eveldat
amodel.

3.Theext
ernalorvi
ewl
eveli
ncl
udesanumberofext
ernalschemasoruserv
iews,

-descr
ibesthepartoft
hedatabasethatapart
icul
arusergr
oupi
sint
erest
edi
nand
hi
destherestofthedat
abasefrom t
hatusergroup.

-Asi nt heprevi
ouslevel
,eachex t
ernalschemai
sty
pical
lyimplement
edusi
nga
representat
ionaldat
amodel ,possi
blybasedonanexternalschemadesi
gnina
high-leveldatamodel.

TheDBMS mustt r
ansf
orm arequestspeci
fi
edonanexternalschemaintoa
requestagainsttheconcept
ualschema,andt heni
ntoar
equestont hei
nter
nal
schemaf orprocessi
ngovert
hestoreddat
abase.

I
ftherequestisadat abaseretri
eval,thedataextract
edf r
om t
hestor
eddat abase
mustbe r ef
ormatted to mat ch the user’s ext
ernalv i
ew.The processes of
t
ransf
ormingrequestsandr esult
sbet weenlevel
sar ecal
ledmappi
ngs.

Thesemappi
ngsmaybetime-consumi
ng,sosomeDBMSs—especi al
lyt
hoset
hat
ar
emeanttosuppor
tsmal
ldat
abases—donotsuppor
text
ernalv
iews.

Eveninsuchsy stems,however
,acertai
namountofmappi ngi
snecessar
yto
tr
ansf
ormrequestsbetweent
heconcept
ualandi
nter
nall
evel
s

Q1.
c)Explai
ndiff
erentt
ypesofuserf
ri
endl
yint
erf
acesandt
ypesofuserwho
t
ypi
callyuseeach?

Typesofuserf
ri
endl
yint
erf
aceswhousest
hem:

Int
erfaces f
ort he DBA:I
nterf
aces forDBA incl
ude commands forcreati
ng
accounts,
set
ting syst
em parameter
s,gr
ant
ing account aut
hor
izat
ion,
changi
ng
schema,
andr
eor
gani
zingt
hest
oragest
ruct
ureofdat
abase.

Int
erfacesforpar
ametri
cUsers:
Int
erfacesforparamet
ricusersusual
lyhav
esmall
seofoper at
ionst
hattheymustperformrepeatedly
.Example:someofparamet
ri
c
usersarebanktel
ler
s.

DBMSI
nter
faces:

Menu-BasedI nt
erf
acesforWebCli
entsorBr
owsi
ng.Thesei
nter
facespresentthe
userwit
hlist
sofoptions(
cal
ledmenus)t
hatl
eadt
heuserthr
ought hef
ormul at
ion
ofarequest.

Forms-BasedI
nterf
acesdispl
aysaformtoeachuser
.Userscanfil
loutalloft
he
for
m ent
ri
estoinsertnew dat
a,ort
heycanfi
lloutonlycer
tai
nentries,i
nwhich
caset
heDBMSwi l
lretr
iev
ematchingdat
afort
heremaini
ngentr
ies.

GraphicalUserI
nter
facesdi
spl
aysaschemat otheuserindi
agrammati
cform.The
userthencanspecif
yaquerybymani
pulatingt
hediagram.GUIsuti
li
zebot
hmenus
andforms.MostGUI suseapoi
nti
ngdevice.

Nat ur
alLanguageI nterf
acesacceptsrequest
swritt
eni nEngl
ishorsomeot her
l
anguageandat t
emptt ounder
standthem.SpeechInputandOutputuseofspeech
asani nputqueryandspeechasananswert oaquesti
onorresul
t.Thespeechi
nput
i
sdet ectedusingal i
brar
yofpredefi
nedwordsandusedt osetupt heparamet
ers
thataresuppli
edtothequeri
es.

Int
erfacesf
orParamet
ri
cUserssuchasbankt
ell
ers,of
tenhav
easmal
lsetof
operati
onst
hatt
heymustper
for
mrepeat
edl
y.

Int
erfacesfort
heDBA.DBAusepr i
vil
egedcommands.Theseincludecommands
forcreati
ngaccount
s,sett
ingsyst
em parameter
s,granti
ngaccountauthor
izat
ion,
changingaschema,andreorgani
zingthest
oragest
ructur
esofadatabase

Q2.
a)Def
inet
hef
oll
owi
ngwi
thanexampl
e:

(
i)Weakent
it
ytype(
ii
)par
ti
cipat
ionconst
rai
nt

(
ii
i)
car
dinal
it
yrat
io(
iv)r
ecur
siv
erel
ati
onshi
p(v)speci
ali
zat
ion

Weakent
it
yty
pe:

 Entit
yty
pest
hatdonothav
ekeyat
tri
but
esoft
hei
rownar
ecal
ledweakent
it
y
types

 I
ncont
rast
,regul
arent
it
yty
pest
hatdo hav
eakeyat
tri
but
e—ar
ecal
led
st
rongent
it
ytypes

 Enti
ti
esbelongi
ngtoaweakent i
tyty
pearei
dent
ifi
edbybeingr
elat
edt o
speci
fi
centit
iesfr
om anot
herent
it
ytypei
ncombinati
onwi
thoneoft hei
r
att
ri
butev
alues.

 Wecallthisotherent
it
ytypet heident
if
yingorownerent
it
yty
pe,andwecall
t
herelati
onshiptypet
hatr
elatesaweakent i
tyty
petoit
sownerthei
dent
if
ying
r
elat
ionshipoftheweakentit
yt y
pe

 Aweakent it
ytypealwayshasat ot
alpar
ti
cipat
ionconst
rai
ntwithrespectto
i
tsident
if
yingrelat
ionshi
pbecauseaweakentit
ycannotbeident
if
iedwithout
anownerentity

 Aweakent i
tyty
penor
mallyhasapart
ialkey
,whi
chist
heatt
ri
butethatcan
uni
quel
yident
if
yweakent
it
iest
hatar
erelat
edtot
hesameownerent
it
y

 Assumet hatnotwodependent
soft
hesameemployeeev erhav
ethesame
fi
rstname,theat
tri
but
eNameofDEPENDENTi
sthepart
ialkey

Par
ti
cipat
ionconst
rai
nt:

 Thepar t
ici
pati
on const
raintspeci
fi
eswhethertheexi
stenceofan ent
it
y
dependsonitsbei
ngr el
atedtoanot
herent
it
yviat
herel
ati
onshi
pty
pe

 Thisconstrai
ntspeci
fi
est hemi
nimum numberofrel
ati
onshi
pinst
ancesthat
each entit
y can parti
cipat
ein and is somet
imes cal
led t
he mini
mum
cardi
nal
ityconstr
aint

 Ther
ear
etwot
ypesofpar
ti
cipat
ionconst
rai
nts—t
otalandpar
ti
al

 Ifacompanypoli
cystatesthateveryemployeemustwor kf
oradepart
ment,
t
hen an empl
oyeeent i
tycan existonlyifi tpar
ticipat
esin atl
eastone
WORKS_FORrelati
onshi
pi nst
ance.Thus,t hepart
icipati
onofEMPLOYEEin
WORKS_FORiscal l
edt ot
alparti
cipat
ion,meaningt hateveryent
it
yinthe
t
otalsetofemployeeent i
ti
esmustber elatedtoadepar tmententi
tyvi
a
WORKS_FOR.Totalpar
ti
cipati
onisalsocall
edexist
encedependency.

 we do notex pectevery employee t


o manage a depar t
ment,so t he
part
ici
pat
ion ofEMPLOYEE i nthe MANAGES r elat
ionshi
ptype i
spar t
ial
,
meaningthatsomeorpartofthesetofemployeeenti
tiesar
erel
atedtosome
depart
mententit
yviaMANAGES, butnotnecessar
il
yall

 I
nERdiagr
ams,t
otalpar
ti
cipat
ion(
orexist
encedependency
)isdispl
ayedas
adoubl
eli
neconnecti
ngthepar t
ici
pat
ingenti
tytypet otherelat
ionshi
p,
wher
easpar
ti
alpar
ti
cipat
ioni
srepr
esent
edbyasi
ngl
eli
ne.

Car
dinal
it
yrat
io

Car
dinal
it
yRat
iosf
orBi
nar
yRel
ati
onshi
ps:

Thecardi
nali
tyrat
ioforabinar
yrel
ati
onshi
pspeci
fi
esthemaxi
mum number
ofr
elat
ionshi
pinstancest
hatanent
it
ycanpart
ici
pat
ein.

For exampl
e, i n t
he WORKS_FOR bi
nary rel
ationship
type,
DEPARTMENT: EMPLOYEEisofcardinal
ityrat
io1:N,meani
ngt hateach
depart
mentcanber el
atedt
o(thatis,employs)anynumberofempl oyees
(N),
9butanemploy
eecanber el
atedt
o( workfor)atmostonedepart
ment( 1)

Thepossi
blecar
dinal
it
yrat
iosf
orbi
nar
yrel
ati
onshi
pty
pesar
e1:
1,1:
N,N:
1,

andM:
N

In1:
1anemployeecanmanageatmostonedepar
tmentandadepar
tment
canhav
eatmostonemanager

InM:N anemployeecanwor
konsev
eralpr
oject
sandapr
ojectcanhav
e
sev
eralempl
oyees

Car
dinali
tyr
ati
osforbi
nar
yrel
ati
onshi
psar
erepr
esent
edonERdi
agr
amsby
di
spl
ay i
ng1,M,andNonthedi
amonds
Recur
siv
eRel
ati
onshi
ps

 Eachenti
tyty
pethatpar
ti
cipat
esi
nar
elat
ionshi
pty
pepl
aysapar
ti
cul
ar
rol
eintherel
ati
onshi
p

therol
enamesignifi
estherolet hataparti
cipati
ngenti
tyfr
om t
heent it
y
type play
s in each r el
ationshipinst
ance For exampl e, i
n t he
WORKS_FORr elat
ionshi
pt ype,EMPLOYEEpl ayst
her ol
eofempl oyee
orworkerandDEPARTMENTpl aystheroleofdepart
mentoremploy er

 sameenti
tytypepar
ti
cipatesmor
ethanonceinar elati
onshiptypei
n
dif
fer
entr
oles,
suchr
elati
onshi
pty
pesar
ecall
edrecursiv
er el
ati
onshi
ps.

SPECI
ALI
ZATI
ON

 Speci
ali
zat
ionisthepr
ocessofdef
ini
ngasetofsubcl
assesofanent
it
y
ty
pe;thi
senti
tyty
peiscall
edt
hesupercl
assoft
hespecial
izat
ion.

 Thesetofsubcl
assesthatfor
msaspeci al
izat
ioni
sdef i
nedonthe
basi
s ofsome disti
ngui
shi
ngchar
act
eri
sti
c oft he ent
it
ies i
nthe
super
class.

 For ex ampl e, t
he set of subcl asses { SECRETARY, ENGI
NEER,
TECHNI CIAN}i s a speci
ali
zati
on oft hesupercl
ass EMPLOYEE t
hat
dist
inguishes among empl oyee ent
iti
es based on the j
ob ty
pe of
eachempl oyee.

Q2.
b)DevelopanERdiagram f
orkeepingtrackofi
nfor
mat
ionaboutacompany
dat
abasetaki
ngi
ntoaccountat
leastf
iveent
iti
es.
Q3.
a)Def
inet
hef
oll
owi
ngt
erms

i
)Key i
i)Superkey i
ii
)Candi
dat
ekey

i
v)Pr
imar
ykey v)For
eignkey

Key:

KEYSi nDBMSi sanat t


ributeorsetofat tr
ibut
eswhi chhel
psy outoi dent
if
ya
row(t
uple)i
nar elat
ion(table).Theyall
ow yout of i
ndtherel
ati
onbet weent wo
tabl
es.Keyshel
py ouuniquel yident
if
yar owinat abl
ebyacombi nat
ionofoneor
morecolumnsinthattabl
e.

Superkey :

Asuperkeyisagr
oupofsingleormul
tipl
ekeyswhichi
denti
fi
esrowsinatabl
e.A
Superkey may have addit
ionalat
tri
butes t
hatare notneeded foruni
que
i
denti
fi
cati
on

Exampl
e suppose we have EmpSSN,
EmpNum,
Empname means EmpSSN and
EmpNum namearesuperkeys.

Pr
imar
ykey:

ThePRIMARY KEY cl
ausespeci
fi
esoneormoreatt
ribut
esthatmakeup the
pri
mar
ykeyofar el
ati
on.Ifapri
marykeyhasasi
ngl
eat t
ri
but
e,thecl
ausecan
f
oll
owt
heat
tri
but
edi
rect
ly.

Ex:thepr
imar
ykeyofDEPARTMENTcanbespeci
fi
edasf
oll
ows:
DnumberI
NT
PRIMARYKEY,

Candi
dat
ekey

TheUNI
QUEcl
ausespeci
fi
esal
ter
nat
e(uni
que)key
s,al
soknownascandi
dat
e
key
s

TheUNI QUEcl
ausecanalsobespecif
ieddi
rect
lyf
orauniquekeyi
fiti
sasi
ngl
e
at
tri
but
e,asint
hefol
lowi
ngexample:DnameVARCHAR(15)UNIQUE

FOREI
GNKEY

Referenti
alintegri
tyisspeci
fi
edv i
atheFOREI GN KEYclause, constr
aintcanbe
vi
olatedwhent uplesarei
nser
tedordeleted,orwhenaforeignkeyorpr imarykey
at
tributev al
uei supdated.The def
aultaction t
hatSQL takesf oran int
egri
ty
vi
olationistorejectt
heupdat
eoperati
ont hat
wil
lcauseaviolat
ion,whichisknown
astheRESTRI CTopt i
on.

Theschemadesignercanspecifyanalt
ernat
iveact
iont
obet akenbyat
tachi
nga
ref
erent
ialt
ri
ggered act
ion cl
ause t
o anyforei
gn keyconstr
aint
.The opt
ions
i
ncludeSETNULL,CASCADE, andSETDEFAULT.Anopt i
onmustbequalifi
edwith
eit
herONDELETEorONUPDATE.

Ingeneral
,theact i
ont akenbyt heDBMSf orSETNULLorSETDEFAULTi sthe
samef orbothONDELETEandONUPDATE:Thev al
ueoft heaff
ectedr eferencing
att
ri
butesischangedt oNULLf orSETNULLandt othespecifi
eddef aultv alueof
theKeyandr eferent
ialintegr
it
yconstr
aint
swer enotincl
udedinear l
yv er si
onsof
SQL.Theact ionf orCASCADEON DELETEi stodeletealltherefer
enci ngt uples,
whereas the act i
on forCASCADE ON UPDATE i sto change the value oft he
ref
erenci
ngf oreignkeyat tri
bute(
s)totheupdat
ed( new)pr i
marykeyv al uef orall
theref
erenci
ngt upl
es.

Q3.
b)Consi
dert
hef
oll
owi
ngCOMPANYdat
abase

EMP(
Name,
SSN,
Sal
ary,
Super
SSN,
Gender
,Dno)

DEPT(
DNum,
Dname,
Mgr
SSN,
Dno)

DEPT_LOC(
Dnum,
Dlocat
ion)

DEPENDENT(
ESSN,
Dep_name,
Sex)
WORKS_ON(
ESSN,
Pno,
Hour
s)

PROJECT(
Pname,
Pnumber
,Pl
ocat
ion,
Dnum)

Wr
it
ether
elat
ionalal
gebr
aquer
iesf
ort
hef
oll
owi
ng

(i
)Ret
ri
evethename,addr
ess,sal
aryofempl
oyeeswhowor
kfort
heResear
ch
depar
tment
.

(i
i)fi
nd the names ofempl
oyees who wor
k on al
lpr
oject
s cont
rol
led by
depar
tmentnumber4.

ii
i)Retr
iev
etheSSNofallempl
oyeeswhoeitheri
ndepart
mentno:
4ordi
rect
ly
supervi
seanempl
oyeewhoworkindepar
tmentnumber:
4

(
iv)Ret
ri
evet
henamesofempl
oyeeswhohavenodependent
s

(v) Retr
ieve each depar
tment number
,the number of empl
oyees i
nthe
departmentand

t
hei
raver
agesal
ary.

ANSWERS:

i
)Retr
ievet
hename,addr
ess,sal
aryofempl
oyeeswhowor
kfort
heResear
ch
depar
tment

SELECTFNAME,
LNAME,
ADDRESS

FROM EMPLOYEE,
DEPARTMENT

WHEREDNAME=‘
Resear
ch’
ANDDNUMBER=DNO;

(i
i)fi
nd the names ofempl
oyees who wor
k on al
lpr
oject
s cont
rol
led by
depar
tmentnumber4.

SELECTFNAME,
LNAME

FROM EMPLOYEE

WHERE((
SELECTPNO FROM WORKS_ONWHERESSN=ESSN)
CONTAI
NS

(
SELECTPNUMBERFROM PROJECT

WHEREDNUM =4))
;

ii
i)
Ret
rievetheSSN ofal
lempl
oyeeswhoeit
herindepart
mentno:
4ordi
rect
ly
super
viseanemploy
eewhoworkindepar
tmentnumber:
4

(
SELECTSSNFROM PROJECT,
DEPARTMENT,
EMPLOYEE

WHEREDNUM =DNUMBERANDMGRSSN=SSNANDDNO=4)

UNI
ON

(
SELECTSSNFROM PROJECT,
WORKS_ON,
EMPLOYEE

WHEREPNUMBER=PNOANDESSN=SSNANDDNO=4)
;

ESSN)<---- πSSN(σDN0=4(EMPLOYEE))
DEPT4(

WORKER_DEPTS<-----πS(
WORKS-ON*
DEPT4)

MGRS<--------- πDNUMBER(
EMPLOYEE⋈θSSN=MGRSSNDEPARTMENT)

MANAGED_DEPTS( <------πDNUMBER(
DNUM) (σDN0=4(
MGRS)
)

RESULT<-(
WORKERDEPTUMANAGEDDEPS)

(
iv)Ret
ri
evet
henamesofempl
oyeeswhohavenodependent
s

SELECTFNAME, LNAME
FROM EMPLOYEE
WHERENOTEXI STS(SELECT*FROM DEPENDENTWHERESSN=ESSN)
;
V)Ret
rieveeachdepar
tmentnumber
,thenumberofempl
oyeesi
nthedepar
tment
andtheiraver
agesal
ary.

SELECTDNO,COUNT(
*),
AVG(
SALARY)
FROM EMPLOYEE
GROUPBYDNO;

RESULT->Dno(
COUNT(
AVERAGE(SALARY)
))(
EMPLOYEE)

Q3 C)Summari
ze the steps invol
ved i
n conver
ti
ng t
he ER const
ruct
sto
cor
respondi
ngr
elat
ionalt
ables

St
epsi
nERt
oRel
ati
onalmappi
ng

St
ep1:Mappi
ngofRegul
arEnt
it
ytypes

 Foreachregularent
it
ytypeEint
heERschema,cr
eat
ear
elat
ionRt
hat
i
ncludesal
lthesi
mpleatt
ri
but
es

I
ncl
udeonl
ythesi
mpl
ecomponent
sat
tri
but
esofcomposi
teat
tri
but
es

 Chooseoneoft
hekeyat
tri
but
esofEaspr
imar
ykeyf
orR

 Ift
hechosenkeyofEisacomposit
e,thent
hesetofsi
mpl
eat
tri
but
est
hat
f
ormitwi
llt
oget
herf
ormthepr
imarykeyofR

Eg:EMPLOYEE,PROJECT,DEPARTMENT ar er
egul
arent
it
iesandt
hei
rpr
imar
y
keysar
eSsn,Pnumber
,Dnumberr
espect
ivel
y

St
ep2:Mappi
ngofWeakEnt
it
ytypes

 Foreachweakent
it
ytypeW intheERschemawithownerent
it
yt y
peE,
cr
eat
earel
ati
onRthati
ncl
udesal
lthesi
mpl
eatt
ri
but
esofW asat
tri
butesof
R

Incl
ude as f
orei
gn keyatt
ribut
es ofR,t
he pr
imar
ykeyofr
elat
ion t
hat
corr
espondtoownerenti
tyt
ype

 Thepri
marykeyofRi
sthecombi
nat
ionofpr
imar
ykeyoft
heownerandt
he
par
ti
alkeyoft
heweak

ent
it
yty
pe
Eg:DEPENDENTi
sweakent
it
yty
pe.

Incl
udethepri
marykeySsnofEMPLOYEEwhi
chcor
respondst
oownerent
it
yty
pe
asforei
gnkeyatt
ri
but
eofDEPENDENT

St
ep3:Mappi
ngofBi
nar
y1:
1Rel
ati
onshi
ptypes

I
denti
fyt he r
elat
ions S and T t
hatcor
responds t
othe ent
it
yty
pes
par
ti
cipat
ingi
nR

Thr
eepossi
bleappr
oaches:

1)For
eignkeyappr
oach:

Chooseoneoft
her
elat
ionSandi
ncl
udeasf
orei
gnkeyi
nSt
hepr
imar
ykeyofT

Eg:MANAGESi
s1:
1rel
ati
onshi
pbet
weenDEPARTMENTandEMPLOYEE

I
ncl
udet
hepri
mar ykeyofEMPLOYEEasf
orei
gnkeyi
ntheDEPARTMENTand
r
enamei
tasMgr_ssn

2)Mer
gedr
elat
ionshi
pappr
oach:

Mer
gingt
woent
it
yty
pesandr
elat
ionshi
pint
oasi
ngl
erel
ati
on

3)Cr
ossr
efer
enceappr
oach:

Setupthi
rdrel
ati
onRf
ort
hepur
poseofcr
ossr
efer
enci
ngt
hepr
imar
ykey
soft
wo
rel
ati
onsSandT

St
ep4:Mappi
ngofBi
nar
y1:
NRel
ati
onshi
ptypes

Ident
if
yt herelati
onSt hatr
epr
esent
sthepar
ti
cipat
ingent
it
yty
peatt
heN-
sideoftherelati
onshi
pt y
pe

I
ncl
udeasf or
eignkeyi
nSt
hepr
imar
ykeyoft
her
elat
ionTt
hatot
herent
it
y
t
ypepar
ti
cipati
nginR

Eg:WORKS_FOR,CONTROLS,SUPERVISI
ON i
s1:Nrel
ati
onshi
pty
pef or
WORKS_FORi
ncl
udeprimar
ykeyDnumberoft
heDEPARTMENTrel
ati
onasfor
eign
keyi
nEMPLOYEErel
ati
onandcal
litDno

St
ep5:Mappi
ngofBi
nar
yM:
NRel
ati
onshi
ptypes

 Cr
eat
eanewr
elat
ionSt
orepr
esentR

 Incl
udeaf or
eignkeyat tr
ibut
esinSt hepri
marykey
soft herelat
ionthat
repr
esentthe par
ti
cipat
ing enti
tyt
ypes,thei
rcombinat
ion wi
llform the
pri
marykeyofS
Eg:WORKS_ONisM:Nrelat
ionshi
pty
pe,i
ncl
udethepr
imar
ykey
sofEMPLOYEE
andPROJECTasfor
eignkeyforWORKS_ONrel
ati
on

St
ep6:Mappi
ngofMul
ti
val
uedat
tri
but
es

 Foreachmul
ti
val
uedat
tri
but
eA,
creat
eanewr
elat
ionR

 Therelat
ionRwil
lincl
udeat
tri
but
est
oAandt
hepr
imar
ykeyat
tri
but
eKasa
for
eignkeyinR

 DEPT_LOCATI
ON i s new rel
ati
on, t
he at
tri
but
e Dl
ocat
ion r
epr
esent
s
mult
ival
uedat
tri
but
eLOCATIONSofDEPARTMENT

St
ep7:Mappi
ngofBi
nar
yN-ar
yRel
ati
onshi
ptypes

 Foreachn-ar
yrel
ati
onshi
pty
peR,wher
en>2cr
eat
eanew r
elat
ionSt
o
repr
esentR

Incl
udef or
eign keyattri
but
esinS theprimarykeyoft
herel
ati
onsthat
repr
esentthepar t
ici
pat
ingenti
tyt
ypes Al
soincl
udesi
mpl
eat
tri
but
esofn-
aryrel
ati
onshipt
y pes

Q4 a)
Expl
ain wit
h example basi
c const
rai
ntst
hatcan be speci
fi
ed when a
dat
abaset
ableiscreat
ed.
Const
rai
ntscanbedi
videdi
ntot
hef
oll
owi
ngt
wot
ypes,Col
umnl
evelconst
rai
nts:Li
mit
sonl
y
col
umndata.

Tabl
elevelconstr
aint
s:Li
mitswhol
etabl
edat a.Constr
aint
sareusedt omakesurethatthe
i
ntegr
it
yofdataismaintai
nedint
hedat
abase.Foll
owingarethemostusedconst
rai
ntst
hatcan
beappli
edtoatabl
e.

 NOTNULL

 UNI
QUE

 PRI
MARYKEY

 FOREI
GNKEY

 CHECK

 DEFAULT

NOTNULLconst rai
ntrest
ri
ctsacolumnf
rom havi
ngaNULLv al
ue.OnceNOTNULLconst
rainti
s
appl
iedtoacolumn,youcannotpassanul
lvaluet
othatcol
umn.Itenf
orcesacol
umntocontai
n
aproperval
ue.

CREATETABLESt
udent
(s_i
dintNOTNULL,
Namev
archar
(60)
,Agei
nt)
;
UNIQUEconst rai
ntensuresthatafiel
dorcol
umnwillonl
yhav euni
quevalues.AUNIQUE
constr
aintfi
eldwillnothav
eduplicatedat
a.Thi
sconstr
aintcanbeappl
iedatcolumnlev
elor
tabl
elevel.

CREATETABLESt
udent
(s_i
dintNOTNULLUNI
QUE,
Namev
archar
(60)
,Agei
nt)
;

Pri
marykeyconst
rai
ntuni
quelyi
denti
fi
eseachr ecor
dinadat
abase.APr
imar
yKeymustcont
ain
uni
queval
ueanditmustnotcont
ainnullv
alue.

CREATEt
abl
eSt
udent(
s_i
dintPRI
MARYKEY,
Namev
archar
(60)NOTNULL,
Agei
nt)
;

For
eignKeyConst
rai
nt

FOREIGN KEYi
susedt or
elatetwotabl
es.FOREI
GN KEYconst
rai
nti
sal
sousedt
orest
ri
ct
act
ionst
hatwoul
ddest
royl
inksbet
weentabl
es.

CREATEt
abl
eOr
der
_Det
ail
(

or
der
_idi
ntPRI
MARYKEY,

or
der
_namev
archar
(60)NOTNULL,

c_i
dintFOREI
GNKEYREFERENCESCust
omer
_Det
ail
(c_i
d)

)
;

CHECKconstr
ainti
susedtor
estr
icttheval
ueofacol
umnbet
weenar
ange.I
tper
for
mscheckon
thev
alues,
bef
orestor
ingt
hem i
ntothedatabase

CREATEt
abl
eSt
udent
(s_i
dintNOTNULLCHECK(
s_i
d>0)
,Namev
archar
(60)NOTNULL,
Age
i
nt)
;

DEFAULT-Set
sadef
aul
tval
uef
oracol
umnwhennov
aluei
sspeci
fi
ed

Q4b)
Wri
teSQLsynt
axf
ort
hef
oll
owi
ngwi
thexampl
e:

(i
)SELECT
(i
i)ALTER
(i
ii
)UPDATE
I
)SQLSELECTSt atementSy
ntax
SELECTcolumn1,col
umn2..
..
col
umnN

FROM t
abl
e_nameWHERECONDI
TION;

Exampl
e:SELECT*FROM Cust
omer
s

WHERECount
ry='
Mex
ico'
;

UPDATESy
ntax:

UPDATEt
abl
e_name
SETcol
umn1=v
alue1,
col
umn2=v
alue2.
..
.,col
umnN=v
alueN

WHERE[
condi
ti
on]
;

Exampl
e:UPDATECUSTOMERSSETADDRESS='
Pune'

WHEREI
D=6;

ALTERTABLESYNTAX:

ALTERTABLEt
abl
e_name

ADDcol
umn_namedat
aty
pe;

Exampl
e:ALTERTABLECust
omer
s

ADDEmai
lvar
char
(255)
;

Q4c)
Consi
dert
hef
oll
owi
ngr
elat
ionschema

Wor
ks(
Pname,
Cname,
sal
ary)

Li
ves(
Pname,
Str
eet
,Ci
ty)

Locat
ed_i
n(Cname,
cit
y)

Manager
(Pname,
Mgr
name)

Wr
it
etheSQLquer
iesf
ort
hef
oll
owi
ng

i
)Fi
ndt
henamesofal
lper
sonswhol
ivei
ntheci
tyBangal
ore.

i
i)Retr
ievet
henamesofal
lper
sonof"
Inf
osys"whosesal
aryi
sbet
weenRs
.50000

i
ii
)Fi
ndt
henamesofal
lper
sonswhol
ivesandwor
kint
hesameci
ty

i
v)Li
stt
henamesoft
hepeopl
ewhowor
kfor“
TechM”al
ongwi
tht
heci
ti
est
hey
l
ivei
n.

v)
Findt
heaver
agesal
aryof“
Inf
osys”per
sons

i
)SELECT L.Pname,
Cit
yfr
om Wor
ksW andLi
vesL

wher
eW.
pname=L.
pnameandL.
cit
y=Bangl
ore;

i
i)
SELECT L.
Pname f
rom Wor
ks W,
Liv
es L wher
e W.
cnmae=’
i
nfosy
s’AND
W.
Pname=L.
Cnameandsal
ary<50,
000

i
ii
)SELECT W. Pname FROM Wor
ks W,
Liv
esL,
Locat
edI
N I FROM Wor
ks
w,
LivesL,
Locat
edinIWHERE

W.
Cname=I
.CnameANDW.
Pname=L.
PnameANDI
.Ci
ty=L.
Cit
y;

i
v)SELECT L.PNAME,
Cit
y FROM Wor
ks W,
Liv
esLwher
eCname=”
Tech M”AND
W.Pname=L.
Pname;

V)
SELECT AVG(Sal
ary)Fr
om Wor
ksW L.
Locat
ed_i
n Wher
eW.
Cname=I
.Cname
ANDL.Cname=I
nfosys;

Q5a.
Expl
aint
hef
oll
owi
ngconst
ruct
susedi
nSQLwi
thexampl
e:

(
i)
Nest
edquer
ies

i
i)
Aggr
egat
efunct
ions

i
ii
)Tr
igger
s

i
v)Vi
ewsandt
hei
rupdabi
li
ty

v)schemachangest
atement
s

TRI
GGERS:

 Tr
iggerhast
hreecomponent
s:ev
ent
,condi
ti
on,
act
ion

Ev
ent
:Whenev
enthappens,
tri
ggeri
sact
ivat
ed

Condi
ti
on:I
fcondi
ti
oni
str
ue,
tri
ggerexecut
esorski
ps

Act
ion:Act
ionsar
eper
for
medbyt
ri
gger
s

 Whenev
entoccur
sandcondi
ti
oni
str
ue,
execut
etheact
ion

Synt
ax:

CREATETRI
GGER<t
ri
gger
_name>

[
Bef
ore|
Aft
er|
Inst
eadof
]

[
Inser
t|
Updat
e|Del
ete]on<t
abl
e_name>

[
foreachr
ow]

[
when<condi
ti
ont
oexecut
e>]
Tr
iggerbody
;

 Rat
herthanofferi
ngusersonlytheopt
ionofabor
ti
nganoper
ati
on(usi
ng
ASSERTI
ON)t hatcausesaviol
ati
on,t
heDBMSshoul dmaket
hefol
lowi
ng
opt
ionavai
labl
e.

I
tmaybeusefult
ospecifyacondi
ti
ont
hat
,ifv
iol
ated,causessomeusert
o
bei
nfor
medofthevi
olat
ion.

eg:Amanagermaywantt obei nf
ormedifanemploy
ee'strav
elex penses
exceedacert
ainli
mitbyreceivi
ngamessagewhenev erthisoccurs.The
acti
on t
hatt
heDBMS mustt akei nthiscaseist
o send an appropri
ate
messagetot
hatuser
.Thecondit
ionisthususedt
omonit
orthedatabase.

Vi
ew

Av
iewi
sasi
ngl
etabl
ethati
sder
ivedf
rom ot
hert
abl
es.

 Theseot
hert
abl
escoul
dbebaset
abl
esorpr
evi
ousl
ydef
inedv
iews.

 Av iewdoesnotnecessar
il
yexisti
nphysi
calf
orm;i
tisconsider
edavirt
ual
tabl
e,incontr
asttobaset abl
es,whosetupl
esareactual
lystoredi
nt he
database.

Synt
ax:

CREATEVI
EW <v
iewname>[
(<col
umnname>{
,<col
umn>}
)]

AS<sel
ectst
atement
>

 Forexample,wemayfr
equentl
yissuethefol
lowi
ngquer
ythatret
ri
evet
he
employeenameandt
heproj
ectnamesthatt
heemploy
eeworkson.

SELECTFNAME,
PNAME

FROM EMPLOYEE,PROJECT,WORKS_ON WHERE (


SSN = ESSN AND PNO =
PNUMBER)
;

 Ratherthanhavi
ngt ospeci
fythejoi
noft heEMPLOYEE,WORKS_ON,and
PROJECTt abl
esev er
yti
meweissuethatquer
y,wecandef
ineav
iewthati
sa
resul
tofthesej
oins.

 Wecant henissuequeri
esont hevi
ew,whichar
especi
fi
edassi
ngl
e-t
abl
e
r
etr
iev
alsratherthanasr
etr
iev
alsi
nvol
vingt
wojoi
nsonthr
eet
abl
es.

 Wecal
lt
heEMPLOYEE,WORKS_ON,andPROJECTt
abl
est
hedef
ini
ngt
abl
es
oft
hevi
ew.
 Wedi dnotspeci
fyanynewattri
but
enamesforthev
iewWORKS_ON1;inthi
s
case,WORKS_ON1i nher
it
sthenamesofthevi
ewatt
ribut
esf
rom t
hedef
ini
ng
tabl
esEMPLOYEE, PROJECT,andWORKS_ON.

 Weex pl
icit
lyspeci
fi
esnewat t
ri
butenamesf
ort hevi
ewDEPT_I NFO,usi
nga
one-to-onecor r
espondencebetweent
heat
tri
butesspeci
fi
edintheCREATE
VIEW clauseand thosespecifi
ed int
heSELECT clauseoft
hequer ythat
defi
nesthev i
ew.

 Wecannow specifySQLqueriesonv i
ewsV1andV2i
nthesamewaywe
speci
fyquer
iesi
nvol
vingbaset
ables.

Aggr
egat
eFunct
ions
SQLaggr
egati
onfunct
ionisusedtoperformthecal
cul
ati
onsonmul
ti
pler
owsofasi
ngl
e
col
umnofatabl
e.I
tret
urnsasi
nglev
alue.Iti
sal
sousedt
osummari
zet
hedata.

COUNTfunct
ionisusedtoCountthenumberofr
owsi
nadat
abaset
abl
e.I
tcanwor
konbot
h
numer
icandnon-numeri
cdataty
pes.

SELECTCOUNT(
col
umn_name)FROM t
abl
e_nameWHEREcondi
ti
on;
SELECTCOUNT(
Product
ID)FROM Pr
oduct
s;

TheAVG(
)funct
ionr
etur
nst
heav
eragev
alueofanumer
iccol
umn.

SELECTAVG(
Pri
ce)FROM Pr
oduct
s;

r
etur
nst
het
otalsum ofanumer
iccol
umn.

TheSUM(
)funct
ion

SELECTsum(
Pri
ce)FROM Pr
oduct
s;

t
heSQLMI
N()andMAX(
)Funct
ions

TheMI
N()f
unct
ionr
etur
nst
hesmal
lestv
alueoft
hesel
ect
edcol
umn.

TheMAX(
)funct
ionr
etur
nst
hel
argestv
alueoft
hesel
ect
edcol
umn.

SELECTMI
N(Pr
ice)ASSmal
lest
Pri
ceFROM Pr
oduct
s;

Schemachangest
atement
s

TheDROPCommand:

 TheDROP commandcanbeusedt
odr
opnamedschemael
ement
s,suchas
tabl
es,
domai
ns,
orconst
rai
nts.

 Ther
ear
etwodr
opbehav
ioropt
ions:CASCADEandRESTRI
CT.

 Forex
ampl e,
toremovet
heCOMPANYdatabaseschemaandalli
tst
abl
es,
domains,
andotherel
ement
s,t
heCASCADEopti
onisusedasf
oll
ows:

DROPSCHEMACOMPANYCASCADE;

IftheRESTRICTopt
ioni
schoseninplaceofCASCADE,theschemai
s
droppedonl
yifi
thasnoel
ement
sini
t;other
wise,t
heDROPcommandwi
ll
notbeexecut
ed

I
fabaset abl
ewi
thi
naschemaisnotneededanyl
onger
,ther
elat
ionand
i
tsdef
ini
ti
oncanbedel
etedby
usi
ngtheDROPTABLEcommand.

 Forexampl
e,ifwe no l
ongerwish t
o keep t
rack ofdependent
s of
empl
oyeesi
ntheCOMPANYdatabase:

DROPTABLEDEPENDENTCASCADE;

Ift
heRESTRICTopt i
onischoseninsteadofCASCADE,atabl
eisdropped
onl
yifiti
snotr efer
encedinanyconstrai
nts(
forexampl
e,byfor
eignkey
def
ini
ti
onsinanotherrel
ati
on)orv
iews.

 Wi
tht
heCASCADEopt
ion,
allsuchconst
rai
ntsandv
iewst
hatr
efer
encet
he
t
abl
ear
edr
oppedaut
omat
ical
lyf
rom t
heschema,
alongwi
tht
het
abl
eit
sel
f.

TheALTERCommand:

 Thedefini
ti
onofabasetabl
eorofothernamedschemael
ement
scanbe
changedbyusi
ngtheALTERcommand.

 Forbaset
abl
es,
thepossi
bleal
tert
abl
eact
ionsi
ncl
ude:

Addi
ngordr
oppi
ngacol
umn(
att
ri
but
e)

Changi
ngacol
umndef
ini
ti
on

Addi
ngordr
oppi
ngt
abl
econst
rai
nts.

 Forex
ample,t
oaddanatt
ribut
eforkeepi
ngt
rackofj
obsofempl
oyeesto
theEMPLOYEEbaser
elat
ionsi
ntheCOMPANYschema,wecanuset he
command:

ALTERTABLECOMPANY.
EMPLOYEEADDCOLUMNj
obVARCHAR(
12)
;

 Todropacol
umn,wemustchooseei
therCASCADEorRESTRI
CTf
ordr
op
behav
ior
.

IfCASCADEi schosen,al
lconst
rai
ntsandv
iewsthatrefer
encethecol
umn
aredroppedautomati
call
yfr
omtheschema,al
ongwiththecolumn.

IfRESTRICTischosen,t
hecommandi ssuccessf
ulonl
yifnov
iewsor
const
rai
nts(orot
herel
ement
s)r
efer
encet
hecolumn.

 The f
oll
owi
ng command r
emov
es t
he at
tri
but
e ADDRESS f
rom t
he
EMPLOYEEbaset
abl
e:

ALTERTABLECOMPANY.
EMPLOYEEDROPCOLUMNAddr
essCASCADE;

Nest
edquer
ies
In nested queri
es,queryex ecuti
on start
sf r
om i nner
mostquer yt o out
ermost
queries.Theexecuti
onofinnerquer yisindependentofout erquer
y,buttheresult
ofinnerqueryisusedinexecutionofouterquery.Vari
ousoper at
orsli
keIN,NOTI N,
ANY, ALLetcareusedinwr i
ti
ngi ndependentnestedqueries.

Exampl
e:SELECT* FROM CUSTOMERSWHEREI
DIN(
SELECTI
D

FROM CUSTOMERSWHERESALARY>4500);

Exampl
e:I
NSERTI
NTOCUSTOMERS(
SELECT*FROM CUSTOMERS
WHEREI
DIN(
SELECTI
D FROM CUSTOMERS);

Exampl
e:DELETEFROM CUSTOMERS

WHEREAGEI
N(SELECTAGEFROM CUSTOMERS WHEREAGE>=27)
;

Q5b)
Whati
sDynami
cSQL?Howi
tisdi
ff
erentf
rom EmbeddedSQL

Dy
nami
cSQL

 Programmi
ngt
echni
quet
hatenabl
est
obui
ldSQLst
atement
sdy
nami
cal
lyat
runt
ime

 Thiscanbehel pfulwheni ti
snecessarytowri
tecodethatcanadj
ustt
o
varyi
ngdat abases,condi
ti
ons,or
serv
ers.I
talsomakesi
teasi
ertoaut
omat
e
tasksthatarerepeatedmanytimes

 Twomai
ncommands:PREPAREandEXECUTE

 Eg:charc_sql
str
ing[
]={
"DELETEFROM Sai
lor
sWHEREr
ati
ng>5"
};

EXECSQLPREPAREr
eady
togoFROM :
csql
str
ing;

EXECSQLEXECUTEr
eady
togo;

 Thefi
rstst
atementdeclar
est
heCvari
abl
ec_sql
str
ingandi
nit
ial
izesi
tsv
aluet
o
thest
ri
ngrepresent
ati
onofanSQLcommand

 Thesecondst
atementr
esul
tsinthisstri
ngbeingparsedandcompil
edasan
SQL command,withthe resul
ti
ngexecutabl
e bound t
ot he SQL var
iabl
e
ready
togo.

 Thet
hir
dst
atementexecut
est
hecommand
St
ati
c(Embedded)SQL Dynami
c(I
nter
act
ive)SQL

InStati
cSQL, howdatabasewi
llbeaccessed
ispredeter
minedintheembeddedSQL InDynamicSQL,howdat
abasewi
llbe
stat
ement . accessedi
sdetermi
nedatr
unti
me.

I
tismor
eswi
ftandef
fi
cient
. I
tisl
essswi
ftandef
fi
cient
.

SQLst
atement
sar
ecompi
ledatcompi
let
ime. SQLst
atement
sar
ecompi
ledatr
unt
ime.

Par
sing,
Val
idat
ion,
Opt
imi
zat
ionand Par
sing,
Val
idat
ion,
Opt
imi
zat
ionand
Gener
ati
onofappl
icat
ionpl
anar
edoneat Generat
ionofappl
icat
ionpl
anar
edoneat
compi
leti
me. runti
me.

Itisgeneral
lyusedf
orsi
tuat
ionswher
edat
ais Iti
sgeneral
lyusedf
orsi
tuati
onswher
e
distr
ibut
eduniforml
y. datai
sdistr
ibut
ednonunif
ormly.

EXECUTEI
MMEDI ATE,EXECUTEand EXECUTEI
MMEDI ATE,EXECUTEand
PREPAREst
atementsarenotused. PREPAREst
atementsareused.

I
tisl
essf
lexi
ble. I
tismor
efl
exi
ble.

Li
mit
ati
onofDy
nami
cSQL:

Wecannotusesomeoft
heSQLst
atement
sDy
nami
cal
ly.

Per
for
manceoft
hesest
atement
sispoorascompar
edt
oSt
ati
cSQL.

Li
mit
ati
onsofSt
ati
cSQL:

Theydonotchangeatr
unt
imet
husar
ehar
d-codedi
ntoappl
icat
ions.

Q5c)Consi
dert
hef
oll
owi
ngCOMPANYdat
abase (10
marks)

EMP(
Name,
SSN,
Sal
ary,
Super
SSN,
Dno)

DEPT(
DNum,
Dname,
Mgr
SSN,
Dno)

DEPT_LOC(
Dnum,
Dlocat
ion)

DEPENDENT(
ESSN,
Dep_name,
Sex)

WORKS_ON(
ESSN,
Pno,
Hour
s)

PROJECT(
Pname,
Pnumber
,Pl
ocat
ion,
Dnum)

Wr
it
etheSQLquer
iesf
ort
hef
oll
owi
ng

i
)Ret
ri
evet
henameoft
heempl
oyeewhowor
kswi
thsamedepar
tmentasr
avi

i
i)
Ret
ri
evet
henumberofdependent
sforanempl
oyee“
Ravi

i
ii
)Ret
ri
evethenameoft
hemanager
swor
kingi
nlocat
ion“
DELHI
”whohasno
f
emaledependent
s

i
v)Li
stf
emal
eempl
oyeesf
rom Dno=20ear
ningmor
ethan50000

v)
List“
CSE”depar
tmentdet
ail
s
1)SELECTname,
dnoFROM employ
eee,
depar
tmentdwher
ednoI
N(SELECTdno
FROM empWHEREname=’
ravi
’;

i
i)SELECT COUNT(
*) FROM dependent
s d,
emp e WHERE e.
ssn=d.
essn AND
name=’rav
i’
;

i
ii
)SELECTE.NAMEFROM EMPLOYEEE , DEPARTMENTD,DEPTLOCATI ONS DL
WHEREE. SSN=D.MGRSSN ANDD. DNO=DL.DNOANDDLOCATION=’DELHI ’wher
e
NOTEXI STS(
SELECT FROM empl
oyeewhered.dnumber
=e.
dnoandd.sex=’F’

I
v)sel
ectssnf
rom empwher
edno=20andsal
ary
<500000

v
)SELECT*FROM deptwher
edname=’
cse’
;

Q6a)
.Whati
sSQLJ?Howi
tisdi
ff
erentf
rom JDBC.

SQLJ

 SQLJ(
shor
tforSQL-Jav
a)dev
elopedbygr
oupofdat
abasev
endor
sandSun

I
twasdevel
opedtocomplementt
hedy
nami
cwayofcr
eat
ingquer
iesi
n
JDBCwi
thastat
icmodel

I
tisv
erycl
oset
oembeddedSQL

Ithassemi -st
ati
cSQLquer i
esall
owsthecompilertoperfor
m SQlsynt
ax
checks,str
ongt y
pechecksof t
hecompati
bil
it
yoft hehostv ar
iabl
eswi
th
respect
iveSQLattr
ibut
es,
viewsandst
oredpr
ocedureatcompli
ati
onti
me.

SQLJ(
stat
ic) JDBC(
dynami
c)

PERFORMANCE stat
ic SQL i
sf ast
er Dynami c SQL st at
ements
thandynami
cSQL, requir
et heSQLstatementsto
be parsed, t able/
view
authori
zationtobechecked,

AUTHORI
ZATI
ON WithSQLJ,theowner Wi thJDBC,t heowneroft he
of t he applicat
ion appl
icat
ion grant
s pri
vil
eges
grants EXECUTE on al lthe under
lyi
ng tabl
es
authori
tyon t
he plan that ar e used by t he
orpackage,and t he appl
icat
ion.
reci
pient of t hat
GRANT mustr un the
appli
cati
on

Q6b)Dr
awandexpl
ain3-t
ierAr
chi
tect
ureandt
echnol
ogyr
elevantt
oeacht
ier
.
Wr
it
etheadvant
agesof3-t
ierar
chi
tect
ure.

Thet
hreet
ierappl
icat
ionar
chi
tect
ure

Dat
aintensi
vei
nter
netappl
icat
ionscanbeunder
stoodi
nter
msoft
hreef
unct
ional
components

Dat
amanagement
,appl
icat
ionl
ogi
candpr
esent
ati
on

Si
ngl
eTi
erandCl
ient
-Ser
verAr
chi
tect
ures

Dat
aintensi
vei nt
ernetappl
icat
ionswerecombi
nedi
ntoasi
ngl
eti
er,i
ncl
udi
ng
DBMS,appli
cat
ionlogicandt
heuserint
erf
ace.

Theappli
cat
iontypi
cal
lyr
anonamai nf
rameandusersaccessedi
tthr
oughdumb
ter
minal
sthatcoul
dpefor
m onl
ydat
ainputanddi
spl
ay

Advant
age:easi
lymai
ntai
nedbyacent
raladmi
nist
rat
or

Di
sadv
atages:

- user
sexpectGUIt
hatr
equi
remor
ecomput
ati
onalpowert
hansi
mpl
edumb
t
erminal
s

-donotscal
efort
housandsofuser
s

Thecommoditi
zati
onofPCandt heavail
abil
it
yofcheapcl
ient
scomput
ersl
edt
o
thedev
elopmentofthet
wot
ierar
chit
ecture.

Twot i
erarchi
tect
urealsoref
err
edascl
ientserv
erar
chi
tectur
e,consi
stsofcl
ient
computerand ser v
ercomputer
,whi
ch int
eract
s wi
tht hr
ough a welldefi
ned
prot
ocols.

Cli
ent
simplementjustt
hegraphi
caluseri
nter
faceandt
heserveri
mplement
sbot
h
thebusi
nesslogicandt hedatamanagement ,suchcl
ient
sar ecal
ledasThi
n
Cli
ent
s.

Cli
entsthati
mplementbot
huserinterf
aceandbusi nesslogi
c,wit
htheremai
ning
partbei
ngimplement
edattheser
verlevel
,suchcl
ientsarecal
ledasThi
ckCli
ents.

Thickcl
ient
smodelhasdi sadvantagesoverThincl
ient
smodelNocent r
alpl
aceto
updateandmai ntai
nthebusinesslogic,si
nctheapplicat
ionr unsatmanysites-
Largeamountoft r
usti
srequiredbetweenclientandserv er.
eg:ATM -Doesnot
scalewit
hthenumberofcl ients-Donotscal east heappl i
cati
onaccessesmore
andmoredat abasesyst
ems
Thr
eeTi
erAr
chi
tect
ure

t
hreeti
erar
chi
tect
uregoesonest
epf
urt
herseper
atesappl
icat
ionl
ogi
cfr
om dat
a
management

 Pr
esent
ati
onTi
er:

-userrequi
resanat
urali
nter
facet
omaker
equest
spr
ovi
dei
nputandt
osee
r
esult
s

-t
hisl
ayerofcodei
seasyt
oadoptt
odi
ff
erentdi
spl
aydev
icesandf
ormat
s

-i
tdeci
deshowt
odi
spl
ayt
hei
nfor
mat
ion

-eg:webbasedi
nter
faces

 Mi
ddl
eTi
er:

-Appl
icat
ionl
ogi
cexecut
esher
e

-Itcontr
olswhatdat
aneedst
o bei
nputbef
oreanact
ion canbeexecut
ed,
det
ermi
nesthe

contr
olflow bet
weenmult
iact
ionst
eps,cont
rol
saccesst
oDBl
ayer
,assembl
es
dynamicall
ygenerat
edHTMLpagesf
rom DBquer
y

 Dat
aManagementTi
er:
-Dat
aint
ensi
vewebappl
icat
ionsi
nvol
veDBMS

-I
nfor
mat
ioni
sst
oredandr
etr
iev
edf
rom DB

-Inf
ormat
ioni
spassedbackt
othel
ogi
cti
erf
orpr
ocessi
ngandev
ent
ual
lybackt
o
t
heuser

-Technol
ogi
esusedar
eXML,
stor
edpr
ocedur
es

Advant
agesofThr
eeTi
erAr
chi
tect
ure

Heterogeneous Sy stems:Applicat
ions can uti
li
ze t
he str
engths ofdi
ff
erent
pl
atformsanddi f
ferentsof
twar
ecomponent satt hedif
fer
entti
ers.Iti
seasyt o
modifyorreplacethecodeatanyt i
erwit
houtaffect
ingt
heothert
iers.

Int
egrat
eddataaccess:Inmanyappl
icati
ons,thedatamustbeaccessedf r
om
sever
alsour
ces.Thi
scanbehandledt
ransparentl
yatthemiddl
eti
er,wher
eitcan
centr
all
ymanagetheconnect
ionst
oal
lDBsy stemsinv
olved.

Scal
abi
li
tyt
oManyCl
ient
s:

Eachclienti
sl ight
weightandal laccesstothesystem isthrought hemi ddl
et i
er.
Themi ddletiercansharedat abaseconnecti
onsacr osscli
ent s,andi fthemi ddl
e
ti
erbecomest hebott
le-neck,wecandepl oyseveralserver
sexecut ingthemi ddl
e
ti
ercode;clientscanconnectt oany oneoftheseser ver
s,i
ft hel ogicisdesigned
appr
opr i
atel
y.

SoftwareDev el
opmentBenef its:Bydiv
idingt heappl i
cati
oncleanlyint
opar t
st hat
addresspresentat
ion,dataaccess,andbusi nessl ogic,wegainmanyadv antages.
Thebusi nesslogi
ci scentral
ized,andi sther ef
oreeasyt omai ntai
n,debug,and
change.Inter
acti
onbet weentiersoccursthroughwel l
-defi
ned,standardi
zedAPl s.
Therefor
e,eachappl i
cati
ont i
ercanbebui l
toutofr eusablecomponent sthatcan
beindivi
duall
ydeveloped,debugged,andt ested.
Q6Expl
ainst
oredpr
ocedur
ewi
thexampl
e

I
ti s someti
mes usef ulto cr
eat
e database pr
ogram modules (procedur
es or
funct
ions)
-thatarest or
edandexecutedbyt heDBMS att hedat abaseserv
er.
Thesearehistori
cal
lyknownasdatabasestoredprocedur
es,al
thoughtheycanbe
funct
ionsorprocedures.

St
oredpr
ocedur
esar
eusef
uli
nthef
oll
owi
ngci
rcumst
ances:

a)I
fadat
abasepr
ogr
am i
sneededbysev
eralappl
icat
ions,
itcanbest
oredatt
he

ser
verandi
nvokedbyanyoft
heappl
icat
ionpr
ogr
ams.Thi
sreducesdupl
icat
ion

ofef
for
tandi
mpr
ovessof
twar
emodul
ari
ty.

b)Execut
ingapr
ogr
am att
heser
vercanr
educedat
atr
ansf
erandhence

communi
cat
ioncostbet
weent
hecl
ientandser
veri
ncer
tai
nsi
tuat
ions.

c)Thesepr
ocedur
escanenhancet
hemodel
ingpowerpr
ovi
dedbyv
iewsby

al
lowi
ngmor
ecompl
ext
ypesofder
iveddat
atobemadeav
ail
abl
etot
he

dat
abaseuser
s.SELECTC.
cid,
C.cname,
COUNT(
*)FROM Cust
omer
sC,

Or
der
saWHEREC.
cid=O.
cidGROUPBYC.
cid,
C.cname

Q7a)Explai
ninser
tion,
delet
ionandmodif
icat
ionanomal
ies.Whyar
ethey
consi
deredbad?Il
lustr
atewithexampl
e

Designar el
at i
onschemasot hatitiseasytoexpl
ainitsmeaning.Donotcombi ne
att
ri
butesf rom mult
ipl
eent it
yt ypesandr el
ati
onshiptypesint
oasi nglerel
ati
on.
Reducing redundantv alues int upl
es.Save storage space and avoi
d update
anomalies.
I nsertionanomalies.
 Del eti
onanomal i
es.
 Modi ficati
onanomal i
es

I
nsert
ionAnomal
ies
To i
nsertanew empl
oyeet
upl
eint
o EMP_DEPT,we musti
ncl
ude ei
thert
he
att
ribut ev aluesf ort hatdepartmentt hattheempl oy eewor ksf or,ornul ls.I t
's
dif
ficulttoi nsertanewdepar tmentt hathasnoempl oy eeasy etint heEMP_DEPT
rel
ation.Theonl ywayt o do thisist o placenullv al
uesi nt heat t
ri
but esf or
empl oy ee.Thi scausesapr obl
em becauseSSNi sthepr i
mar ykeyofEMP_DEPT,
andeacht uplei ssupposedt orepresentanempl oyeeent ity-notadepar tment
enti
ty.
DeletionAnomal i
es
Ifwedel etef r
om EMP_DEPTanempl oyeetuplethathappenst orepr esentt hel ast
empl oy eewor ki
ngf orapar ti
culardepar t
ment ,thei nfor
mat ionconcer ningt hat
depar t
menti slostf r
om thedatabase.
Modi ficati
onAnomal ies
InEMP_DEPT,i fwechanget hev alueofoneoft heat tr
ibutesofapar ticular
depar t
ment -sayt hemanagerofdepar tment5-wemustupdat et het uplesofal l
empl oy eeswhowor kinthatdepartment.

Q7b)Explain1NF,2NF, 3NFwithexampl e.
Fir
stNormalForm( 1NF)
I
tstat
esthatthedomai nsofatt
ribut
esmusti ncl
udeonl
yat
omic(si
mple,i
ndivi
sible)
val
uesandt hatt
hev al
ueofanyat tr
ibut
ei natupl
emustbeasingl
evaluefr
om t he
domainofthatatt
ri
bute.Itdi
sal
lowsmul t
ival
uedandcomposi
teatt
ri
bute.

Pract
icalRule:"El
imi
nat
eRepeat ingGr
oups,"i
.e.,makeaseparat
etabl
eforeach
setofrelat
edattri
but
es,
andgiveeachtableaprimarykey.
FormalDef ini
ti
on:A relat
ion isinfi
rstnormalf orm (1NF)i
fand onl
yifall
underl
yingsimpledomainscontai
natomicval
uesonly.

SecondNor malFor m( 2NF)


Secondnor malf ormi sbasedont heconceptoff ullyfunct
ionaldependency.A
functi
onalX->Yi safullyfunct
ionaldependencyisr emov alofanyatt
ribut
eAf r
om
Xmeanst hatthedependencydoesnothol danymor e.Arelat
ionschemai sin2NF
ifever
ynonpr imeattr
ibuteinrelat
ionisful
lyfunct
ional l
ydependentont heprimary
keyoft herelati
on.I
tal socanber est
atedas:ar elationschemai sin2NFi fevery
nonpri
me att
ribut
ei nr el
ati
on is notpart
ial
ly dependenton any key oft
he
rel
ati
on.Pr
act
icalRule:"
Eli
minateRedundantData,"i.
e.,i
fanat
tri
but
edependson
onlypar
tofamul t
ival
uedkey,removeitt
oasepar atetabl
e.

For
malDefi
nit
ion:Arel
ati
onisi
nsecondnor malfor
m( 2NF)i
fandonl
yifi
tisi
n
1NFandev
erynonkeyat
tri
but
eisful
lydependentont
hepri
marykey

Thir
dNor malForm(3NF)
Thir
dnor malformisbasedont heconceptoft ransi
ti
vedependency
.Af uncti
onal
dependencyXYi narel
ati
onisat r
ansit
ivedependencyi ft
her
eisasetofat tri
butes
Zthatisnotasubsetofanykeyoft herel
ation,andbothXZandZYhol d.Inother
words,arelat
ionisin3NFif,wheneveraf uncti
onaldependencyXAhol dsint he
rel
ati
on,eit
her(a)Xisasuperkeyoftherel
at i
on,or(b)Aisaprimeattr
ibuteoft he
rel
ati
on.

Pract
icalRul
e:"
Eli
minat
eColumnsnotDependentonKey,
"i.
e.,ifat
tri
but
esdonot
contr
ibutet
oadescri
pti
onofakey
,removethem t
oaseparat
et abl
e.

For
malDefi
niti
on:Arelat
ionisi
nthirdnormalfor
m( 3NF)i
fandonl
yifiti
sin2NF
andev
erynonkeyat
tri
buteisnont
ransit
ivel
ydependentont
hepri
marykey.

1NF:Risin1NFi f
fal
ldomainval
uesareatomic.
2NF:Risin2NFi ffRisi
n1NFandev erynonkeyat
tri
but
ei sf
ull
ydependentonthe
key.
3NF:R isi n 3NF i
ffR is2NF and everynonkeyatt
ribut
ei snon-transi
ti
vel
y
dependentonthekey.

7c)Consi
dert
her
elat
ionschemaR(
A,B,
C,D,
E,F)andt
hef
unct
ionaldependenci
es
A->B,C->DF,AC->E,D->F.
Whatisthepr
imarykeyoft hi
srel
ati
onR?Whati
sits
highestnor
malf or
m? Pr
eservi
ngt
hedependency,
decomposeRintot
hir
dnormal
form.

St
epstofi
ndt hehighestnor malf orm ofarelat
ion:
1.Fi
ndal lpossiblecandidatekeysofther el
ati
on.
2.Divi
deal latt
ributesintotwocat egori
es:pri
meat tr
ibut
esandnon-pr
ime
at
t r
ibutes.
3.Check f or1st normalf orm then 2nd 
and so on.I fitf
ail
sto sat
isf
y
t

n nor malform conditi
on, hi
ghestnormalform wi
llben-1.
+
Step1.  Aswecansee,( AC)  ={
A,C,B,
E,D}
 butnoneofit
ssubsetcandeter
mi ne
al
latt
ributeofr el
ati
on,SoACwi llbecandidat
ekey.AorCorDcan’ tbederived
fr
om anyot heratt
ri
buteoft
her el
ati
on,sotherewi
llbeonl
y1candi
datekey{AC} .

Step2.
 Pri
meatt
ri
butear
et hoseat
tri
butewhicharepartofcandi
dat
ekey{
A,C}i
n
thi
sexampl
eandother
swillbenon-pri
me{B,D,E}i
nthisexample.
nd
Therelat
ionisnotin2  nor
malform becauseA-B( Ais pr
opersubsetof
nd
candi
datekeyAC)andC->DFi snotin2  normalfor
m(Cis propersubsetof
candi
date keyAC)and DF isin 2nfbecause(
Di snota pr
opersubsetof
candi
datekeyAC).
nd
Ther el
ati
onisnoti n2  Normalform becauseA->Bisparti
aldependency(
A
whichissubsetofcandidatekeyACi sdetermini
ngnon-pr
imeattr
ibut
eB)and
nd
2  normalfor
m doesnotall
owpart
ialdependency.
r
d
Therel
ati
onisnotin3  nor malfor
m becausei
nAB(neit
herAisasuperkeynor
Bisapr i
meattri
bute)andi nCDF( neit
herCisasuperkeynorDFisapr i
me
r
d
at
tri
but
e)buttosati
sfy3   normalfor
,eit
herLHSofanFDshoul
dbesuperkeyor
RHSshouldbepri
meat t
ribut e.

Sot
hehi
ghestnor
malf
orm wi
llbe1st 
Nor
malFor
m

8a)Def i
nenon-addi t
ivejoinpr opert
yofadecomposi t
ionandwr i
teanal gor
it
hm
fort esti
ngofnonaddi ti
vejoinpr opert
y
DependencyPr eservati
on Pr opertyenablesust o enforceaconst r
ainton t he
originalrelati
on from cor responding inst
ances int he smallerrelat
ions.Ifwe
decomposear elat
ionRi ntoR1&R2al ldependenciesofRmustbeapar tofR1&R2
ormustbeder i
vablefrom combi nati
onofFD’ sofR1&R2
Lossl essjoinpropertyenabl esust ofindanyinstanceoft heorigi
nalrelat
ionfrom
corresponding instances i nt he smal l
err el
ati
ons ( Both used by the desi gn
algor i
thmst oachi evedesi rabl
edecomposi ti
ons).A pr oper
tyofdecomposi t
ion,
whi chensur esthatnospur i
ousr owsar egeneratedwhenr elati
onsar ereunited
throughanat ur
aljoinoperation.
Algor i
thm 11. 1Test ingf ort hel ossless( nonaddi t
ive)j
oinpr opert y
Input:Auni versalrelationR,adecomposi ti
onDECOMP={ R1;R2;:::;Rn}ofR,
andasetFoff uncti
onaldependenci es
.1.Cr eateani nit
ialmat ri
xSwi thoner owif oreachr elati
onRii nDECOMP,and
onecol umnjf oreachat t
ri
but eAj i
nR.
2.SetS( i;j):=bi jf
oral lmat ri
xent ries.3.Foreachr ow if oreachcol umnji fRi
i
ncludesat tr
ibuteAj ThensetS( i;j):=aj
4.Repeatt hef oll
owi ngl oopunt ilacompl eteloopexecut i
onr esultsinnochanges
toSForeachX->Yi n
Ff oral lrowsi nSwhi chhast hesamesy mbol sinthecol umnscor r
espondi
ngt o
attr
ibutesi nXmaket hesy mbol si neachcol umnt hatcor r
espondt oanattri
butein
Ybet hesamei nalltheser owsasf ollows:ifanyoft her owshasan“ α”symbolfor
thecol umn,sett heot herr owst ot hesame“ α”sy mboli nt hecol umn.Ifno“ α”
symbolex ist
sf ortheat tri
but einanyoft herows, chooseoneoft heβsy mbolsthat
appeari noneoft her owsf ort heat tri
buteandsett heot herrowst othatsame
symbolβi nt hecolumn
5.Ifar ow ismadeupent i
relyof“ α”sy mbol s,thent hedecomposi t
ionhast he
losslessjoinpr operty; otherwi sei tdoesnot .

8c)Wr
it
etheal
gor
it
hm t
ofi
ndt
hemi
nimalcoverf
oraset
sofFD’
s

ConsiderR={A,B,
C,D,
E,F}.
FD’s{A->C,
AC->D,
E->AD,
E->H}Fi
nd t
hei
rr
educi
ble
coverfort
hissetofFD’
s(mini
malcover
)
Sol
ution:

1.Br
eakdown 
the 
RHS 
ofeach 
funct
ionaldependency 
int
oa 
singl
eat
tri
but
e

F={AC
ACD
EA
E->D
EH

Next

mini
mizet
heLHS

Tryi
ng t o 
remov unecessar
e  y at
tri
but
es 
fr
om t
he 
LHS 
of each 
funct
ional
dependency

NOTE: 
y onl
ou  y 
need t
o consi
der
 f
unct
ionaldependenci
es 
whose 
LHS 
has 
2 or
moreatt
ri
butes 
!

AC→ D:CheckifAisneccessar
y
Repl
ace"
AC→ D"wi t
h" eC+
C→ D"andcomput
fD∈ C+,
I thenAisunnecessar
y
eC+:
Comput

C+=C -No
C→ D:Checki
fCisneccessar
y
Repl
ace"AC→ D"with" eA+
A→ D"andcomput
IfD∈ A+,t
henCisunnecessar
y

eA+:
Comput

A+=A (A→ C)
=AC (AC→ D)
=ACD -Done-YES!

Resul
t:

℉={A→ C
A→D
E→ A
E→ D
E→ H}
Next

mini
mizet set
he   off
unct
ionaldependenci
es

Tr
yingt
o r unecessar
emove  y f
unct
ionaldependenci
es

℉={A → C
A→D
E→ A
E→ D
E→ H}

1."
A→ C"
:isunnecessar
yifA+cont
ainsCwi
thoutusi
ngA→ C

A+=A (A → D)
=AD Done

A→ Ci
snecessar
y

2."
A→ D"
:isunnecessar
yifA+cont
ainsDwi
thoutusi
ngA→ D

A+=A (A → C)
=AC Done
A→ Di
snecessar
y

3."
E→ A"
:isunnecessar
yifE+cont
ainsAwi
thoutusi
ngE→ A

E+=E (E→ D)
=DE (E→ H)
=DEH Done

E→ Ai
snecessar
y

+
3."
E→ D"
:isunnecessar
yifE cont
ainsDwi
thoutusi
ngE→ D

+
E =E (
E→ A)
=AE (
E→ H)
=AEH (
A → C)
=ACEH (A → D)
=ACDEH Done

E→ Di
sunnecessar
y!!
!

+
3."
E→ H"
:isunnecessar
yifE cont
ainsHwi
thoutusi
ngE→ H

+
E =E (
E→ A)
=AE (
E→ D)
=ADE (
A → C)
=ACDE (A → D)
=ACDE Done

E→ Di
snecessar
y

1.Resul
t:

℉={A→ C
A→D
E→ A
E→ H}
2.Gr
oupthe 
funct
ionaldependenci
es 
thathav
e commonLHS 
toget
her
Fi
nalResul
t:

℉={A→ CD
E→ AH}
The r esul
ti
ng set of funct
ional dependenci
es 
is a 
mini
mal cover
 of
t or
he  i
ginalset
 off
unct
ionaldependenci
es

The 
mi ni
mal cover i
s equivalent
 t
o t he or
iginal set  
of f uncti
onal
dependenci
es 
but
 mayhav e 
af ewernumberof 
functi
onaldependencies

8c)Given bel
ow aretwo sets ofFD’sf ora rel
ati
on R(A,
B,C,
D,E)
.Ar
ethey
equi
val
ent?F={A->C,
AC->D,
E->AD,E->H}andG={A->CD,E->AH}

FCOVERSG GCOVERSF
A+={
ACD} A+={ACD}
E+={EAHCD} AC+={
ACD}
E+={EACDH}
Det
ermi
ningwhet
herFcover
sG-
+
 (
A) 
={A,
C,D}
        
//cl
osur
eofl
eftsi
deof
 A 
→ CDusi
ngsetF
+
 (
E) 
={A,
C,D,
E,H}
  /
/ cl
osur
eofl
eftsi
deofE 
→ AHusi
ngsetF
 
Thus,
weconcl
udeFcover
sGi
.e.F 
⊇G

Det
ermi
ningwhet
herGcover
sF-
+
 (
A) 
={A,
C,D}
        /
/cl
osur
eofl
eftsi
deof
 A 
→ Cusi
ngsetG
+
 (
AC) 
={A,
C,D}
       
// 
closur
eofl
eftsi
deofAC→ Dusi
ngsetG
+
 (
E) 
={A,
C,D,
E,H}
  /
/ cl
osur
eofl
eftsi
deofE 
→ ADandE→ Husi
ngsetG
 

Det
ermi
ningwhet
herbot
hFandGcovereachot
her
-
Fr
om St
ep-01,
weconcl
udeFcov
ersG.
From St
ep-02,
weconcludeGcov
ersF.
Thus,weconcl
udebot
hFandGcov ereachot
heri
.e.F=G.

Q9 a)Bri
efl
ydiscussont hetwo phasel
ocki
ng pr
otocolusedi
nconcur
rency
cont
rol.Howdoesit
i
tgurant
eesseri
ali
zabi
li
ty.
Two-PhaseLocki ngTechni quesf orConcur rencyCont rol
 Some oft he mai nt echni ques used t o cont rolconcur rentexecut i
on of
transact ionsar ebasedont heconceptofl ockingdat ai t
ems.
 Al ocki sav ariableassoci at edwi t
hadat aitem t hatdescr i
best hest atusof
thei tem wi thr espectt opossi bleoper ationsthatcanbeappl i
edt oit.
Ty pesofLocksandSyst em LockTabl es
Bi naryLocks.
 Abi nar yl ockcanhav et wost atesorval ues:lockedandunl ocked( or1and0,
forsi mpl ici ty).
 Adi st i
nctl ocki sassoci atedwi theachdat abasei tem X.
I ft hev al ueoft hel ockonXi s1,i em Xcannotbeaccessedbyadat
t abase
oper ationt hat
request st hei tem.
I ft hev al ueoft helockonXi s0,t hei tem canbeaccessedwhenr equested,
andt hel ockv alue
i
schangedt o1.Wer efertot hecur rentv alue( orstat e)oft hel ockassoci atedwi th
i
t em Xas
lock( X).
 Twooper at ions, l
ock_i tem andunl ock_i t
em, ar eusedwi thbi narylocking.
 At ransact ionr equest saccesst oani tem Xbyf irsti ssui ngal ock_item( X)
oper ation.
IfLOCK( X)=1, thet ransact ionisf orcedt owai t.
IfLOCK( X)=0,i ti ssett o1( thet ransact ionl ockst hei tem)andt he
transact ioni sal l
owedt oaccessi tem X.
 Whent het ransact i
oni st hroughusi ngt hei t
em,i ti ssuesanunl ock_item( X)
oper ation,whi chset sLOCK( X)backt o0( unlockst hei tem)sot hatXmaybe
accessed by ot hert ransact ions.Hence,a bi nar yl ock enf or ces mut ual
excl usi onont hedat aitem.
 Adescr ipt ionoft hel ock_i tem( X)andunl ock_i tem( X)oper ationsi sshowni n
Figur e.
 Thesy st em needst omai ntai n onlyt heser ecor dsf ort hei temst hatar e
cur rent lyl ockedi nal ockt abl e,whi chcoul dbeor gani zedasahashf i
leon
thei t em name.I t
emsnoti nt hel ockt ablear econsi deredt obeunl ocked.The
DBMShasal ockmanagersubsyst em t okeept rackofandcont r
olaccesst o
locks.
Thesi mpl ebi nar yl ockingschemedescr i
bedher ei sused;ev eryt ransact i
onmust
obeyt hef ol l
owi ngr ules:
1.At ransact ionTmusti ssuet heoper ati
onl ock_i tem( X)bef oreanyr ead_i t
em( X)
orwr i
te_i tem( X)
oper ationsar eper f
or medi nT.
2.At ransact ionTmusti ssuet heoper ationunl ock_i tem( X)af teral lread_i t
em( X)
andwr i
te_i tem( X)
oper ationsar ecompl etedinT.
3.At ransact ionTwi llnoti ssueal ock_i t
em( X)oper ationi fital readyhol dsthelock
oni tem X.
4.A t ransact ion T wi llnoti ssueanunl ock_item( X)oper at ionunl essi talr
eady
holdsthel ockoniem X.
t
Theser ulescanbeenf orcedbyt helockmanagermodul eoftheDBMS.Bet ween
thelock_item(X)
andunl ock_it
em(X)operationsi
ntransactionT,Tissai
dtohol
dthelockonitem X.
Atmostone
transacti
oncanhol dthel ockonapar ticulari
tem.Thusnotwotransact
ionscan
accesst hesameitem
concurrently.

Shared/Exclusi
ve(orRead/ Writ
e)Locks.
 Thepr ecedi
ngbi nar
yl ockingschemeist oorest
ri
ctivefordatabaseit
ems
becauseatmost
one transacti
on can holdal ock on a giv
en item.We shoul d al
low several
tr
ansactionsto
accessthesamei tem XiftheyallaccessXforreadi
ngpurposesonly.
 Thi sisbecauser eadoper ati
onsonthesamei tem bydif
ferenttr
ansact
ions
arenotconfl
ict
ing.

Howev er, i
fat ransact ioni stowr i
teani em X,
t itmusthav eexclusiveaccesst oX.
 Fort hi spur pose,adi fferentt ypeofl ock,cal ledamul tipl
e-model ock,i s
used.I nt hi
s
scheme—cal l
ed shar ed/ exclusive orr ead/wr itel ocks—t here aret hree l
ocking
operations:
read_lock(X) ,wr it
e_l ock( X),andunl ock(X).
 Al ockassoci at edwi thani tem X,LOCK( X),now hast hreepossi blestates:
read-l ocked,wr i
telocked,orunl ocked.A r ead-l ockedi tem isal socal l
ed
shar e-l ocked because ot hert ransactions ar e all
owed t oread t he i
tem,
wher easawr ite-l ockedi tem i scal l
edexcl usive-lockedbecauseasi ngle
transact ionexcl usi vel
yhol dst hel ockont heitem.
 The t hr ee oper ations r ead_l ock(X), wr it
e_l ock(X), and unl ock(X) are
descr ibedi nFi gur eAsbef or e,eachoft het hreel ockingoper at
ionsshoul dbe
consi der edi ndi visible;no i nterleavi
ng shoul dbeal lowedonceoneoft he
oper ationsi sst ar tedunt ileit
hert heoper at i
ont erminatesbygr antingthelock
ort het ransact ioni splacedi nawai ti
ngqueuef orthei t
em.
Theshar ed/ excl usivel ockingscheme,t hesyst em mustenf orcet hef ol
lowing
rules:
1.At ransact onTmusti
i ssuet heoper ationr ead_l ock( X)orwr ite_lock( X)bef ore
anyr ead_i tem( X)
oper at i
oni sper for medi nT.
2.At ransact ionTmusti ssuet heoper ationwr it
e_lock( X)bef oreanywr ite_i tem( X)
oper at i
oni s
perfor medi nT.
3.At ransact onTmusti
i ssuet heoper at ionunl ock( X)af teral lr ead_i tem( X)and
write_i tem( X)
oper at i
onsar ecompl et edi nT.
4.At ransact ionTwi llnoti ssuear ead_l ock( X)oper ationi fitalreadyhol dsar ead
(shar ed)l ockora
write( excl usi ve)l ockoni em X.Thi
t sr ulemayber elaxedf ordowngr adi ngofl ocks,
aswedi scuss
shor tly.
5.At ransact ionTwi llnoti ssueawr i
t e_l ock( X)oper ationi fitalreadyhol dsar ead
(shar ed)l ockorwr ite(ex clusiv e)l ockoni tem X.Thi sr ulemayal sober el axedf or
upgr adi ngofl ocks, aswedi scussshor tl
y .
6.At ransact onTwi
i llnoti ssueanunl ock( X)oper ationunl essi tal readyhol dsa
read( shar ed)l ockorawr i
te( ex clusive)l ockoni em X.
t
 At ransact i
oni ssai dt of ol l
owt het wo-phasel ocki ngpr ot ocoli fal llocki ng
oper ations( read_l ock,wr ite_lock)pr ecedet hef irstunl ockoper at ioni nt he
t ransact ion.
 Suchat ransact ioncanbedi videdi nt ot wophases:anexpandi ngorgr owi ng
( first)phase, dur ing
whi chnew l ocksoni temscanbeacqui r ed butnonecanber eleased;anda
shrinki ng( second)phase,
duringwhi chexi stingl ockscanber eleasedbutnonewl ockscanbeacqui red.
I fl ockconv ersioni sal l
owed,t henupgr adingofl ocks( from r ead-l ockedt o
wr ite-l ocked)mustbedonedur i
ngt heexpandi ngphase, anddowngr adi ngof
locks( from wr i
te-l ockedt or ead-l ocked)mustbedonei nt heshr inking
phase.
 Tr ansact ionsT1andT2i nFi gure5. 11( a)donotf ollowt het wo-phasel ocki ng
pr ot ocolbecauset hewr ite_lock( X)oper at
ionf ol l
owst heunl ock( Y)oper at i
on
in T1,and si mi larlyt he wr ite_l ock( Y)oper at i
on f ollows t he unl ock( X)
oper ationi nT2.
I fweenf orcet wo-phasel ocking, thet ransact ionscanber ewr ittenasT1′ and
T2′ ,asshowni nFi gur e5. 12.Now, t heschedul eshowni nFi gur e5. 11( c)i snot
per mi t
tedf orT1′ andT2′ undert her ul esofl ocki ngbecauseT1′ willi ssuei t
s
wr ite_l ock( X)bef orei tunl ocksi tem Y;consequent l
y,whenT2′i ssues i t
s
r ead_l ock( X) ,iti sf or cedt owai tunt ilT1′r el easest hel ockbyi ssui ngan
unl ock( X)i nt heschedul e
Basic,Conser vat i
v e,Stri
ct ,andRi gorousTwo-PhaseLocki ng.
Therear eanumberofv ar i
ationsoft wo-phasel ocking( 2PL) .Thet echniquej ust
descri
bedi sknown
asbasi c2PL.
 Av ariat i
onknownasconser vative2PL( orst ati
c2PL)r equir
esat r
ansact ion
tolockal lt hei temsi taccessesbef orethet r
ansact i
onbegi nsexecut ion,by
predecl ar i
ngi sr
t ead-setandwr it
e-set .
 Ther ead-setofat r
ansact i
oni st hesetofal li
temst hatt hetr
ansactionr eads,
andt hewr it
e-seti st hesetofal litemst hati twrites.Ifanyoft hepredecl ared
i
temsneededcannotbel ocked,t het ransact i
ondoesnotl ockanyi tem;
i
nst ead, i
twai t
sunt ilallt hei temsar eav ailableforlocking.
 Thedi fferencebet weenst r
ictandr igorous2PL:t hef ormerhol dswr i
te-locks
untilitcommi t
s,
whereast hel atterhol dsal llocks( readandwr it
e) .
 Al so, thedi f
ferencebet weenconser vativeandr i
gorous2PLi sthatthef ormer
mustl ockal litsitemsbef orei tst arts,sooncet hetransacti
onst artsiti sin
i
tsshr inkingphase;t hel atterdoesnotunl ockanyofi tsit
emsunt ilafterit
terminat es( bycommi t
tingorabor ting),sot het ransactionisini t
sexpandi ng
phaseunt ili
tends.
 Usual lyt he concur rency cont rol subsyst em i tselfi sr esponsiblef or
gener atingt her ead_l ockandwr i
te_l ockr equests.

9b) Checkwhethergivenschedulei
sserial
izabl
eornotusi
ngprecedencegr
aph.
Explai
nwi t
halgori
thm.
S1:R1(X)R2(Z)R1(Z)R3(X)R3(Y)W1(X)W3(Y)R2(Y)W2(Z)W2(
Y)
9c)
Expl
ainBasi
cti
mest
ampi
ngal
gor
it
hm Basi
cTi
mest
ampOr
der
ing(
TO)
.

Whenev ersomet ransacti


onTt r
iestoissuear ead_i
tem(X)orawr ite_it
em(X)
operati
on,thebasicTOal gorit
hm comparesthet imest
ampofTwi t
hr ead_TS(X)
andwr it
e_TS(X)t
oensur ethatthetimest
ampor deroftransact
ionexecut i
onisnot
viol
ated.Ifthi
sorderisv i
olat
ed,thentr
ansacti
onTi sabor t
edandr esubmi t
tedto
thesystem asanewt ransacti
onwi t
hanewt i
mestamp.

IfTi sabortedandr ol
ledback,anyt r
ansactionT1t hatmayhav eusedav alue
writ
tenbyTmustal sober ol
ledback.Simil
arly,anyt r
ansacti
onT2thatmayhav e
usedav al
uewr i
tt
enbyT1mustal soberoll
edback, andsoon.Thisef
fecti
sknown
ascascadingroll
backandi soneoft heproblemsassoci at
edwit
hbasicTO,since
theschedulespr
oducedar enotguarant
eedtober ecoverable.

The concur rencycont rolal gorit


hm mustcheckwhet herconf l
icting oper ations
viol
atet het imestamp
orderingi nthef oll
owingtwocases:
1.Whenev erat ransacti
onTi ssuesawr i
te_it
em(X)operati
on,thef ollowingcheck
i
sper formed:
a.Ifr ead_TS( X)>TS( T)orifwr i
te_TS( X)>TS( T)
,thenabor tandr ollbackT
andr ejectt heoper ati
on.Thi sshoul dbedonebecausesomey oungert r
ansact ion
withat imest amp gr eatert han TS( T)—and hence af terT i nt he t imest amp
ordering—hasal readyreadorwr i
tt
ent hev alueofitem XbeforeThadachancet o
wr i
teX, thusv iol
atingthet i
mest ampor der i
ng.
b.I fthecondi ti
oninpar t(a)doesnotoccur ,thenexecutet hewr i
te_item( X)
operationofTandsetwr i
te_TS( X)t oTS( T).
2.Whenev erat ransacti
onTi ssuesar ead_item(X)operati
on,thef ollowingcheck
i
sper formed:
a.Ifwr i
te_TS(X)>TS( T),thenabor tandr ol
lbackTandr ejectt heoper at i
on.
Thisshoul dbedonebecausesomey oungert r
ansacti
onwi t
ht imest ampgr eater
thanTS( T)—andhenceaf terTi nt hetimest ampordering—hasal readywr i
tt
ent he
v
alueofit
em Xbefor
eThadachancet or
eadX.
b.I
fwrit
e_TS(
X)≤TS(T),t
henexecut
etheread_it
em(X)oper
ati
onofTandset
r
ead_TS(X)tot
helar
gerofTS(T)andt
hecurr
entread_TS(
X)

Stri
ctTi mest ampOr dering( TO).Av ari
ationofbasi cTOcal l
edst rictTOensur es
thattheschedul esar ebot hst ri
ct(foreasyr ecov er abil
ity)and( conf lict)seriali
zable.
Inthisv ariati
on,at ransact i
onTi ssuesar ead_i tem( X)orwr i
te_i tem( X)sucht hat
TS(T)>wr it
e_TS( X)hasi tsr eadorwr it
eoper ationdel ay edunt ilthet ransactionT′
thatwrot et hev alueofX( henceTS( T′
)=wr i
te_TS( X))hascommi t
tedorabor ted
Toi mpl ementt hisalgorithm,itisnecessar yt osi mul atet helocki ngofani t
em X
thathasbeenwr i
tt
enbyt ransacti
onT′ untilT′ isei t
hercommi ttedorabor ted.This
algori
thm doesnotcausedeadl ock, sinceTwai tsf orT′ onlyifTS( T)>TS( T′).
Thomas’ sWr iteRul e.Amodi fi
cati
onoft hebasi cTOal gor i
thm, knownasThomas’ s
wr i
ter ule,does notenf or
ce conf lictser i
alizabi l
it
y,buti tr eject sf ewerwr it
e
operationsbymodi f
yingt hechecksf ort hewr ite_item( X)oper ationasf oll
ows:
1.Ifread_TS( X)>TS( T),thenabor tandr ollbackTandr ejecttheoper ation.
2.Ifwr i
te_TS( X)>TS( T),thendonotexecut et hewr i
teoper ati
onbutcont i
nue
processing.
3.I fnei thert hecondi tioni npar t( 1)nort hecondi tioni npar t( 2)occur s,then
executet hewr i
te_i t
em( X)oper ati
onofTandsetwr i
te_TS( X)toTS( T) .

10a)
Expl
ainmul
tiv
ersi
onconcur
rencycont
rolpr
otocol
s.
10b)
Wri
teshor
tnot
esont
hef
oll
owi
ng

I.t
ransact
ionrol
lbackandcascadi
ngrol
lback.II
.tr
ansact
ionsupportinSQLIII
.
shadow paging I
V.NO-UNDO/ REDO RecoveryBased on Def
erred Updat
e V.
RecoveryTechni
quesBasedonImmediat
eUpdat e

Tr
ansact
ionRol
lbackandCascadi
ngRol
lback

Ifat ransact ionf ai


lsf orwhat everr easonaf terupdatingt hedat abase,but
beforet he t ransaction commi t
s,i tmay be necessar yt or ollback t he
transaction.I fanydat aitem v al
ueshav ebeenchangedbyt het ransaction
andwr it
tent ot hedat abaseondi sk,t heymustber est
or edtot heirprevi
ous
values( BFIMs) .Theundo-t ypel ogent r
iesar eusedt orestoret heol dv alues
ofdat aitemst hatmustber olledback.
Ifat r
ansact ionTi srolledback, anyt ransact i
onSt hathas, i
nt hei nter
im, r
ead
thev alueofsomedat ai t
em Xwr itt
enbyTmustal sober oll
edback.Si mi l
arly
,
onceSi sr oll
edback,anyt ransact ionRt hathasreadt hev alueofsomedat a
item Ywr i
ttenbySmustal sober oll
edback;andsoon.Thi sphenomenoni s
called cascadi ng rollback,and i tcan occurwhen t he r ecov erypr otocol
ensur esr ecov er abl
eschedul esbutdoesnotensur est ri
ctorcascadel ess
schedul es.Under st
andabl y,cascadi ngr ollbackcanbecompl exandt ime-
consumi ng.Thati swhyal mostal lrecover ymechani smsar edesi gnedsot hat
cascadi ngr oll
backi snev errequi red.
Il
lust
rat
ing cascading roll
back (
a process thatnev eroccursin str
ictor
cascadeless schedules).(a) The read and wr i
te operat
ions of thr
ee
tr
ansacti
ons.(b)System logatpointofcrash.(c)Operat
ionsbefor
ethecrash.

 Figureashowsanex ampl ewher ecascadi ngr oll


backi sr equired.Ther ead
andwr i
teoper ati
onsoft hreeindi
vidualtransact i
onsar eshowni nFigure(a).
Figure(b)showst hesystem logatthepoi ntofasy stem crashf orapar ti
cular
execut i
onschedul eoftheset r
ansactions.Thev aluesofdat ai temsA,B,C,
andD,whi char eusedbyt het r
ansact i
ons,ar eshownt other i
ghtoft he
system l ogent ri
es.Weassumet hattheor i
ginalitem v alues,showni nthe
fi
rstline,areA=30, B=15, C=40, andD=20.Att hepoi ntofsy st
em fail
ure,
tr
ansact ionT3hasnotr eachedi t
sconcl usionandmustber olledback.The
WRI TEoper ati
onsofT3,mar kedbyasi ngle*i nFigur e( b),aret heT3
operat i
onst hatareundonedur ingtransactionr oll
back.Fi gure(c)gr aphi
cally
showst heoper at
ionsoft hedif
fer
enttransact i
onsal ongt het i
meaxi s.

ShadowPagi ng
 Thi srecov eryschemedoesnotr equiretheuseofal ogi nasi ngle-user
env ir
onment .I n a mul t
iuserenv i
ronment,a log maybe needed f ort he
concur rencycont r
olmet hod.
 Shadow pagi ng considerst hedatabaset o bemadeup ofanumberof
fi
x edsize disk pages ( ordi sk bl
ocks)—say,n—forr ecover
y purposes.A
directorywi t
hnent ri
esi sconstructed,wheretheit
hent rypoint
st ot heith
dat abasepageondi sk.
 The di r
ectoryi s kepti n main memor yi fi
tis nott oo lar
ge,and al l
references—r eadsorwr ites—todatabasepagesondi skgothroughi t
.Whena
transact i
onbegi nsexecut ing,t
hecur rentdi
rect
ory
— whoseent r
iespoi ntto
themostr ecentorcur r entdat abasepagesondi sk—i scopi edi nt oashadow
director y.
 Theshadow di rector yi st hensav edondi skwhi let hecur rentdi r
ect oryis
usedbyt het ransact ion.
 Dur i
ngt ransact i
onex ecut ion, theshadowdi rect oryi snev ermodi fied.When
awr i
te_i tem oper ati
oni sper formed,anew copyoft hemodi fieddat abase
pagei scr eated, butt heol dcopyoft hatpagei snotov erwr itt
en.
 I nstead,t henew pagei swr ittenel sewher e—onsomepr ev iousl yunused
diskbl ock.Thecur r
entdi rector yent r
yi smodi fiedt opoi nttot henew di sk
block, wher east heshadowdi rect oryi snotmodi f iedandcont inuest opoi ntto
theol dunmodi fieddi skbl ock.
 Tor ecov erfrom af ailuredur ingt ransact i
onexecut ion, i
tissuf fici entt ofree
themodi fieddat abasepagesandt odi scardt hecur rentdi r
ector y .
 Thest at eoft hedat abasebef oret ransactionexecut ioni sav ailabl ethr ough
theshadowdi rectory,andt hatst atei sr ecov eredbyr ei nstati
ngt heshadow
director y.Thedat abaset husi sr etur nedt oi tsst atepr iort ot het ransact i
on
thatwasex ecutingwhent hecr ashoccur red,andanymodi f iedpagesar e
discar ded.
 Commi ttingat ransact i
oncor respondst odi scar dingt hepr ev iousshadow
director y.Si ncer ecov er yi nvolv esnei therundoi ngnorr edoingdat ai tems,
thist echni quecanbecat egor i
zedasaNOUNDO/ NO-REDO t echni quef or
recov er y.
I n a mul t
iuser env ir
onmentwi th concur rentt ransact ions,l ogs and
checkpoi nt smustbei ncor porat edi nt ot heshadowpagi ngt echni que.
 Onedi sadv antageofshadow pagi ngi sthatt heupdat eddat abasepages
changel ocat i
onondi sk.Thi smakesi tdifficultt okeepr elat eddat abase
pages cl ose t oget her on di sk wi t
hout compl ex st orage management
strategi es.
 Fur ther mor e,i ft he di rect oryi sl arge,t he ov erhead ofwr iting shadow
director ies t o di sk as t ransact ions commi ti s si gnifi
cant .A f ur t
her
compl icat ion i s how t o handl e gar bage col lect ion when a t ransact i
on
commi ts.Theol dpagesr eferencedbyt heshadowdi rectoryt hathav ebeen
updat edmustber el
easedandaddedt oal istoff reepagesf orf utureuse.
Thesepagesar enol ongerneededaf terthet ransact ioncommi ts.Anot her

i
ssuei sthattheoperat
iontomigrat
e
betweencurrentandshadowdir
ector
iesmustbeimpl
ementedasanat
omi c
operati
on.
NO-UNDO/ REDORecov er
yBasedonDeferr
edUpdate
 Thei deabehinddeferr
edupdateistodeferorpost
poneanyactual
updat est ot hedat abaseondi skunt i
lt het ransact ioncompl etesi ts
ex ecut ionsuccessf ul l
yandr eachesi tscommi tpoi nt .
 Dur i
ngt ransact i
onex ecut ion,t heupdat esar er ecor dedonl yi nthel og
andi nt hecachebuf fers.Af tert het ransact i
onr eachesi tscommi tpoi nt
andt hel ogi sf or cewr i
ttent odi sk,t heupdat esar er ecor dedi nt he
dat abase.
 I fat ransact i
onf ai lsbef orer eachingi tscommi tpoi nt ,therei snoneed
toundoanyoper at ionsbecauset het ransact ionhasnotaf f ectedt he
dat abaseondi ski nanyway .Ther ef ore,onl yREDOt y pel ogent ri
esar e
neededi nt hel og,whi chi ncludet henew v alue( AFI M)oft hei tem
wr it
t enbyawr i
teoper ation.
 TheUNDO-t y pel og ent riesar enotneeded si nceno undoi ng of
oper ationswi l
lber equi r
eddur i
ngr ecov ery.Al thought hismaysi mpl if
y
ther ecov erypr ocess,i tcannotbeusedi npr act iceunl esst ransact i
ons
areshor tandeacht ransact i
onchangesf ewi tems.
 Forot hert ypesoft ransact i
ons,t her ei st hepot ent ialf orr unni ngoutof
buf ferspacebecauset ransact ionchangesmustbehel di nt hecache
buf fer sunt ilthecommi tpoi nt,somanycachebuf fer swi llbepi nned
andcannotber epl aced.Wecanst at eat ypicaldef er redupdat epr ot ocol
asf ol lows:
1.At ransact i
oncannotchanget hedat abaseondi skunt ilitr eachesi ts
commi tpoi nt;hence al lbuf ferst hathav e been changed by t he
transact ion must be pi nned unt ilt he t ransact ion commi ts( this
cor respondst oano-st ealpol i
cy ).
2.At ransact i
ondoesnotr eachi tscommi tpoi ntunt ilallitsREDO-t ype
l
ogent riesar er ecor dedi nthel ogandt hel ogbuf feri sf orce-wr i
ttent o
disk.
 Not icet hatst ep2oft hispr otocoli sar estatementoft hewr ite-ahead
l
oggi ng( WAL)pr ot ocol .Becauset hedat abasei snev erupdat edondi sk
unt ilaf tert het ransact ioncommi t
s,t her ei snev eraneedt oUNDOany
oper ations.
 REDOi sneededi ncaset hesy stem f ai l
saf terat ransact i
oncommi ts
butbef or eal lit
schangesar erecor dedi nt hedat abaseondi sk.I nt his
case,t het ransact ionoper ationsar er edonef rom t hel ogent riesdur ing
recov ery . For mul ti
user sy stems wi th concur rency cont rol,t he
concur rencycont r
olandr ecov erypr ocessesar ei nt er related.
 Consi derasy stem i nwhi chconcur rencycont rolusesst ri
ctt wo-phase
locki ng,so t he l ocks on wr i
tten i tems r emai ni n ef fectunt ilt he
transact ion r eachesi tscommi tpoi nt.Af tert hat ,t he l ockscan be
released.Thi sensur esst rictandser iali
zabl eschedul es.
Recover
yTechni quesBasedonI mmedi ateUpdat e
I nt heset echni ques,whenat ransact i
oni ssuesanupdat ecommand,
thedat abaseondi skcanbeupdat edi mmedi atel y,wi thoutanyneedt o
wai tfort het ransact iont or eachi t
scommi tpoi nt .Not icet hati tisnota
requi rementt hatev er yupdat ebeappl iedi mmedi at el yt odi sk;i tisj ust
possi blet hatsomeupdat esar eappl iedt odi skbef oret het ransact ion
commi ts.
 Pr ov isionsmustbemadef orundoi ngt heef fectofupdat eoper ati
ons
thathav ebeenappl i
edt ot hedat abasebyaf ai l
edt ransact i
on.Thi si s
accompl ishedbyr ol l
ingbackt het ransact ionandundoi ngt heef fectof
thet ransact i
on’ swr it
e_i t em oper ations
 .Ther ef or e,t heUNDO-t ypel ogent ries,whi chi ncl udet heol dv alue
(BFI M)oft hei tem,mustbest oredi nt hel og.BecauseUNDOcanbe
needed dur i
ng r ecov er y,t hesemet hodsf ol l
ow ast ealst rategyf or
deci dingwhenupdat edmai nmemor ybuf ferscanbewr ittenbackt o
disk.T
Theor et ical ly,wecandi stingui sht womai ncat egor iesofi mmedi ate
updat eal gor ithms.
1.I ft her ecov eryt echni queensur est hatal lupdat esofat ransact ion
arer ecor dedi nt hedat abaseondi skbef oret het r ansact ioncommi ts,
ther ei s nev er a need t o REDO any oper ations of commi tted
transact i
ons.Thi si scal ledt heUNDO/ NO-REDOr ecov eryal gor ithm.I n
thismet hod,al lupdat esbyat ransact i
onmustber ecor dedondi sk
bef oret het ransact ioncommi ts,sot hatREDOi snev erneeded.Hence,
thismet hodmustut ili
zet hest eal/f orcest rategyf ordeci di ngwhen
updat edmai nmemor ybuf fer sar ewr ittenbackt odi sk.
2.I ft het ransact i
oni sal lowedt ocommi tbef or eal litschangesar e
wr it
tent ot hedat abase,wehav et hemostgener alcase,knownast he
UNDO/ REDOr ecov eryal gor ithm.I nt hiscase, t hest eal/ no-MODULE-5
forcest rat egyi sappl ied.Thi si sal sot hemostcompl ext echni que,but
themostcommonl yusedi npr actice.
Wewi llout lineanUNDO/ REDOr ecov er yal gor i
thm andl eav ei tasan
exer ci sef ort her eadert odev elopt heUNDO/ NO-REDOv ar iation.When
concur rentex ecut ioni sper mi tted,t her ecov erypr ocessagai ndepends
ont hepr ot ocol susedf orconcur rencycont rol.
The pr ocedur e RI U_M ( Recov ery usi ng I mmedi at e Updat es f ora
Mul tiuserenv ironment )out li
nesar ecov eryal gor ithm f orconcur rent
transact i
onswi thimmedi ateupdat e( UNDO/ REDOr ecov ery ).
Assumet hatt hel ogi ncl udescheckpoi ntsandt hatt heconcur r
ency
cont rolpr ot ocolpr oducesst rictschedul es—as,f orexampl e,t hest ri
ct
two-phasel ocki ngpr otocoldoes.Recal lt hatast ri
ctschedul edoesnot
allowat ransact i
ont or eadorwr i
teani tem unl esst het r
ansact iont hat
wr otet hei tem hascommi tted.Howev er,deadl ockscanoccuri nst ri
ct
two-phasel ocki ng,t husr equi ri
ngabor tandUNDOoft ransact ions.For
ast rictschedul e,UNDO ofanoper at i
onr equi reschangi ngt hei tem
backt oi tsol dv alue( BFI M) .
Pr ocedur eRI U_M ( UNDO/ REDOwi t
hcheckpoi nts) .
1.Use t wo l i
st s oft r ansact ions mai ntained by t he sy stem:t he
commi tted t ransact i
ons si nce t he l astcheckpoi ntand t he act ive
transact i
ons.
2.Undo al lt hewr i
te_i tem oper at i
onsoft he act ive( uncommi tted)
transactions,usi ngt heUNDO pr ocedure.Theoper at
ionsshoul dbe
undonei nt her everseoft heor derinwhi cht heywer ewr i
tt
eni ntot he
l
og.
3.Redoal lthewr i
te_i t
em oper at
ionsoft hecommi tt
edt ransact ions
from thel og,int heor deri nwhi chtheywer ewr i
tt
eni nt
ot hel og,usi ng
theREDOpr ocedur edef inedear li
er.
TheUNDOpr ocedur ei sdef i
nedasf ollows:
Pr ocedure UNDO ( WRI TE_OP) . Undoing a wr ite_it
em oper ation
wr it
e_opconsi stsofexami ningi t
slogent r
y[ writ
e_it
em, T,X, old_v alue,
new_v al
ue]andset tingt hev alueofitem Xint hedatabaset ool d_val ue,
whi chi sthebef orei mage( BFIM).Undoi nganumberofwr it
e_item
oper ati
onsf rom oneormor etransactionsfrom thelogmustpr oceedi n
ther everseor derfrom t heor derinwhi chtheoper ati
onswer ewr it
teni n
thel og.

Transact ionSuppor tinSQL


 Wi th SQL, t her e i s no expl icit Begi n_Transact ion st atement .
Transact ioni niti
ationi sdonei mpl icitlywhenpar ticularSQLst atement s
areencount ered.Howev er,ev eryt ransact i
onmusthav eanexpl ici tend
statement ,whi chisei t
heraCOMMI ToraROLLBACK.Ev eryt ransact i
on
hascer tainchar act eri
st icsat tributedt oi t
.Thesechar acter isticsar e
speci fi
edbyaSETTRANSACTI ONst at ementi nSQL.
 Thechar act erist
icsar et heaccessmode,t hedi agnost i
car easi ze,and
thei solati
onl evel.Theaccessmodecanbespeci f
iedasREADONLY
orREADWRI TE.Thedef aul tisREADWRI TE,unl esst hei sol ationl evel
ofREAD UNCOMMI TTED i sspeci f i
ed,i nwhi chcaseREAD ONLY i s
assumed.AmodeofREADWRI TEal lowssel ect,updat e,i nser t,del ete,
andcr eatecommandst obeexecut ed.AmodeofREADONLY,ast he
namei mpl ies,issi mpl yf ordat aretr i
ev al.
 Thedi agnost i
car easi zeopt ion,DI AGNOSTI CSI ZEn,speci fiesan
i
nt egerv aluen,whi chi ndi catest henumberofcondi t
ionst hatcanbe
heldsi multaneousl yi nt hedi agnost icar ea.Thesecondi tionssuppl y
feedbacki nfor mat ion( er rorsorexcept i
ons)t otheuserorpr ogr am on
thenmostr ecentlyex ecut edSQLst atement .
 Thei solationl evelopt i
oni sspeci fi
edusi ngt hest atementI SOLATI ON
LEVEL ,wher et he v alue f orcan be READ UNCOMMI TTED,READ
COMMI TTED,REPEATABLE READ,or SERI ALI ZABLE.The def ault
i
sol ati
onl ev elisSERI ALI ZABLE,al thoughsomesy stemsuseREAD
COMMI TTEDast hei rdef aul t
.I
I fat ransact ionex ecut esatal oweri solati
onl evelt hanSERI ALI ZABLE,
thenoneormor eoft hef ol l
owi ngt hreev i
olati
onsmayoccur :
 1.Di rt
yr ead.At ransact i
onT1mayr eadt heupdat eofat r
ansact ionT2,
whi chhasnoty etcommi tted.I fT2f ailsandi sabor ted,t henT1woul d
hav ereadav aluet hatdoesnotexi standi sincorrect.
 2.Nonr epeat abler ead.At ransact ionT1mayr eadagi venv al uef rom a
table.Ifanothert ransact i
onT2l aterupdat esthatv al
ueandT1r eads
thatvalueagai n,T1wi l
lseeadi ff
erentv alue.
 3.Phant oms.At r
ansact i
onT1mayr eadasetofr owsf r
om at able,
perhapsbasedonsomecondi t
ionspeci fiedintheSQLWHERE-cl ause.
Now supposet hatat ransact
ion T2 i nser t
sanew r ow rt hatal so
satisf
iestheWHERE-cl ausecondi ti
onusedi nT1,intot hetableused
byT1.Ther ecordri scalledaphant om r ecordbecausei twasnott here
whenT1st artsbuti sther ewhenT1ends.T1mayormaynotseet he
phantom,ar ow t hatpr ev i
ouslydidnotexi st
.Iftheequi val
entser ial
orderisT1f oll
owedbyT2, thenther ecordrshoul dnotbeseen;buti fit
isT2f oll
owedbyT1,t hent hephant om r ecordshoul dbeint her esult
givent oT1.I fthesy stem cannotensur et hecorrectbehav i
or,theni t
doesnotdealwi tht hephant om recordpr oblem.

You might also like