Professional Documents
Culture Documents
Database Management System
Database Management System
Whatisadatabase?
ADatabaseisalogical,consistent,andorganizedcollectionofdatathat
caneasilybea
ccessed,managedandupdated.C RUD
Databasemostlyc onsistsoftheobjects(tables),andtablesincludethe
recordsandfields.
Fieldsaretheb
asicunitsofdatastorage,whichcontaininformationabout
aparticularaspectorattributeoftheentitydescribedbythedatabase.
DBMSisusedfortheextractionofdatafromthedatabaseintheformof
queries.
Whatisadatabasesystem?
Thec ollectionofdatabaseandDBMSsoftwaretogetherisknownasa
databasesystem.Throughthedatabasesystem,wecanperformmany
activitiessuchas-
Thedatacanbestoredinthed
atabasewithease,andtherearen
o
issuesofdataredundancyanddatainconsistency.
DatabaseManagementSystem
Adatabasemanagements ystemissoftwarethatisusedtomanagethe
database.
DBMSprovidesaninterfacet operformvariousoperationslike
databasecreation,storingdatainit,updatingdata,creatingatablein
thedatabase,andalotmore.
Itprovidesp
rotectionandsecuritytothedatabase.
Inthecaseofm
ultipleusers,italsomaintainsdatac
onsistency.
Forexample,M ySQL,Oracle,etcareaverypopularcommercialdatabase
whichisusedindifferentapplications.
AdvantagesofDBMS
Controlsdatabaseredundancy:Itcancontroldataredundancybecause
itstoresallthedatainonesingledatabasefileandthatrecordeddatais
placedinthedatabase.
Datasharing:InDBMS,theauthorizedusersofanorganizationcanshare
thedataamongmultipleusers.
EasilyMaintenance:Itcanbeeasilymaintainableduetothecentralized
natureofthedatabasesystem.
Reducetime:Itreducesdevelopmenttimeandmaintenanceneeds.
Backup:Itprovidesbackupandrecoverysubsystemsthatcreatean
automaticbackupofdatafromhardwareandsoftwarefailuresandrestores
thedataifrequired.
multipleuserinterfaces:Itprovidesdifferenttypesofuserinterfaceslike
graphicaluserinterfaces,applicationprograminterfaces
Redundancycontrol
Restrictionforunauthorizedaccess
Providesmultipleuserinterfaces
Providesbackupandrecovery
Enforcesintegrityconstraints
Ensuredataconsistency
Easyaccessibility
Easydataextractionanddataprocessingduetotheuseofqueries
DisadvantagesofDBMS
CostofHardwareandSoftware:Itrequiresahigh-speeddataprocessor
andlargememorysizetorunDBMSsoftware.
Size:Itoccupiesalargespaceofdisksandlargememorytorunthem
efficiently.
Complexity:Databasesystemcreatesadditionalcomplexityand
requirements.
Higherimpactoffailure:Failurehashighlyimpactedthedatabase
because,inmostoftheorganization,allthedataisstoredinasingle
databaseandifthedatabaseisdamagedduetoelectricfailureordatabase
corruptionthenthedatamaybelostforever.
WhatistheE-Rmodel?
E-RmodelisashortnamefortheE ntity-Relationshipmodel.Thismodelis
basedonther ealworld.Itcontainsn ecessaryobjects(knownasentities)
andtherelationshipamongtheseobjects.
it'saconceptualmodelthatgivestheg raphicalrepresentationofthe
logicalstructureofthedatabase.
AnERdiagramismainlycomposedofthefollowingthreecomponents-
EntitySets,A ttributes,andR elationshipSet.
IntheE-Rdiagram,e ntitiesarerepresentedbyr ectangles,relationshipsare
representedbydiamonds,a ttributesarethecharacteristicsofentitiesand
representedbye llipses,andd ataflowisrepresentedthroughastraight
line.
WhatisanEntity?
TheE
ntityisasetofattributesinadatabase.Ane
ntitycanbeareal-world
objectwhichp hysicallyexistsinthisworld.
WhatisanEntitySet?
Anentitysetisasetofthes
ametypeofentities.
For example, a set of people, a set of students, a set of
companies,etc.
●N -aryRelationshipSet-N-aryrelationshipsetisarelationshipset
where‘n’entitysetsparticipateinarelationshipset.
TherearethreetypesofrelationshipsinDBMS-
One-To-One:Hereo nerecordofanyobjectcanberelatedtoonerecordo
f
anotherobject.
Ex:-Indiancitizenandaadharno.
One-To-Many(many-to-one):Hereo nerecordofanyobjectcanberelated
tomanyrecordsofotherobjectsandviceversa.
Ex:-Indiancitizenandmobileno.
Many-to-many:Herem orethanonerecordofanobjectcanberelatedton
numberofrecordsofanotherobject.
Ex:Teacherandstudent
CardinalityConstraint
Cardinalityconstraintdefinesth emaximumnumberofrelationshipinstances
inwhichanentitycanparticipate.
●One-to-OneCardinality- AnentityinsetAcanbeassociatedwithatmost
oneentityinsetB.AnentityinsetBcanbeassociatedwithatmostoneentityin
setA.
●O ne-to-ManyCardinality-AnentityinsetAcanbeassociatedwithany
number(zeroormore)ofentitiesinsetB.AnentityinsetBcanbeassociated
withatmostoneentityinsetA.
●M any-to-OneCardinality- AnentityinsetAcanbeassociatedwithatmost
oneentityinsetB.AnentityinsetBcanbeassociatedwithanynumberof
entitiesinsetA.
●M any-to-ManyCardinality-AnentityinsetAcanbeassociatedwithany
number(zeroormore)ofentitiesinsetB.AnentityinsetBcanbeassociated
withanynumber(zeroormore)ofentitiesinsetA.
Attributes
Attributesarethed
escriptivepropertiesthatareo wnedbyeachentity
ofanEntitySet.
TypesofAttributes:
●S impleAttributes-Simpleattributesarethoseattributest hatcannot
bedividedfurther.Ex.Age
●C ompositeAttributes-Compositeattributesaret hoseattributesthat
arecomposedofmanyothersimpleattributes.Ex.Name,Address
●M ulti-ValuedAttributes-Multi-valuedattributesarethoseattributest hat
cantakemorethanonevalueforagivenentityfromanentityset.Ex.
MobileNo,EmailID
●D erivedAttributes-Derivedattributesarethosea ttributesthatcanbe
derivedfromotherattributes(s).Ex.AgecanbederivedfromDOB.
●K eyAttributes-Keyattributesarethoseattributest hatcanidentifyan
entityuniquelyinanentityset.E
x.RollNo.
Constraints
Relationalconstraintsarether estrictionsimposedonthedatabase
contentsandoperations.Theyensurethecorrectnessofdatainthe
database.
●D
omainConstraint-Domainconstraintdefinesthedomainorsetofvalues
foranattribute.Itspecifiesthatthevaluetakenbytheattributemustbethe
atomicvaluefromitsdomain.
●T
upleUniquenessConstraint-T upleUniquenessconstraintspecifiesthat
allthetuplesmustbenecessarilyuniqueinanyrelation.
●K
eyConstraint-Allt hevaluesoftheprimarykeymustbeunique.The
valueoftheprimarykeymustnotbenull.
●E
ntityIntegrityConstraint-Entityintegrityconstraintspecifiesthatno
attributeoftheprimarykeymustcontainanullvalueinanyrelation.
●R
eferentialIntegrityConstraint-Itspecifiesthatallthevaluestakenbythe
foreignkeymusteitherbeavailableinrelationoftheprimarykeyorbenull.
Key
Ak
eyisasetofattributesthatcanidentifyeachtupleuniquelyinthe
givenrelation.
SuperKey-Asuperkeyisas etofattributesthatcanidentifyeach
tupleuniquelyinthegivenrelation.Asuperkeym
ayconsistofany
numberofattributes.
CandidateKey-As etofminimalattribute(s)thatcanidentifyeach
tupleuniquelyinthegivenrelationiscalledacandidatekey.
Primarykey
Aprimarykeyisac andidatekeythatthed
atabasedesignerselectswhile
designingthedatabase
Aprimarykeyisafieldorthecombinationoffieldsthatu
niquelyidentifyeach
recordinthetable.
PrimaryKeysareu niqueandN OTNULL
AlternateKey
Candidatekeysthatareleftu
nimplementedorunuseda
fterimplementingthe
primarykeyarecalledalternatekeys.
CompositeKey
Ap
rimarykeycomposedofmultipleattributesandn
otjustasingle
attributeiscalledacompositekey.
UniqueKey
Itisu
niqueforalltherecordsinthetable.Onceassigned,itsvaluecannotbe
changedi.e.itisnon-updatable.Itm ayhaveaNULLvalue.
Foreignkey
Theforeignkeyisusedtolinkoneormoretablestogether.Itisalsoknownas
ther eferencingkey.
Aforeignkeyiss pecifiedasakeythatisrelatedtotheprimarykeyof
anothertable.Itmeansaf oreignkeyfieldinonetablereferstotheprimary
keyfieldoftheothertable.
Itidentifieseachrowofanothertableu niquelythatmaintainsther eferential
integrity.
Ana ttribute‘X’iscalledaforeignkeytosomeotherattribute‘Y’whenits
valuesaredependentonthevaluesofattribute‘Y’.
Therelationinwhichattribute‘Y’ispresentiscalledt hereferencedrelation.
Therelationinwhichattribute‘X’ispresentiscalledt hereferencingrelation.
FunctionalDependency:
Inanyrelation,afunctionaldependencyα→βholdsif-T
wotupleshaving
thesamevalueofattributeαalsohavethesamevalueforattributeβ.
TypesofFunctionalDependency:
●T rivialFunctionalDependencies–AfunctionaldependencyX →Yissaid
tobetrivialifandonlyifY⊆ X.oThus,ifR HSofafunctionaldependencyis
as ubsetofLHS,thenitiscalledatrivialfunctionaldependency.
●N on-TrivialFunctionalDependencies–AfunctionaldependencyX→Yis
saidtobenon-trivialifandonlyifY⊄ X.oThus,ifthereexistsatleastone
attributeintheRHSofafunctionaldependencythatisn otapartofLHS,thenit
iscalledanon-trivialfunctionaldependency.
DecompositionofaRelation:
Thep
rocessofbreakingupordividingasinglerelationintotwoormore
subrelationsiscalledthed
ecompositionofarelation.
PropertiesofDecomposition:
●L
osslessD ecomposition-Losslessdecompositionensuresn o
informationislostfromtheoriginalrelationduringdecomposition.
Whenthes ubrelationsarejoinedback,thesamerelationisobtained
thatwasdecomposed.
●D
ependencyPreservation-Dependencypreservationensures None
ofthefunctionaldependenciesthatholdontheoriginalrelationarelost.o
Thesubr elationsstillholdorsatisfythefunctionaldependenciesof
theoriginalrelation
TypesofDecomposition:
●L osslessJoinDecomposition: ConsiderthereisarelationRwhich
isdecomposedintosubrelationsR1,R2,….,Rn.Thisdecompositioniscalled
losslessjoindecompositionw henthejoinofthesubrelationsresultsinthe
samerelationRthatwasdecomposed.
●L
ossyJoinDecomposition:ConsiderthereisarelationRwhichis
decomposedintosubrelationsR1,R2,….,Rn.
Thisdecompositioniscalledlossyjoindecompositionw henthejoinofthesub
relationsdoesnotresultinthesamerelationRthatwasdecomposed.
Whatisnormalization?
Normalizationisthep
rocessofminimizingredundancyfromarelation
orsetofrelations.Redundancyinrelationmaycauseinsertion,
deletion,andupdateanomalies.So,ithelpstominimizetheredundancy
inrelations.Normalformsareusedtoeliminateorreduceredundancyin
databasetables.
InDBMS,databasenormalizationisaprocessofmakingthedatabase
consistentby-
●Reducingther edundancies
●Ensuringtheintegrityofdatathroughlosslessdecomposition
Normalizationisconsideredanessentialprocessasitisusedtoavoid
dataredundancy,insertionanomaly,updateanomaly,deletion
anomaly.
Theirmostcommonlyusednormalformsare:
FirstNormalForm(1NF)
SecondNormalForm(2NF)
ThirdNormalForm(3NF)
Boyce&CoddNormalForm(BCNF)
Whatis1NF?
Arelationisinfirstnormalformifitdoesnotcontainany
compositeormulti-valuedattribute.Arelationisinitsfirst
normalformifeveryattributeinthatrelationisasingle
valuedattribute.
Createseparatetablesforeachgroupofrelateddataandidentifyeach
rowwithauniquecolumn
Whatis2NF?
Tobeinthesecondnormalform,arelationmustbeinthefirst
normalformandtherelationmustnotcontainanypartial
dependency.Arelationisin2NFifithasN
oPartialDependency,i.e.,
nonon-primeattribute(attributeswhicharenotpartofany
candidatekey)isdependentonanypropersubsetofanycandidate
keyofthetable.
PartialDependency-Ifthep
ropersubsetofcandidatekeys
determinesnon-primeattributes,itiscalledpartialdependency.
Whatis3NF?
Arelationisinthethirdnormalformifthereisnotransitivedependency
fornon-primeattributesaswellasitisinthesecondnormalform.
Arelationisin3NFifatleasto
neofthefollowingconditionsholdsin
everynon-trivialfunctionaldependencyX-->Y
Xisasuperkey.
Yisaprimeattribute(eachelementofYispartofsomecandidatekey).
A→Biscalledat ransitivedependencyifandonlyif-A
isnotasuper
keyandB isanon-primeattribute.
Transitivedependency-IfA->BandB->CaretwoFDsthenA->Cis
calledtransitivedependency.
WhatisBCNF?
ArelationRisinB
CNFifRisinT hirdNormalFormandforeveryFD,
LHSissuperkey.A
relationisinB
CNFiffineverynon-trivialfunctional
dependencyX-->Y,
Xisasuperkey
BCMFstandsforBoyce-CoddNormalForm.Itisanadvancedversionof
3NF,soitisalsoreferredtoas3.5NF.BCNFisstricterthan3NF.
AtablecomplieswithBCNFifitsatisfiesthefollowingconditions:
Itisin3
NF.
ForeveryfunctionaldependencyX->Y,X shouldbethesuperkeyofthe
table.ItmerelymeansthatXcannotbeanon-primeattributeifYisaprime
attribute
WhatisDenormalization?
Denormalizationistheprocessofb oostingupdatabaseperformance
anda
ddingredundantdatawhichhelpstogetridofcomplexdata.
Denormalizationisapartofthed
atabaseoptimizationtechnique.This
processisusedtoavoidtheu seofcomplexandcostlyjoins.
Denormalizationdoesn'trefertothethoughtofnotnormalizing;instead,
denormalizationt akesplaceafternormalization.
Transaction
Thetransactionisas
inglelogicalunitofworkformedbyasetof
hichaccessesandpossiblymodifiesthecontentsofa
operations. w
database.Transactionsaccessdatausingreadandwriteoperations.
OperationsinTransaction
●R eadOperation-Read(A)instructionwillreadthevalueof‘A’fromthe
databaseandwillstoreitinthebufferinthemainmemory.
●WriteOperation– Write(A)willwritetheupdatedvalueof‘A’fromthe
buffertothedatabase.
●A ctiveState–
● Thisisthefirststateinthelifecycleofatransaction.
● Atransactioniscalledinana ctivestateaslongasitsinstructions
areexecuted.
● Allthechangesmadebythetransactionnowarestoredinthebuffer
inthemainmemory.
●P artiallyC ommittedState–
● Afterthelastinstructionofthetransactionhasbeenexecuted,it
entersintoapartiallycommittedstate.
● Afterenteringthisstate,thetransactionisconsideredtobepartially
committed.oItisnotconsideredfullycommittedbecauseallthe
changesmadebythetransactionarestillstoredinthebufferinthe
mainmemory.
●CommittedState–
● Afterallthec hangesmadebythetransactionhavebeen
successfullystoredinthedatabase,itentersintoacommitted
state.
● Now,thetransactionisconsideredtobefullycommitted.
●FailedState–
● W henatransactionisg
ettingexecutedintheactivestateor
partiallycommittedstateandsomefailureoccursduetowhichit
becomesimpossibletocontinuetheexecution,itentersintoafailed
state.
●A bortedState–
● Afterthetransactionhasfailedandenteredintoafailedstate,allthe
changesmadebyithavetobeundone.
● Toundothechangesmadebythetransaction,itbecomesnecessary
torollbackthetransaction.
● Afterthet ransactionhasrolledbackcompletely,itentersintoan
abortedstate.
●T
erminatedState–
● Thisisthelaststateinthelifecycleofatransaction.
● Aftere nteringthec ommittedstateorabortedstate,the
transactionfinallyentersintoaterminatedstatewhereitslife
cyclefinallycomestoanend.
ExplainACIDproperties
ACIDpropertiesaresomebasicrules,whichhavetobes atisfiedbyevery
transactiontopreserveintegrity.Thesepropertiesandrulesare:
ATOMICITY:a llornothingrule.
● Thispropertyensuresthateitherthetransactiono ccurscompletely
oritdoesnotoccuratall.
● Inotherwords,itensuresthatn otransactionoccurspartially.
CONSISTENCY:Thispropertyreferstotheu niformityofthedata.
● Thispropertyensuresthatintegrityconstraintsaremaintained.
● Inotherwords,itensuresthatthed atabaseremainsconsistent
beforeandafterthetransaction.
ISOLATION:
● Thispropertyensuresthatm ultipletransactionscanoccur
simultaneouslywithoutc ausinganyinconsistency.
● Theresultantstateofthesystema fterexecutingallthe
transactionsisthesameasthestatethatwouldbeachievedifthe
transactionsweree xecutedseriallyoneaftertheother.
DURABILITY:
● Thispropertyensuresthata llthechangesmadebyatransaction
afteritssuccessfulexecutionarewrittens uccessfullytothedisk.
● Italsoensuresthatthesec hangesexistpermanentlyandare
neverlostevenifthereoccursafailureofanykind.
Schedules:
Theo
rderinwhichtheo
perationsofmultipletransactionsappearfor
executioniscalledaschedule.
SerialSchedules:
● A llthetransactionse
xecuteseriallyoneaftertheother.
● Wheno netransactionexecutes,n oothertransactionisallowed
toexecute.
● Serialschedulesarealways-C onsistent,Recoverable,
Cascadeless,andStrict
Non-SerialSchedules
● M ultipletransactionsexecuteconcurrently.
● Operationsofallthetransactionsareinterleavedormixedwith
eachother.
● Non-serialschedulesaren otalways-Consistent,Recoverable,
Cascadeless,andStrict.
Serializability
●Somenon-serialschedulesmayleadtoinconsistencyofthedatabase.
●Serializabilityisaconceptthathelpstoidentifywhichnon-serial
schedulesarecorrectandwillm aintaintheconsistencyofthe
database.
SerializableSchedules–
● Ifagivenn on-serialscheduleof‘n’transactionsisequivalentto
someserialscheduleof‘n’transactions,thenitiscalleda
serializableschedule.
● Serializableschedulesarealways-C onsistent,Recoverable,
Cascadeless,andStrict.
TypesofSerializability–
●C onflictSerializability-Ifagivenn on-serialschedulecanbe
convertedintoaserialschedulebyswappingitsn on-conflicting
operations,thenitiscalledac
onflictserializableschedule.
●V iewSerializability-Ifagivenscheduleisfoundt obeviewedas
equivalenttosomeserialschedule,thenitiscalledaviewserializable
schedule.
Non-SerializableSchedules–
●Anon-serialschedulet hatisnotserializableiscalledanon-serializable
schedule.
●Anon-serializablescheduleisn otguaranteedtoproducethesame
effectasproducedbyaserialscheduleonanyconsistentdatabase.
●Non-serializableschedules-mayormaynotbeconsistent,mayormay
notberecoverable.
IrrecoverableSchedules–
Ifinaschedule,
● Atransactionperformsadirtyreadoperationfroman
uncommittedtransaction
● Andifitc
ommitsbeforethetransactionfromwhichithasread
thevalue,thensuchascheduleisknownasanIrrecoverable
Schedule.
RecoverableSchedules–
Ifinaschedule,
● Atransactionperformsad irtyreadoperationfromanuncommitted
transaction
● Itsc ommitoperationisdelayeduntiltheuncommitted
transactioneithercommitsorrollsback,thensuchascheduleis
knownasaRecoverableSchedule.
TypesofRecoverableSchedules–
●C ascadingS chedule-Ifinaschedule,failureofonetransaction
causesseveralotherdependenttransactionstorollbackorabort,then
suchascheduleiscalledaC ascadingScheduleorCascadingRollback
orCascadingAbort.
●C ascadelessSchedule-Ifinaschedule,a transactionisnot
allowedtoreadadataitemuntilthelasttransactionthathaswrittenit
iscommittedoraborted,thensuchascheduleiscalledaCascadeless
Schedule.
●S trictSchedule-Ifinaschedule,a
transactionisneitherallowedto
readnorwriteadataitemuntilthelasttransactionthathasbeen
writteniscommittedoraborted,thensuchascheduleiscalledaStrict
Schedule.
WhatisRDBMS?
RDBMSstandsforR elationalDatabaseManagementSystems.
Itisbasedonther elationalmodel.
Itisusedtomaintainthed atarecordsandindicesintables.
Arelationaldatabaseisthem ostcommonlyuseddatabase.Itcontainsa
numberoftablesandeachtablehasitsownprimarykey.
Duetoacollectionofano rganizedsetoftables,datacanbeaccessed
easilyinRDBMS.
RDBMSisthesystemthatenablesyoutoperformdifferentoperationssuch
asu pdate,insert,delete,manipulateandadministerarelational
databasewithminimaldifficulties.
MostofthetimeRDBMSusesS QLlanguagebecauseitiseasily
understandableandisusedoften.
Relationaldatabaseshavethefollowingproperties:
Valuesareatomic.
Allofthevaluesinacolumnhavethesamedatatype.
Eachrowisunique.
Thesequenceofcolumnsisinsignificant.
Thesequenceofrowsisinsignificant.
Eachcolumnhasauniquename.
Integrityconstraintsmaintaindataconsistencyacrossmultipletables.
WhatisdataabstractioninDBMS?
DataAbstractionisap
rocessofhidingunwantedorirrelevantdetails
fromtheend-user.Itprovidesadifferentviewandhelpsinachievingdata
independencewhichisusedtoenhancethesecurityofdata.
Thedatabasesystemsconsistofcomplicateddatastructuresandrelations.
Foruserstoaccessthedataeasily,thesecomplicationsarekepthidden,
andonlytherelevantpartofthedatabaseismadeaccessibletotheusers
throughdataabstraction.
Forexample,Weknowthatmostoftheuserspreferthosesystemswhich
haveasimpleGUIthatmeansnocomplexprocessing.So,tokeepthe
usertunedandformakingaccesstothedataeasy,itisnecessarytodo
dataabstraction.Inaddition,dataabstractiondividesthesysteminto
differentlayerstomaketheworkspecifiedandwelldefined
therearethreelevelsofabstractionforDBMS,whichareasfollows−
PhysicalorInternalLevel
LogicalorConceptualLevel
VieworExternalLevel
PhysicalorInternalLevel
ItisthelowestlevelofabstractionforDBMSwhichdefineshowthedatais
actuallystored,itdefinesdatastructurestostoredataandaccessmethods
usedbythedatabase.
Actually,itisdecidedbydevelopersordatabaseapplicationprogrammers
howtostorethedatainthedatabase.
So,overall,theentiredatabaseisdescribedatthephysicalorinternal
level.Itisaverycomplexleveltounderstand.
Forexample,customerinformationisstoredintables,anddataisstoredin
theformofblocksofstoragesuchasbytes,gigabytes,etc.
Whatis2-Tierarchitecture?
The2-Tierarchitectureisthesameasbasicclient-server.Inthetwo-tier
architecture,a pplicationsontheclientendcandirectlycommunicatewith
thed
atabaseattheserver-side.
Whatisthe3-Tierarchitecture?
The3-Tierarchitecturecontainsanotherlayerbetweentheclientand
server.Theintroductionof3-tierarchitectureisfortheeaseoftheusersas
itprovidestheGUI,whichmakesthesystemsecureandmuchmore
accessible.
Inthisarchitecture,theapplicationonthec
lient-endinteractswithan
applicationontheserverwhichfurthercommunicateswitht hedatabase
system.
WhatisFunctionalDependency?
FunctionalDependencyisthestartingpointofnormalization.
Itexistswhenarelationbetweentwoattributesallowsyoutodeterminethe
correspondingattribute'svalueuniquely.
Thefunctionaldependencyisalsoknownasdatabasedependencyandis
definedastherelationshipwhichoccurswhenoneattributeinarelation
uniquelydeterminesanotherattribute.
ItiswrittenasA->BwhichmeansBisfunctionallydependentonA.
RelationalAlgebra:
RelationalAlgebraisap
roceduralquerylanguagethattakesarelation
asaninputandg
eneratesarelationasanoutput.
BTrees
Ateverylevel,wehaveK
eyandD
ataPointerandd
atapointerpointstoeither
blockorrecord.
PropertiesofB-Trees:
RootsofaB-treecanhavec
hildrenbetween2andP,wherePistheOrderof
thetree.
Orderoftree–M
aximumnumberofchildrenanodecanhave.
Aninternalnodecanhavec hildrenbetween⌈ P/2⌉ andP
Aninternalnodecanhavekeysbetween⌈ P/2⌉ –1andP-1
B+Trees
InB+trees,thestructureofleafandnon-leafaredifferent,sotheirorderis.
Orderofn on-leafwillbehigherascomparedtoleafnodes.
Searchingt imewillbelessinB+treessincet heydon'thaverecordpointers
innon-leafb ecauseofwhichd epthwilldecrease.
Indexing:
Indexingisawaytoo
ptimizetheperformanceofadatabaseby
minimizingthenumberofdiskaccessesrequiredwhenaqueryis
processed.Itisadatastructuretechniquet hatisusedtoquickly
locateandaccessthedatainadatabase.
Indexesarecreatedusingafewdatabasecolumns.
● ThefirstcolumnistheS
earchkeythatcontainsac opyoftheprimarykeyor
candidatekeyofthetable.Thesevaluesarestoredinsortedordersothatthe
correspondingdatacanbeaccessedquickly.
Note:Thedatamayormaynotbestoredinsortedorder.
● ThesecondcolumnistheD
ataReferenceorP
ointerw
hichcontainsasetof
pointersholdingtheaddressofthediskblockwherethatparticularkeyvalue
canbefound.
FileStructures:
●PrimaryIndex:Aprimaryindexisa norderedfile,withrecordsoffixed
lengthwithtwofields.Thefirstfieldisthesamea stheprimarykeyasa
datafileandthesecondfieldisapointertothedatablock,wherethekeyis
available.
Theaveragenumberofblockaccessesusingindex=log2Bi+1,whereBi
=numberofindexblocks.
●C lusteringIndex:Aclusteringindexiscreatedonadatafilewhose
recordsarep hysicallyorderedonanon-keyfield(calledtheClustering
field).
●S econdaryIndex:Secondaryindexprovidessecondarymeansof
accessingafileforwhichprimaryaccessalreadyexists.
SQL
WhatisSQL
SQLisastandardlanguagefors
toring,manipulating,andretrieving
dataindatabases.
SQLstandsfortheS tructuredQueryLanguage.
Itisthestandardlanguageusedtom
aintaintherelationaldatabaseand
performmanydifferentdatamanipulationoperationsonthedata.
SQLwasinitiallyinventedin1970.
Itisadatabaselanguageusedford
atabasecreation,deletion,fetching
andmodifyingrows,etc.sometimes,itispronouncedas'sequel.'Wecan
alsouseittohandleorganizeddatacomposedofentities(variables)and
relationsbetweendifferententitiesofthedata.
WhataretheusagesofSQL?
SQLisresponsibleformaintainingt herelationaldataandthedata
structurespresentinthedatabase.Someofthecommonusagesare
givenbelow:
Toexecutequeriesagainstadatabase
Toretrievedatafromadatabase
Toinsertsrecordsinadatabase
Toupdaterecordsinadatabase
Todeleterecordsfromadatabase
Tocreatenewdatabases
Tocreatenewtablesinadatabase
Tocreateviewsinadatabase
Toperformcomplexoperationsonthedatabase
Whataretablesandfieldsinthedatabase?
Atableisasetoforganizeddataintheformofrowsandcolumns.
Itenablesuserstostoreanddisplayrecordsinthestructureformat.Itis
similartoworksheetsinthespreadsheetapplication.Here,rowsreferto
thetuples,representingthesimpledataitem,andc olumnsarethe
attributeofthedataitemspresentinaparticularrow.
Columnscanbecategorizedasvertical,andRowsarehorizontal.
Fieldsarethecomponentstoprovidethestructureforthetable.
Itstoresthesamecategoryofdatainthesamedatatype.
Atablecontainsafixednumberofcolumnsbutcanhaveanynumberof
rowsknownastherecord.Itisalsocalledacolumninthetableofthe
database.
Itrepresentstheattributeorcharacteristicsoftheentityintherecord.
Howmanytypesofdatabaselanguagesarethere?
DataDefinitionLanguage(DDL)
Datadefinitionlanguage(DDL)isacomputerlanguageusedtoc
reateand
modifythestructureofdatabaseobjectsinadatabase.Thesedatabase
objectsincludeviews,schemas,tables,indexes,etc.
SQLcommandsthatcanbeusedtodefinethedatabaseschema.
e.g.,CREATE,ALTER,DROP,TRUNCATE,RENAME,etc.
●C REATE-tocreateadatabaseanditsobjectslike(table,index,views,
storeprocedure,function,andtriggers)
●A LTER-altersthestructureoftheexistingdatabase
●D ROP-deleteobjectsfromthedatabase
●T RUNCATE-removeallrecordsfromatable,includingallspaces
allocatedfortherecordsareremoved
●R ENAME-renameanobject
DQL(DataQueryLanguage):
DQLstatementsareusedforp
erformingqueriesonthedatawithin
schemaobjects.ThepurposeoftheDQLCommandistogetsome
schemarelationbasedonthequerypassedtoit.
ExampleofDQL:
● SELECT–isusedtoretrievedatafromthedatabase.
DataManipulationLanguage(DML)
Thesecommandsareusedforthem anipulationofalreadyupdateddata
Thismanipulationinvolvesinsertingdataintodatabasetables,r etrieving
existingdata,d eletingdatafromexistingtables,andm
odifyingexisting
data.
DMLismostlyincorporatedinSQLdatabases
e.g.,SELECT,UPDATE,INSERT,DELETE,etc.
●S ELECT-retrievedatafromadatabase
●INSERT-insertdataintoatable
●U PDATE-updatesexistingdatawithinatable
●D ELETE-Deleteallrecordsfromadatabasetable
●M ERGE-UPSERToperation(insertorupdate)
DataControlLanguage(DCL)
DCLisusedtoc
ontroluseraccessinadatabase.
Thiscommandisrelatedtos ecurityissues.
UsingtheDCLcommand,a llowsorrestrictstheuserfromaccessingdata
inthedatabaseschema.
e.g.,GRANTandREVOKE
●G RANT-allowusersa
ccessprivilegestothed
atabase
●R EVOKE-w ithdrawusersaccessprivilegesgivenb yusingthe
GRANTc ommand
TransactionC
ontrolLanguage(TCL)
TransactionControlLanguage(TCL)commandsareu sedtomanage
transactionsinthedatabase
Theseareusedtom anagethechangesmadetothedatainatableby
DMLstatements.
e.g.,COMMIT,ROLLBACK,andSAVEPOINT.
●C OMMIT-commitsaTransaction
●R OLLBACK- rollbackatransactionincaseofanyerroroccurs
●S AVEPOINT-torollbackthetransactionmakingpointswithingroups
NestedQuery:Innestedqueries,a queryiswritteninsideaquery.
Theresultoftheinnerqueryisusedintheexecutionoftheouterquery.
Independent
Correlated
SELECT:
TheSELECTstatementisusedtos electdatafromadatabase.
Syntax-
●SELECTcolumn1,column2,...FROMtable_name;
●Here,column1,column2,...arethefieldnamesofthetableyouwanttoselectdata
from.
Ifyouwanttoselectallthefieldsavailableinthetable,usethefollowingsyntax:
●SELECT*FROMtable_name;
Ex–
●SELECTCustomerName,CityFROMCustomers;
SELECTDISTINCT:
TheSELECTDISTINCTstatementisusedtoreturnonlydistinct(different)values.
●SELECTDISTINCTCountryFROMCustomers;
WHERE:
TheWHEREclauseisusedtof ilterrecords.
Ex–●SELECT*FROMCustomersWHERECountry='Mexico';
AND,ORandNOT:
TheWHEREclausecanbecombinedwithAND,OR,andNOToperators.
●TheANDoperatordisplaysarecordifalltheconditionsseparatedbyANDareTRUE.
●TheORoperatordisplaysarecordifanyoftheconditionsseparatedbyORisTRUE.
TheNOToperatordisplaysarecordifthecondition(s)isNOTTRUE
ORDERBY:
TheORDERBYkeywordisusedtosorttheresult-setinascendingordescending
order.
TheORDERBYkeywordsortstherecordsinascendingorderbydefault.
Tosorttherecordsindescendingorder,usetheDESCkeyword.
Ex–
●SELECT*FROMCustomersORDERBYCountry;
●SELECT*FROMCustomersORDERBYCountryASC,CustomerNameDESC;
INSERTINTO:
TheINSERTINTOstatementisusedtoin
sertnewrecordsinatable.
Ex–
●INSERTINTOCustomers(CustomerName,ContactName,Address,City,PostalCode,
Country)
VALUES('Cardinal','TomB.Erichsen','Skagen21','Stavanger','4006','Norway');
NULLValue:
ItisnotpossibletotestforNULLvalueswithcomparisonoperators,suchas=,<,or<>.
WewillhavetousetheISNULLandISNOTNULLoperatorsinstead.
●SELECTcolumn_names
FROMtable_name
WHEREcolumn_nameISNULL;
●SELECTcolumn_names
FROMtable_name
WHEREcolumn_nameISNOTNULL;
UPDATE:
TheUPDATEstatementisusedtomodifythee xistingrecordsinatable.
●UPDATECustomers
SETContactName='AlfredSchmidt', City='Frankfurt'
WHERECustomerID=1;
DELETE:
TheDELETEstatementisusedtod
eleteexistingrecordsinatable.
●DELETEFROMCustomersWHERECustomerName='AlfredsFutterkiste';
SELECTTOP:
TheSELECTTOPclauseisusedtospecifythenumberofrecordstoreturn.
●SELECTTOP3*FROMCustomers;
●SELECT*FROMCustomersLIMIT3;
●SELECT*FROMCustomersFETCHFIRST3ROWSONLY;
AggregateFunctions:
MIN():
TheMIN()functionreturnsthes
mallestvalueoftheselectedcolumn.
●SELECTMIN(column_name)FROMtable_nameWHEREcondition;
MAX():
TheMAX()functionreturnsthelargestvalueoftheselectedcolumn.
●SELECTMAX(column_name)FROMtable_nameWHEREcondition;
COUNT():
TheCOUNT()functionreturnsthen umberofrowsthatmatchesaspecified
criterion.
●SELECTCOUNT(ProductID)FROMProducts;
AVG():
TheAVG()functionreturnsthea veragevalueofanumericcolumn
●SELECTAVG(Price)FROMProducts;
SUM():
TheSUM()functionreturnsthet otalsumofanumericcolumn.
●SELECTSUM(Quantity)FROMOrderDetails;
LIKEOperator:
TheLIKEoperatorisusedinaWHEREclausetos earchforaspecified
patterninacolumn.Therearetwowildcardsoftenusedinconjunctionwiththe
LIKEoperator:
●Thep
ercentsign(%)representszero,one,ormultiplecharacters.
●Theu
nderscoresign(_)r epresentsone,singlecharacter.
IN:
TheINoperatora llowsyoutospecifymultiplevaluesinaWHERE
clause.TheINoperatorisashorthandform
ultipleORconditions.
Ex–
●SELECT*FROMCustomersWHERECountryIN('Germany','France',
'UK');
BETWEEN:
TheBETWEENoperatorselectsvalueswithinagivenrange.
Thevaluescanbenumbers,text,ordates.
TheBETWEENoperatorisinclusive:b eginandendvaluesareincluded
UNION:
TheUNIONoperatorisusedtocombinether esult-setoftwoormore
SELECTstatements.
●EverySELECTstatementwithinU NIONmusthavethesamenumberof
columns
●Thecolumnsmustalsohaves
imilardatatypes.
●ThecolumnsineverySELECTstatementmustalsobeinthesameorderThe
UNIONoperatorselectsonlydistinctvaluesbydefault.Toallowduplicatevalues,
useUNIONALL
Ex–
●SELECTCityFROMCustomers
UNION
SELECTCityFROMSuppliersORDERBYCity;
GROUPBY:
GROUPBYstatementg roupsrowsthathavethesamevaluesintosummary
rows,
like"findthenumberofcustomersineachcountry".
TheGROUPBYstatementisoftenusedwithaggregatefunctions
(COUNT(),MAX(),MIN(),SUM(),AVG())togrouptheresult-setbyoneormore.
●SELECTCOUNT(CustomerID),CountryFROMCustomersGROUPBY
CountryORDERBYCOUNT(CustomerID)DESC;
HAVING:
TheHAVINGclausewasaddedtoSQLbecausetheW HEREkeywordcannot
beusedwithaggregatefunctions.* WHEREisgivenpriorityoverHAVING.
●SELECTCOUNT(CustomerID),
CountryFROMCustomers
GROUPBYCountry
HAVINGCOUNT(CustomerID)>5;
CREATEDATABASE:
TheCREATEDATABASEstatementisusedtoc
reateanewSQLdatabase.
Syntax–
●CREATEDATABASEdatabasename;
DROPDATABASE:
TheDROPDATABASEstatementisusedtod
ropanexistingSQLdatabase.
Syntax–
●DROPDATABASEdatabasename;
CREATETABLE:
TheCREATETABLEstatementisusedtoc
reateanewtableinadatabase.
Syntax–
●CREATETABLEtable_name(column1datatype,column2datatype,column3
datatype,....);
DROPTABLE:
TheDROPTABLEstatementisusedtod ropanexistingtableinadatabase.
Syntax–
●DROPTABLEtable_name;
TRUNCATET ABLE:
TheTRUNCATETABLEstatementisusedtodeletethedatainsideatable,but
notthetableitself.Syntax–
●TRUNCATETABLEtable_name;
ALTERTABLE:
TheALTERTABLEstatementisusedt oadd,delete,ormodifycolumnsinan
existingtable.
TheALTERTABLEstatementisa lsousedtoaddanddropvariousconstraints
onanexistingtable.
Ex–
●ALTERTABLECustomersADDEmailvarchar(255);
●ALTERTABLECustomersDROPCOLUMNEmail;
●ALTERTABLEPersonsALTERCOLUMNDateOfBirthyear;
WhatisJoin?
TheJoinoperationisoneofthemostusefulactivitiesinrelational
algebra.Itisthemostcommonlyusedwaytoc ombineinformationfrom
twoormorerelations.
AJOINclauseisusedtoc ombinerowsfromtwoormoretables,based
onarelatedcolumnbetweenthem.
Therearethefollowingtypesofjoin:
Innerjoins:
Innerjoinisof3categories.Theyare:
Thetajoin
Naturaljoin
Equijoin
Outerjoins:Outerjoinhasthreetypes.Theyare:
Leftouterjoin
Rightouterjoin
Fullouterjoin
WhatisINNERJOINinSQL?
TheINNERJOINkeywords electsallrowsfromboththetablesas
longastheconditionsatisfies.Thiskeywordwillcreatethe
result-setbycombiningallrowsfromboththetableswherethe
conditionsatisfiesi.ev
alueofthecommonfieldwillbethesame
SELECT c
olumn_name(s)
FROMt
able1
INNERJ
OINt
able2
ON
t
able1.column_name=
table2.column_name
;
WhatisLeftJoininSQL?
TheLeftJoinisusedt ofetchallrowsfromtheleft-handtableand
commonrecordsbetweenthespecifiedtables.
Itreturnsa lltherowsfromtheleft-handsidetableeventhoughthere
arenomatchesontheright-handsidetable.
Ifitdoesnotfindanymatchingrecordfromtherightsidetable,thenit
returnsnull.ThisjoincanalsobecalledaLeftOuterJoin.
WhatistheRightJOINinSQL?
TheRightjoinisusedtoretrieveallrowsfromtheright-handtableand
onlythoserowsfromtheothertablethatfulfillthejoincondition.
Itreturnsalltherowsfromtheright-handsidetableeventhoughthereare
nomatchesintheleft-handsidetable.
Ifitfindsunmatchedrecordsfromtheleftsidetable,itr eturnsaNull
value.ThisjoinisalsoknownasRightOuterJoin.
Whatisself-joinandwhatistherequirementof
self-joining?
ASELFJOINisusedtojoinatablewithself.Thisjoincanbeperformed
usingtablealiases,whichallowustoavoidrepeatingthesametablename
inasinglesentence.Itwillthrowanerrorifweusethesametablename
morethanonceinasinglequerywithoutusingtablealiases.
ASELFJOINisrequiredwhenwewanttocombinedatawithotherdatain
thesametableitself.Itisoftenveryusefultoconvertahierarchical
structuretoaflatstructure.
ThefollowingsyntaxillustratestheSELFJOIN:
SELECTcolumn_lists
FROMtable1AST1,table1AST2
WHEREjoin_conditions;
HighestSalary
SELECTname,MAX(salary)assalary
FROMemployee
SecondhighestSalary
Usingnested
SELECTname,MAX(salary)ASsalary
FROMemployee
WHEREsalary<(SELECTMAX(salary)
FROMemployee);
SELECTsalary
FROMemployee
ORDERBYsalarydesclimit1,1
ThirdHighestSalary:
SELECTname,MAX(salary)ASsalary
FROMemployee
WHEREsalary<(SELECTMAX(salary)
FROMemployee
WHEREsalary<(SELECTMAX(salary)
FROMemployee) );
● SELECTsalary
FROMemployee
ORDERBYsalarydesclimit2,1
FindNthhighestelement
● SELECTsalary
FROMemployee
ORDERBYsalarydesclimitn-1,1
JOINKCODES
i)
SELECTIF(GRADE<8,NULL,NAME),GRADE,MARKS
FROMSTUDENTSJOINGRADES
WHEREMARKSB ETWEENMIN_MARKANDMAX_MARK
ORDERBYGRADEDESC,NAME
ii)Addingmorethantwotables
//m
oret
hant
wot
ables
selecth
.h
acker_id
,h
.n
ame
froms
ubmissionss
innerj
oinc
hallengesc
ons
.
challenge_id
=
c
.
challenge_id
innerj
oind
ifficultyd
onc
.
difficulty_level
=
d
.d
ifficulty_level
innerj
oinh
ackersh
ons
.
hacker_id
=
h
.h
acker_id
wheres
.s
core
=
d
.s
core
a
ndc
.d
ifficulty_level
=
d
.d
ifficulty_level
groupb
yh
.h
acker_id,h
.n
ame
having
c
ount(s
.h
acker_id)>
1
orderb
yc
ount
(s
.
h
acker_id
)d
esc,s
.h
acker_id
a
sc