Beruflich Dokumente
Kultur Dokumente
ChaitanyaSingh|FiledUnder:DBMS|Updated:May2,2015
Normalizationisaprocessoforganizingthedataindatabasetoavoiddataredundancy,insertionanomaly,
updateanomaly&deletionanomaly.Letsdiscussaboutanomaliesfirstthenwewilldiscussnormalformswith
examples.
AnomaliesinDBMS
Therearethreetypesofanomaliesthatoccurwhenthedatabaseisnotnormalized.TheseareInsertion,
updateanddeletionanomaly.Letstakeanexampletounderstandthis.
Example:Supposeamanufacturingcompanystorestheemployeedetailsinatablenamedemployeethathas
fourattributes:emp_idforstoringemployeesid,emp_nameforstoringemployeesname,emp_addressfor
storingemployeesaddressandemp_deptforstoringthedepartmentdetailsinwhichtheemployeeworks.At
somepointoftimethetablelookslikethis:
emp_id
emp_name
emp_address
emp_dept
101
Rick
Delhi
D001
101
Rick
Delhi
D002
123
Maggie
Agra
D890
166
Glenn
Chennai
D900
166
Glenn
Chennai
D004
Theabovetableisnotnormalized.Wewillseetheproblemsthatwefacewhenatableisnotnormalized.
Updateanomaly:IntheabovetablewehavetworowsforemployeeRickashebelongstotwodepartmentsof
thecompany.IfwewanttoupdatetheaddressofRickthenwehavetoupdatethesameintworowsorthedata
willbecomeinconsistent.Ifsomehow,thecorrectaddressgetsupdatedinonedepartmentbutnotinotherthen
asperthedatabase,Rickwouldbehavingtwodifferentaddresses,whichisnotcorrectandwouldleadto
inconsistentdata.
Insertanomaly:Supposeanewemployeejoinsthecompany,whoisundertrainingandcurrentlynotassigned
toanydepartmentthenwewouldnotbeabletoinsertthedataintothetableifemp_deptfielddoesntallownulls.
Deleteanomaly:Suppose,ifatapointoftimethecompanyclosesthedepartmentD890thendeletingtherows
thatarehavingemp_deptasD890wouldalsodeletetheinformationofemployeeMaggiesincesheisassigned
onlytothisdepartment.
Toovercometheseanomaliesweneedtonormalizethedata.Inthenextsectionwewilldiscussabout
normalization.
Normalization
Herearethemostcommonlyusednormalforms:
Firstnormalform(1NF)
Secondnormalform(2NF)
Thirdnormalform(3NF)
Boyce&Coddnormalform(BCNF)
Firstnormalform(1NF)
Aspertheruleoffirstnormalform,anattribute(column)ofatablecannotholdmultiplevalues.Itshouldhold
onlyatomicvalues.
Example:Supposeacompanywantstostorethenamesandcontactdetailsofitsemployees.Itcreatesatable
thatlookslikethis:
emp_id
emp_name
emp_address
emp_mobile
101
Herschel
NewDelhi
8912312390
8812121212
102
Jon
Kanpur
103
Ron
Chennai
9900012222
7778881212
9990000123
104
Lester
Bangalore
8123450987
Twoemployees(Jon&Lester)arehavingtwomobilenumberssothecompanystoredtheminthesamefieldas
youcanseeinthetableabove.
Thistableisnotin1NFastherulesayseachattributeofatablemusthaveatomic(single)values,the
emp_mobilevaluesforemployeesJon&Lesterviolatesthatrule.
Tomakethetablecomplieswith1NFweshouldhavethedatalikethis:
emp_id
emp_name
emp_address
emp_mobile
101
Herschel
NewDelhi
8912312390
102
Jon
Kanpur
8812121212
102
Jon
Kanpur
9900012222
103
Ron
Chennai
7778881212
104
Lester
Bangalore
9990000123
104
Lester
Bangalore
8123450987
Secondnormalform(2NF)
Atableissaidtobein2NFifboththefollowingconditionshold:
Tableisin1NF(Firstnormalform)
Nononprimeattributeisdependentonthepropersubsetofanycandidatekeyoftable.
Anattributethatisnotpartofanycandidatekeyisknownasnonprimeattribute.
Example:Supposeaschoolwantstostorethedataofteachersandthesubjectstheyteach.Theycreateatable
thatlookslikethis:Sinceateachercanteachmorethanonesubjects,thetablecanhavemultiplerowsfora
sameteacher.
teacher_id
subject
teacher_age
111
Maths
38
111
Physics
38
222
Biology
38
333
Physics
40
333
Chemistry
40
CandidateKeys:{teacher_id,subject}
Nonprimeattribute:teacher_age
Thetableisin1NFbecauseeachattributehasatomicvalues.However,itisnotin2NFbecausenonprime
attributeteacher_ageisdependentonteacher_idalonewhichisapropersubsetofcandidatekey.Thisviolates
therulefor2NFastherulesaysnononprimeattributeisdependentonthepropersubsetofanycandidatekey
ofthetable.
Tomakethetablecomplieswith2NFwecanbreakitintwotableslikethis:
teacher_detailstable:
teacher_id
teacher_age
111
38
222
38
333
40
teacher_subjecttable:
teacher_id
subject
111
Maths
111
Physics
222
Biology
333
Physics
333
Chemistry
NowthetablescomplywithSecondnormalform(2NF).
ThirdNormalform(3NF)
Atabledesignissaidtobein3NFifboththefollowingconditionshold:
Tablemustbein2NF
Transitivefunctionaldependencyofnonprimeattributeonanysuperkeyshouldberemoved.
Anattributethatisnotpartofanycandidatekeyisknownasnonprimeattribute.
Inotherwords3NFcanbeexplainedlikethis:Atableisin3NFifitisin2NFandforeachfunctionaldependency
X>Yatleastoneofthefollowingconditionshold:
Xisasuperkeyoftable
Yisaprimeattributeoftable
Anattributethatisapartofoneofthecandidatekeysisknownasprimeattribute.
Example:Supposeacompanywantstostorethecompleteaddressofeachemployee,theycreateatable
namedemployee_detailsthatlookslikethis:
emp_id
emp_name
emp_zip
emp_state
emp_city
emp_district
1001
John
282005
UP
Agra
DayalBagh
1002
Ajeet
222008
TN
Chennai
MCity
1006
Lora
282007
TN
Chennai
Urrapakkam
1101
Lilly
292008
UK
Pauri
Bhagwan
1201
Steve
222999
MP
Gwalior
Ratan
Superkeys:{emp_id},{emp_id,emp_name},{emp_id,emp_name,emp_zip}soon
CandidateKeys:{emp_id}
Nonprimeattributes:allattributesexceptemp_idarenonprimeastheyarenotpartofanycandidatekeys.
Here,emp_state,emp_city&emp_districtdependentonemp_zip.And,emp_zipisdependentonemp_idthat
makesnonprimeattributes(emp_state,emp_city&emp_district)transitivelydependentonsuperkey(emp_id).
Thisviolatestheruleof3NF.
Tomakethistablecomplieswith3NFwehavetobreakthetableintotwotablestoremovethetransitive
dependency:
employeetable:
emp_id
emp_name
emp_zip
1001
John
282005
1002
Ajeet
222008
1006
Lora
282007
1101
Lilly
292008
1201
Steve
222999
employee_ziptable:
emp_zip
emp_state
emp_city
emp_district
282005
UP
Agra
DayalBagh
222008
TN
Chennai
MCity
282007
TN
Chennai
Urrapakkam
292008
UK
Pauri
Bhagwan
222999
MP
Gwalior
Ratan
BoyceCoddnormalform(BCNF)
Itisanadvanceversionof3NFthatswhyitisalsoreferredas3.5NF.BCNFisstricterthan3NF.Atablecomplies
withBCNFifitisin3NFandforeveryfunctionaldependencyX>Y,Xshouldbethesuperkeyofthetable.
Example:Supposethereisacompanywhereinemployeesworkinmorethanonedepartment.Theystorethe
datalikethis:
emp_id
emp_nationality
emp_dept
dept_type
dept_no_of_emp
1001
Austrian
Productionandplanning
D001
200
1001
Austrian
stores
D001
250
1002
American
designandtechnicalsupport
D134
100
1002
American
Purchasingdepartment
D134
600
Functionaldependenciesinthetableabove:
emp_id>emp_nationality
emp_dept>{dept_type,dept_no_of_emp}
Candidatekey:{emp_id,emp_dept}
ThetableisnotinBCNFasneitheremp_idnoremp_deptalonearekeys.
TomakethetablecomplywithBCNFwecanbreakthetableinthreetableslikethis:
emp_nationalitytable:
emp_id
emp_nationality
1001
Austrian
1002
American
emp_depttable:
emp_dept
dept_type
dept_no_of_emp
Productionandplanning
D001
200
stores
D001
250
designandtechnicalsupport
D134
100
Purchasingdepartment
D134
600
emp_dept_mappingtable:
emp_id
emp_dept
1001
Productionandplanning
1001
stores
1002
designandtechnicalsupport
1002
Purchasingdepartment
Functionaldependencies:
emp_id>emp_nationality
emp_dept>{dept_type,dept_no_of_emp}
Candidatekeys:
Forfirsttable:emp_id
Forsecondtable:emp_dept
Forthirdtable:{emp_id,emp_dept}
ThisisnowinBCNFasinboththefunctionaldependenciesleftsidepartisakey.