Beruflich Dokumente
Kultur Dokumente
DataIntegrity
Whenwearedesigningadatabase,thereislotoffactorstobeconcentratedon.Weneedtomakesurethatall
therequireddatasaredistributedamongrighttablesandthereisnoduplication/missingdata.Thespaceutilised
fortheappropriatelyforthedatabase.Timetakenforeachqueryisminimalandsoon.
ImaginewehaveaSTUDENTtablewithStudentdetailsandthesubjectsthathehasoptedfor.Ifweobservethe
tablebelow,JosephhasoptedfortwosubjectsMathematicsandPhysics.Thatisfine.Butwhatiswrongin
belowtable?Hisaddressisrepeatedeachtime,whichisnotnecessaryandwasteofspace.Thisiscalled
redundancyandisnotallowedinadatabase.
Similarly,ifwehavetoinsertonemorerecordforAllen,thenwehavetoenterallhisdetailsintotheabovetable.
Butwhatistheguaranteethatallhisdetailsareenteredcorrectly?Therecouldbeamistakeandhenceleadingto
mismatchinhisdetails.Butwhowilllatersaywhichentryiscorrect?Noone!HencethedatainDBiswrong.
Sameissuecanhappenwhenweupdatethedata.Ifweupdateaddressoneoftherecord,andleaveotherrecord
forJosephabove,againadatamismatch.
Andwhenwedeleteadata,sayforChris,whoishavingonlyoneentry,wholeofhisinformationislost!
ImaginetherearetwoentitiesEmployeeandDepartment,andtheyarenotproperlyrelatedbymeansofforeign
key.Whatwouldbetheresult?Wecanenterasmanydepartmentaswewanttoanemployeeforwhom
departmentmaynotexistsatallinDepartmenttable!Somappingthetablesappropriatelyisalsoaveryimportant
factor.
Dataintegrityensures,alltheabovementionedissuesarenotinjectedintothedatabasewhileitisdesigned.It
guaranteesthatdatabaseisperfectandcomplete.
TypesofIntegrityConstraints
Therearefivetypesofdataintegrityconstraints
DomainConstraint
Hereeachcolumnsofatableareverifiedsothatcorrectdataisenteredintocolumn.Forexample,numericdata
isenteredintoaNUMBERcolumnandnotanycharacter.InaDATEcolumn,correctdatesareenteredandnot
anyinvalidvalues.
https://www.tutorialcup.com/dbms/integrity.htm 1/3
5/19/2017 DatabaseIntegrity
Imaginewehaveatablewheredatefieldsarestoredascharacterandwehavetocopythisdatefieldintoanew
tablewherethiscolumnisdefinedasDATE.Whathappenshereismostofthedatafromtheoriginaltablewillnot
beloadedintonewtable,asthereismismatchinthedatastored.i.e.theoriginaltablewillhavedatesin22
March2015formatwhichnewtablewillnotacceptasdate.Intheforemostcase,iftherewasdomainintegrity,
originaltablewouldnothavesuchdatesandwouldhavepreservedtheintegrityofdataintheoriginaltableitself.
EntityIntegrity
ThisintegrityensuresthateachrecordinthetableisuniqueandhasprimarykeywhichisnotNULL.Thatmeans,
thereisnoduplicaterecordorinformationofdatainatableandeachrecordsareuniquelyidentifiedbynonnull
attributeofthetable.
InaSTUDENTtable,eachstudentshouldbeadifferentfromotherandtherewillnotbeduplicaterecords.Also,
STUDENT_IDwhichisaprimarykeyinthetablehasnonnullvaluesforeachoftherecord.
ColumnConstraint
Thisconstraintensuresthatthevaluesenteredintoacolumnarecorrectbymeansofbusinessrules.Say,thereis
anagecolumnanditsvalueisnegativewhichisnotcorrect.Thisconstraintrefinesfromenteringwrongage.
Similarexampleofsuchconstraintissalarycannotbenegativeemployeenumberwillbeinagivenrangeetc.
Thesearebusinessrules/requirementsthatspecifywhatkindofvaluescouldbeenteredintoeachcolumn.
Thisconstraintisdifferentfromdomainconstraintashereitchecksforthevalidityofthedatabeingenteredlike
correctageisbeingenteredCorrectEmployeeIdisenteredetc.Inthedomainconstraint,itchecks,whether
correctsetofdatabeingisenteredlikeDateisenteredintodatecolumn,Numberisenteredintonumbercolumn
etc.
UserDefinedIntegrityConstraint
https://www.tutorialcup.com/dbms/integrity.htm 2/3
5/19/2017 DatabaseIntegrity
Imagine,whileenteringasalaryofanemployee,weneedtocheckifhissalaryislessthanhismanager.Though
thisissimilartocolumnconstraint,wecannotdirectinsistthisconstraintonthecolumnasthesystemdoesnot
knowwhohismanageris.Weneedtocheckforhismanager'ssalaryfirst,ifitismorethanhisemployer,thenwe
willinsertthedata.Forthiswemanuallyneedtowritecode.ThiskindofconstraintsiscalledUserDefined
IntegrityConstraint.
ReferentialIntegrity
AswediscussedforEmployeeandDepartmenttables,iftheyarenotmappedcorrectly,therewouldbeadata
mismatch.Itwillallowustoenteradepartmentforanemployeewhichdoesnotexist.Itwillallowustodeletea
departmentforwhichemployeesareworking.Whatwouldbetheresult?Employeeswithoutanydepartmentare
notcorrect.Orupdatinganydepartmentnumberintheemployeetablewillresultinadepartmentwhichdoesnot
existatall.Allthesecaseswillleadtomismatchandinvaliddatainthedatabase.
Hencetoensureaboveallcasesaremet,properrelationshiphastobedefinedbetweentherelatedtablesby
meansofprimaryandforeignkeys.i.e.everyforeignkeyinthetableshouldbeaprimarykeyintherelatedtable.
Inourexample,DEPARTMENT_IDshouldbeaprimarykeyinDepartmenttableanditshouldbeaforeignkeyin
theEmployeetable.Thiswillstopentering/updatingadepartmentwhichdoesnotexists.Itwillnotallowusto
deleteanydepartmentfromtheDepartmenttableforwhichemployeesstillexistsinEmployeetable.
SQLInterviewQuestions
https://www.tutorialcup.com/dbms/integrity.htm 3/3