Databases A Beginner's Guide
Databases:
ABeginner’sGuide About the Author
AndrewJ.(Andy)OppelisaproudgraduateofTheBoys’LatinSchoolofMarylandandof
TransylvaniaUniversity(Lexington,Kentucky)whereheearnedaBAincomputerscience in1974.Sincethen,hehasbeencontinuouslyemployedinawidevarietyofinformation technologypositions,includingprogrammer,programmer/analyst,systemsarchitect,project manager,seniordatabaseadministrator,databasegroupmanager,consultant,database designer,datamodeler,anddataarchitect.Inaddition,hehasservedasapart-timeinstructor withtheUniversityofCalifornia,Berkeley,Extensionformorethan20yearsandreceived theHonoredInstructorAwardfortheyear2000.Histeachingworkincludeddevelopingthree coursesforUCExtension,“ConceptsofDatabaseManagementSystems,”“Introductionto RelationalDatabaseManagementSystems,”and“DataModelingandDatabaseDesign.”He alsoearnedhisOracle9iDatabaseAssociatecertificationin2003.Heiscurrentlyemployed asaseniordatamodelerforBlueShieldofCalifornia.Inadditiontocomputersystems,Andy enjoysmusic(guitarandvocals),amateurradio(PacificDivisionViceDirector,American RadioRelayLeague),andsoccer(RefereeInstructor,U.S.Soccer).
Andyhasdesignedandimplementedhundredsofdatabasesforawiderangeof applications,includingmedicalresearch,banking,insurance,apparelmanufacturing, telecommunications,wirelesscommunications,andhumanresources.Heistheauthor ofDatabasesDemystified(McGraw-HillProfessional,2004)andSQLDemystified (McGraw-HillProfessional,2005),andisco-authorofSQL:ABeginner’sGuide (McGraw-HillProfessional,2009).HisdatabaseproductexperienceincludesIMS,DB2, SybaseASE,MicrosoftSQLServer,MicrosoftAccess,MySQL,andOracle(versions7, 8,8i,9i,and10g).
Ifyouhaveanycomments,pleasecontactAndyatandy@andyoppel.com.
About the Technical Editor
ToddMeisterhasbeendevelopingusingMicrosofttechnologiesformorethantenyears.
He’sbeenaTechnicalEditoronmorethan50bookswithtopicsrangingfromSQLServer tothe.NETFramework.Inadditiontotechnicalediting,heservesasanAssistantDirector forComputingServicesatBallStateUniversityinMuncie,Indiana.Helivesincentral Indianawithhiswife,Kimberly,andtheirfourincrediblechildren.ContactToddat tmeister@sycamoresolutions.com.
Databases:
ABeginner’sGuide
AndrewJ.Oppel
NewYorkChicagoSanFrancisco LisbonLondonMadridMexicoCity MilanNewDelhiSanJuan SeoulSingaporeSydneyToronto
ISBN: 978-0-07-160847-3
Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in
a database or retrieval system, without the prior written permission of the publisher.Copyright © 2009 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States
trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-160846-6, MHID: 0-07-160846-X.
MHID: 0-07-160847-8McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in cor-
infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of
porate training programs. To contact a representativ TERMS OF USE such information.or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of
human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy,
McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work
derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without
store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create
is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and
to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to
PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your
ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD-
USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
RANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WAR-
damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in con-
damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such
stances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar
from. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circum-
you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting there-
requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to
tract, tort or otherwise.
v
Contents
vi Databases:ABeginner’sGuide
Contents vii
viii Databases:ABeginner’sGuide
Contents ix
x Databases:ABeginner’sGuide
Acknowledgments
anypeoplewereinvolvedinthedevelopmentofDatabases:ABeginner’sGuide— manyofwhomIdonotknowbyname.First,theeditorsandstaffatMcGraw-Hill
M
provideduntoldhoursofsupportforthisproject.IwishtoespeciallythankEditorial DirectorWendyRinaldiastheindividualwhohasprovidedthemostadviceandinspiration throughoutthedevelopmentofallmybooks.Infact,itwasWendywhogotmestartedasa McGraw-Hillauthor.IalsowishtothankLisaTheobaldforherexcellentcopyeditingand alltheothereditors,proofreaders,indexers,designers,illustrators,andotherparticipants. MyspecialthanksgotoToddMeister,thetechnicaleditor,forhisattentiontodetailand hishelpfulinputsthroughouttheeditingprocess.Finally,mythankstomyfamilyfortheir supportandunderstandingasIfitthewritingscheduleintoanalreadyoverlybusylife.
xi
This page intentionally left blank
Introduction
hirty-fiveyearsago,databaseswerefoundonlyinspecialresearchlaboratories, wherecomputerscientistsstruggledwithwaystomakethemefficientanduseful,
T
publishingtheirfindingsincountlessresearchpapers.Todaydatabasesareaubiquitous partoftheinformationtechnology(IT)industryandbusinessingeneral.Wedirectlyand indirectlyusedatabaseseveryday—bankingtransactions,travelreservations,employment relationships,websitesearches,onlineandofflinepurchases,andmostothertransactions arerecordedinandservedbydatabases.
Asisthecasewithmanyfast-growingtechnologies,industrystandardshavelagged behindinthedevelopmentofdatabasetechnology,resultinginmyriadcommercial products,eachfollowingaparticularsoftwarevendor’svision.Moreover,anumber ofdifferentdatabasemodelshaveemerged,withtherelationalmodelbeingthemost prevalent.Databases:ABeginner’sGuideexaminesallofthemajordatabasemodels, includinghierarchical,network,relational,object-oriented,andobject-relational.This bookconcentratesheavilyontherelationalandobject-relationalmodels,however, becausethesearethemainstreamoftheITindustryandwilllikelyremainsointhe foreseeablefuture.
Themostsignificantchallengeinimplementingadatabaseiscorrectlydesigningthe structureofthedatabase.Withoutathoroughunderstandingoftheproblemthedatabaseis intendedtosolve,andwithoutknowledgeofthebestpracticesfororganizingtherequired data,theimplementeddatabasebecomesanunwieldybeastthatrequiresconstantattention.
xiii
xiv Databases:ABeginner’sGuide Databases:ABeginner’sGuide focusesonthetransformationofrequirementsintoa
workingdatamodelwithspecialemphasisonaprocesscallednormalization,which hasproventobeaneffectivetechniquefordesigningrelationaldatabases.Infact, normalizationcanbeappliedsuccessfullytootherdatabasemodels.And,inkeeping withthenotionthatyoucannotdesignanautomobileifyouhaveneverdrivenone,the StructuredQueryLanguage(SQL)isintroducedsothatthereadermay“drive”adatabase beforedelvingintothedetailsofdesigningone.
I’vedrawnonmyextensiveexperienceasadatabasedesigner,administrator,and instructortoprovideyouwiththisself-helpguidetothefascinatingandcomplexworld ofdatabasetechnology.ExamplesareincludedusingbothMicrosoftAccessandOracle. Publiclyavailablesampledatabasessuppliedbythesevendors(theMicrosoftAccess NorthwinddatabaseandtheOracleHumanResourcesdatabaseschema)areusedinexample figureswheneverpossiblesothatyoucantrytheexamplesdirectlyonyourowncomputer system.Aselftestisprovidedattheendofeachchaptertohelpreinforceyourlearning.
Who Should Read This Book Databases:ABeginner’sGuide isrecommendedforanyonetryingtobuildafoundation
indatabasedesignandmanagement,whetherforpersonalorprofessionaluse.Thebook isdesignedspecificallyforthosewhoareneworrelativelynewtodatabasetechnology; however,thoseofyouwhoneedarefresherinnormalizationanddatabasedesign andmanagementwillalsofindthisbookbeneficial.Whetheryou’reanexperienced developer,you’vehadsomedevelopmentexperience,you’readatabaseadministrator,or you’renewtoprogramminganddatabases,Databases:ABeginner’sGuideprovidesa strongfoundationthatwillbeusefultoanyofyouwantingtolearnmoreaboutdatabase technology.Infact,anyofthefollowingindividualswillfindthisbookhelpfulwhen tryingtounderstandandusedatabases:
●
ThenovicenewtodatabasedesignandSQLprogramming
●
Theanalystormanagerwhowantsabetterunderstandingofhowtodesign, implement,andaccessdatabases
●
Thedatabaseadministratorwhowantstolearnmoreaboutdatabasedesign
●
Thetechnicalsupportprofessionalortesting/QAengineerwhomustperformadhoc queriesagainstSQLdatabases
●
Thewebdeveloperwritingapplicationsthatrequiredatabasesfordatapersistence
Introduction xv
●
Thethird-generationlanguage(3GL)programmerembeddingSQLwithinan application’ssourcecode
●
AnyotherindividualwhowantstolearnhowtodesigndatabasesandwriteSQLcode tocreateandaccessdatabaseswithinanRDBMS Nomatterwhichcategoryyoufitinto,youmustrememberthatthebookisgeared towardanyonewantingtolearnstandarddatabasedesigntechniquesthatworkonany database,notonespecificvendor’sproduct.Thisletsyouapplytheskillsyoulearnin thisbooktoreal-worldsituations,withoutbeinglimitedtoproductstandards.Youwill, ofcourse,stillneedtobeawareofhowtheproductyouworkonimplementsdatabases, particularlydialectsofSQL,butwiththefoundationprovidedinthesepages,you’ll beabletomovefromoneRDBMStothenextandstillhaveasolidunderstandingof databasedesigntheory.Asaresult,you’llfindthatthisbookisausefultooltoanyone newtodatabases,particularlyrelationaldatabases,regardlessoftheproductused.You willeasilybeabletoadaptyourknowledgetothespecificRDBMS.
What the Book Covers Databases:ABeginner’sGuide isdividedintothreeparts.PartIintroducesyoutobasic
databaseconceptsandexplainshowtocreateandaccessobjectswithinyourdatabase usingSQL.PartIIprovidesyouwithafoundationindatabasedevelopment,including thedatabaselifecycle,logicaldesignusingthenormalizationprocess,transformingthe logicaldesignintoaphysicaldatabase,anddataandprocessmodeling.PartIIIfocuses ondatabaseimplementationwithemphasisondatabasesecurity,aswellastheadvanced topicsofdatabasesforonlineanalyticalprocessing(OLAP)andintegratingobjectsand
XMLdocumentsintothedatabase,allowingyoutoexpandonwhatyoulearnedinPartsI andII.Inadditiontothethreeparts,Databases:ABeginner’sGuidecontainsappendices thatincludeanswerstotheself-testquestionsandsolutionstotheTryThisexercisesthat appearthroughoutthebook.
Content Description
Thefollowingoutlinedescribesthecontentsofthebookandshowshowthebookis brokendownintotask-focusedchapters:
Part I: Database Concepts PartIintroducesyoutobasicdatabaseconceptsandexplainshowtocreateandaccess objectswithinyourdatabaseusingSQL. Thischapterintroducesfundamentalconcepts Chapter1:DatabaseFundamentals
anddefinitionsregardingdatabases,includingpropertiescommontodatabases,prevalent
xvi Databases:ABeginner’sGuide
databasemodels,abriefhistoryofdatabases,andtherationaleforfocusingonthe relationalmodel.
Thischapterexplores
Chapter2:ExploringRelationalDatabaseComponents
theconceptual,logical,andphysicalcomponentsthatmakeuptherelationalmodel.
Conceptualdatabasedesign involvesstudyingandmodelingthedatainatechnology-
independentmanner.Logicaldatabasedesignistheprocessoftranslating,ormapping, theconceptualdesignintoalogicaldesignthatfitsthechosendatabasemodel(relational, object-oriented,object-relational,andsoon).Thefinaldesignstepisphysicaldatabase
design, whichinvolvesmappingthelogicaldesigntooneormorephysicaldesigns—each
tailoredtotheparticularDBMSthatwillmanagethedatabaseandtheparticularcomputer systemonwhichthedatabasewillrun.
Thischapterprovidesanoverview
Chapter3:Forms-basedDatabaseQueries
offormingandrunningdatabasequeriesusingtheforms-basedquerytoolinMicrosoft Access,providingafoundationindatabasequeryconceptsforthedatabasedesigntheory thatfollowsinlaterchapters.
ThischapterintroducesSQL,whichhasbecome
Chapter4:IntroductiontoSQL
theuniversallanguageforrelationaldatabasesthatnearlyeveryDBMSinmodernuse supports.Thereasonforitswideacceptanceisclearlythetimeandeffortthatwentinto thedevelopmentoflanguagefeaturesandstandards,makingSQLhighlyportableacross differentRDBMSproducts.
Part II: Database Development PartIIprovidesyouwithafoundationindatabasedevelopment,includingthedatabase
lifecycle,logicaldesignusingthenormalizationprocess,transformingthelogicaldesign intoaphysicaldatabase,anddataandprocessmodeling.
Thischapterintroducestheframeworkinwhich
Chapter5:TheDatabaseLifeCycle
databasedesigntakesplace,ausefulprecursortotheparticularsofdatabasedesign.The
lifecycle ofadatabase(orcomputersystem)isthetermweuseforalltheeventsthattake
placebetweenthetimewefirstrecognizetheneedforadatabase,continuingthroughits developmentanddeployment,andfinallyendingwiththedayitisretiredfromservice.
Inthischapter,youwilllearn
Chapter6:DatabaseDesignUsingNormalization
howtoperformlogicaldatabasedesignusingaprocesscallednormalization.Intermsof understandingrelationaldatabasetechnology,thisisthemostimportanttopicinthisbook, becausenormalizationteachesyouhowbesttoorganizeyourdataintotables.
Inthischapter,wewilllookatentity-
Chapter7:DataandProcessModeling
relationshipdiagrams(ERDs)anddatamodelinginmoredetail.Thesecondpartofthe chapterincludesahigh-levelsurveyofprocessdesignconceptsanddiagrammingtechniques.
Introduction xvii
Chapter8:PhysicalDatabaseDesign Thischapterfocusesonthedatabase
designer’sphysicaldesignwork,whichistransformingthelogicaldatabasedesigninto oneormorephysicaldatabasedesigns.
Part III: Database Implementation PartIIIfocusesondatabaseimplementationwithemphasisondatabasesecurityaswellas
theadvancedtopicsofdatabasesforonlineanalyticalprocessing(OLAP)andintegrating objectsandExtensibleMarkupLanguage(XML)documentsintothedatabase;thisallows youtoexpandonwhatyoulearnedinPartsIandII.
Chapter9:ConnectingDatabasestotheOutsideWorld Thischapterbeginswith
alookattheevolutionofdatabasedeploymentmodels,meaningthewaysthatdatabases havebeenconnectedwiththedatabaseusersandtheothercomputersystemswithinthe enterprisecomputinginfrastructure(theinternalstructurethatorganizesallthecomputing resourcesofanenterprise,includingdatabases,applications,computerhardware,and thenetwork).Thechapterthenexploresthemethodsusedtoconnectdatabasesto applicationsthatuseawebbrowserastheprimaryuserinterface,whichisthewaymany modernapplicationsystemsareconstructed.Itconcludeswithalookatcurrentmethods forconnectingdatabasestoapplications,namelyusingODBCconnections(formost programminglanguages)andvariousmethodsforconnectingdatabasestoapplications writteninJava(acommonlyusedobject-orientedlanguage).
Chapter10:DatabaseSecurity Thischapterpresentstheneedforsecurity,the
securityconsiderationsfordeployingdatabaseserversandclientsthataccessthose servers,andmethodsforimplementingdatabaseaccesssecurity,concludingwitha discussionofsecuritymonitoringandauditing.
Chapter11:DeployingDatabases Thischaptercoverssomeconsiderations
regardingthedevelopmentofapplicationsthatusethedatabasesystem.Theseinclude cursorprocessing,transactionmanagement,performancetuning,andchangecontrol.
Chapter12:DatabasesforOnlineAnalyticalProcessing Thischapterpresentsthe
conceptsofdatabasesforanalyticalprocessing,includingdatawarehousesanddatamarts, anoverviewofdataminingandotherdataanalysistechniques,alongwiththedesign variationsrequiredforthesetypesofdatabases.
Chapter13:IntegratingXMLDocumentsandObjectsintoDatabases This chapterexploresanumberofwaystointegrateXMLandobjectcontentintodatabases.
Part IV: Appendices TheappendicesincludeanswerstotheSelfTestquestionsandsolutionstotheTryThis exercisesthatappearthroughoutthebook.
xviii Databases:ABeginner’sGuide AppendixA:AnswerstoSelfTests ThisappendixprovidestheanswerstotheSelf Testquestionslistedattheendofeachchapter. AppendixB:SolutionstotheTryThisExercises Thisappendixcontainssolutions,
includingdiagramsandapplicableSQLcode,fortheTryThisexercisesthatappearin nearlyeverychapterofthebook.
Chapter Content
Asyoucanseefromtheoutline,Databases:ABeginner’sGuideisorganizedinto chapters.Eachchapterfocusesonasetofkeyskillsandconceptsandcontainsthe backgroundinformationyouneedtounderstandtheconcepts,plustheskillsrequired toapplytheseconcepts.Eachchaptercontainsadditionalelementstohelpyoubetter understandtheinformationcoveredinthatchapter:
Ask the Expert
EachchaptercontainsoneortwoAsktheExpertsectionsthatprovideinformationon questionsthatmightariseregardingtheinformationpresentedinthechapter.
Self Test
EachchapterendswithaSelfTest,asetofquestionsthattestyouontheinformationand skillsyoulearnedinthatchapter.TheanswerstotheSelfTestsareincludedinAppendixA.
Try This Exercises
MostchapterscontainoneortwoTryThisexercisesthatallowyoutoapplytheinformation thatyoulearnedinthechapter.Eachexerciseisbrokendownintostepsthatwalkyou throughtheprocessofcompletingaparticulartask.Whereapplicable,theexercisesinclude
relatedfilesthatyoucandownloadfromourwebsiteat Click ComputingandthenclicktheDownloadsSectionlinkontheleftsideofthepage.Onthe downloadspage,scrolldowntothelistingforthisbookandselectthefilesyouwishto download.ThefilesusuallyincludetheSQLstatementsordiagramsusedwithintheTry Thisexercise.
TocompletemanyoftheTryThisexercisesinthisbook,you’llneedtohaveaccessto anRDBMSthatallowsyoutoenterandexecuteSQLstatementsinteractively.Ifyou’re accessinganRDBMSoveranetwork,checkwiththedatabaseadministratortomakesure thatyou’relogginginwiththecredentialsnecessarytocreateadatabaseandschema.You mightneedspecialpermissionstocreatetheseobjects.Alsoverifywhetheryoushould includeanyparticularparameterswhencreatingthedatabase(forexample,logfilesize), andwhetherrestrictionsonthenamesyoucanuseorotherrestrictionsapply.Besureto checktheappropriatedocumentationbeforeworkingwithanydatabaseproduct. Part
I
Database ConceptsThis page intentionally left blank Chapter
1 Database Fundamentals
3
4 Databases:ABeginner’sGuide KeySkills&Concepts
●
PropertiesofaDatabase
●
PrevalentDatabaseModels
●
ABriefHistoryofDatabases
● WhyFocusonRelational?
hischapterintroducesfundamentalconceptsanddefinitionsregardingdatabases, includingpropertiescommontodatabases,prevalentdatabasemodels,abriefhistoryof
T databases,andtherationaleforfocusingontherelationalmodel.
Properties of a Database
Adatabaseisacollectionofinterrelateddataitemsthataremanagedasasingleunit.This definitionisdeliberatelybroadbecausesomuchvarietyexistsacrossthevarioussoftware vendorsthatprovidedatabasesystems.Forexample,MicrosoftAccessplacestheentire databaseinasingledatafile,soanAccessdatabasecanbedefinedasthefilethatcontains thedataitems.OracleCorporationdefinesitsdatabaseasacollectionofphysicalfilesthat aremanagedbyaninstanceofitsdatabasesoftwareproduct.Aninstanceisacopyofthe databasesoftwarerunninginmemory.MicrosoftSQLServerandSybaseAdaptiveServer Enterprise(ASE)defineadatabaseasacollectionofdataitemsthathaveacommon owner,andmultipledatabasesaretypicallymanagedbyasingleinstanceofthedatabase managementsoftware.Thiscanallbequiteconfusingifyouworkwithmultipleproducts, because,forexample,adatabaseasdefinedbyMicrosoftSQLServerorSybaseASEis exactlywhatOracleCorporationcallsaschema.
Adatabaseobjectisanameddatastructurethatisstoredinadatabase.Thespecific typesofdatabaseobjectssupportedinadatabasevaryfromvendortovendorandfrom onedatabasemodeltoanother.Databasemodelreferstothewayinwhichadatabase organizesitsdatatopatterntherealworld.Themostcommondatabasemodelsare presentedinthe“PrevalentDatabaseModels”sectionlaterinthischapter.
Afileisacollectionofrelatedrecordsthatarestoredasasingleunitbyanoperating system.Giventheunfortunatelysimilardefinitionsoffilesanddatabases,howcanwe
Chapter1: DatabaseFundamentals
5
makeadistinction?AnumberofUnixoperatingsystemvendorscalltheirpasswordfiles “databases,”yetdatabaseexpertswillquicklypointoutthat,infact,thesearenotactually databases.Clearly,weneedabitmorerigorinourdefinitions.Theanswerliesinan understandingofcertaincharacteristicsorpropertiesthatdatabasespossesswhicharenot foundinordinaryfiles,includingthefollowing:
●
Managementbyadatabasemanagementsystem(DBMS)
●
Layersofdataabstraction
●
Physicaldataindependence
●
Logicaldataindependence Thesepropertiesarediscussedinthefollowingsubsections.
The Database Management System
Thedatabasemanagementsystem(DBMS)issoftwareprovidedbythedatabasevendor. SoftwareproductssuchasMicrosoftAccess,Oracle,MicrosoftSQLServer,SybaseASE, DB2,Ingres,andMySQLareallDBMSs.IfitseemsoddtoyouthattheDBMSacronym isusedinsteadofmerelyDMS,rememberthatthetermdatabasewasoriginallywrittenas twowords,andbyconventionhassincebecomeasinglecompoundword.
TheDBMSprovidesallthebasicservicesrequiredtoorganizeandmaintainthe database,includingthefollowing:
● Movesdatatoandfromthephysicaldatafilesasneeded. ●
Managesconcurrentdataaccessbymultipleusers,includingprovisionstoprevent simultaneousupdatesfromconflictingwithoneanother.
●
Managestransactionssothateachtransaction’sdatabasechangesareanall-or-nothing unitofwork.Inotherwords,ifthetransactionsucceeds,alldatabasechangesmadeby itarerecordedinthedatabase;ifthetransactionfails,noneofthechangesitmadeare recordedinthedatabase.
●
Supportsaquerylanguage,whichisasystemofcommandsthatadatabaseuser employstoretrievedatafromthedatabase.
● Providesprovisionsforbackingupthedatabaseandrecoveringfromfailures. ● Providessecuritymechanismstopreventunauthorizeddataaccessandmodification.
6 Databases:ABeginner’sGuide Ask the Expert
I’veheardtheterm“databank”used.Whatisthedifferencebetweenadatabankand Q: adatabase?
Adatabankandadatabasearethesamething.Databankismerelyanoldertermthatwas
A:
usedbythescientistswhodevelopedearlydatabasesystems.Infact,thetermdatabankis stillusedinafewhumanlanguages,suchasbancodedadosinPortuguese.
Layers of Data Abstraction
Databasesareuniqueintheirabilitytopresentmultipleuserswiththeirowndistinctviews ofthedatawhilestoringtheunderlyingdataonlyonce.Thesearecollectivelycalleduser
views .Auserinthiscontextisanypersonorapplicationthatsignsontothedatabasefor
thepurposeofstoringand/orretrievingdata.Anapplicationisasetofcomputerprograms designedtosolveaparticularbusinessproblem,suchasanorder-entrysystem,apayroll- processingsystem,oranaccountingsystem.
WhenanelectronicspreadsheetapplicationsuchasMicrosoftExcelisused,all usersmustshareacommonviewofthedata,andthatviewmustmatchthewaythe dataisphysicallystoredintheunderlyingdatafile.Ifauserhidessomecolumnsin aspreadsheet,reorderstherows,andsavesthespreadsheet,thenextuserwhoopens thespreadsheetwillviewthedatainthemannerinwhichthefirstusersavedit.An alternative,ofcourse,isforeachusertosavehisorherowncopyinseparatephysical files,butthenasoneuserappliesupdates,theotherusers’databecomesoutofdate. Databasesystemspresenteachuseraviewofthesamedata,buttheviewscanbetailored totheneedsoftheindividualusers,eventhoughtheyallcomefromonecommonlystored copyofthedata.Becauseviewsstorenoactualdata,theyautomaticallyreflectanydata changesmadetotheunderlyingdatabaseobjects.Thisisallpossiblethroughlayersof
,whichisshowninFigure1-1.
abstraction
ThearchitectureshowninFigure1-1wasfirstdevelopedbyANSI/SPARC(American NationalStandardsInstitute/StandardsPlanningandRequirementsCommittee)in the1970sandquicklybecameafoundationformuchofthedatabaseresearchand developmenteffortsthatfollowed.MostmodernDBMSsfollowthisarchitecture,which iscomposedofthreeprimarylayers:thephysicallayer,thelogicallayer,andtheexternal layer.Theoriginalarchitectureincludedaconceptuallayer,whichhasbeenomittedhere becausenoneofthemoderndatabasevendorsimplementit.
Chapter1: DatabaseFundamentals
7 External Layer View 1 View 2 View 3 Logical Data Independence
Internal Schema
Logical Layer(Logical Schema)
Physical DataIndependence Physical Layer
File File File File File
Database Database Database Database DatabaseFigure1-1 Database layers of abstraction
The Physical Layer
Thephysicallayercontainsthedatafilesthatholdallthedataforthedatabase.Nearlyall modernDBMSsallowthedatabasetobestoredinmultipledatafiles,whichareusually spreadoutovermultiplephysicaldiskdrives.Withthisarrangement,thediskdrivescan workinparallelformaximumperformance.AnotableexceptionamongtheDBMSsused asexamplesinthisbookisMicrosoftAccess,whichstorestheentiredatabaseinasingle physicalfile.Whilesimplifyingdatabaseuseonasingle-userpersonalcomputersystem, thisarrangementlimitstheabilityoftheDBMStoscaletoaccommodatemanyconcurrent usersofthedatabase,makingitinappropriateasasolutionforlargeenterprisesystems. Inallfairness,MicrosoftAccesswasnotdesignedtobearobustenterpriseclassDBMS.I haveincludeditindiscussionsinthisbooknotbecauseitcompeteswithproductssuchas OracleandSQLServer,butbecauseit’sagreatexampleofapersonalDBMSwithauser interfacethatmakeslearningdatabaseconceptseasyandfun.
Thedatabaseuserdoesnotneedtounderstandhowthedataisactuallystoredwithin thedatafilesorevenwhichfilecontainsthedataitem(s)ofinterest.Inmostorganizations, atechnicianknownasadatabaseadministrator(DBA)handlesthedetailsofinstalling andconfiguringthedatabasesoftwareanddatafilesandmakingthedatabaseavailableto users.TheDBMSworkswiththecomputer’soperatingsystemtomanagethedatafiles automatically,includingallfileopening,closing,reading,andwritingoperations.Thedatabase
8 Databases:ABeginner’sGuide
usershouldnotberequiredtorefertophysicaldatafileswhenusingadatabase,whichis insharpcontrastwithspreadsheetsandwordprocessing,wheretheusermustconsciously savethedocument(s)andchoosefilenamesandstoragelocations.Manyofthepersonal computer–basedDBMSsareexceptionstothistenetbecausetheuserisrequiredtolocate andopenaphysicalfileaspartoftheprocessofsigningontotheDBMS.Conversely, withenterpriseclassDBMSs(suchasOracle,SybaseASE,MicrosoftSQLServer,and MySQL),thephysicalfilesaremanagedautomaticallyandthedatabaseuserneverneedsto refertothemwhenusingthedatabase.
The Logical Layer
Thelogicallayerorlogicalmodelcomprisesthefirstoftwolayersofabstractioninthe database:thephysicallayerhasaconcreteexistenceintheoperatingsystemfiles,whereas thelogicallayerexistsonlyasabstractdatastructuresassembledfromthephysicallayeras needed.TheDBMStransformsthedatainthedatafilesintoacommonstructure.Thislayer issometimescalledtheschema,atermusedforthecollectionofallthedataitemsstoredin aparticulardatabaseorbelongingtoaparticulardatabaseuser.Dependingontheparticular DBMS,thislayercancontainasetoftwo-dimensionaltables,ahierarchicalstructure similartoacompany’sorganizationchart,orsomeotherstructure.The“PrevalentDatabase Models”sectionlaterinthischapterdescribesthepossiblestructuresinmoredetail.
The External Layer
Theexternallayerorexternalmodelisthesecondlayerofabstractioninthedatabase. Thislayeriscomposedoftheuserviewsdiscussedearlier,whicharecollectively calledthesubschema.Inthislayer,thedatabaseusers(applicationprogramsaswell asindividuals)thataccessthedatabaseconnectandissuequeriesagainstthedatabase. Ideally,onlytheDBAdealswiththephysicalandlogicallayers.TheDBMShandlesthe transformationofselecteditemsfromoneormoredatastructuresinthelogicallayer toformeachuserview.Theuserviewsinthislayercanbepredefinedandstoredinthe databaseforreuse,ortheycanbetemporaryitemsthatarebuiltbytheDBMStoholdthe resultsofasingleadhocdatabasequeryuntiltheyarenolongerneededbythedatabase user.Anadhocqueryisaquerythatisnotpreconceivedandthatisnotlikelytobe reused.ViewsarediscussedinmoredetailinChapter2.
Physical Data Independence
Theabilitytoalterthephysicalfilestructureofadatabasewithoutdisruptingexisting usersandprocessesisknownasphysicaldataindependence.AsshowninFigure1-1,the separationofthephysicallayerfromthelogicallayerprovidesphysicaldataindependence
Chapter1: DatabaseFundamentals
9