Tải bản đầy đủ

OReilly SQL and relational theory feb 2009 ISBN 0596523068

SQLandRelationalTheory,1stEdition
byC.J.Date
Publisher:O'ReillyMedia,Inc.
PubDate:February5,2009
PrintISBN-13:978-0-596-52306-0
Pages:432

Overview
UnderstandingSQL'sunderlyingtheoryisthebestwayto
guaranteethatyourSQLcodeiscorrectandyourdatabase
schemaisrobustandmaintainable.Ontheotherhand,if
you'renotwellversedinthetheory,youcanfallintoseveral
traps.InSQLandRelationalTheory,authorC.J.Date
demonstrateshowyoucanapplyrelationaltheorydirectlyto
youruseofSQL.Withnumerousexamplesandclear
explanationsofthereasoningbehindthem,you'lllearnhow
todealwithcommonSQLdilemmas,suchas:
Shoulddatabaseaccessgrantedbethroughviewsinstead
ofbasetables?
Nullsinyourdatabasearecausingyoutogetwrong
answers.Why?Whatcanyoudoaboutit?

CouldyouwriteanSQLquerytofindemployeeswho
haveneverbeeninthesamedepartmentformorethan
sixmonthsatatime?
SQLsupports"quantifiedcomparisons,"butthey'rebetter
avoided.Why?Howdoyouavoidthem?
Constraintsarecruciallyimportant,butmostSQL
productsdon'tsupportthemproperly.Whatcanyoudo
toresolvethissituation?


DatabasetheoryandpracticehaveevolvedsinceEdgarCodd
originallydefinedtherelationalmodelbackin1969.
IndependentofanySQLproducts,SQLandRelationalTheory
drawsondecadesofresearchtopresentthemostup-to-date
treatmentofthematerialavailableanywhere.Anyonewitha
modesttoadvancedbackgroundinSQLwillbenefitfromthe
manyinsightsinthisbook.



Copyright
Copyright©2009,O'ReillyMedia.Allrightsreserved.
PublishedbyO'ReillyMedia,Inc.,1005GravensteinHighway
North,Sebastopol,CA95472.
O'Reillybooksmaybepurchasedforeducational,business,
orsalespromotionaluse.Onlineeditionsarealsoavailable
formosttitles(safari.oreilly.com).Formoreinformation,
contactourcorporate/institutionalsalesdepartment:(800)
998-9938orcorporate@oreilly.com.
TheO'ReillylogoisaregisteredtrademarkofO'ReillyMedia,
Inc.SQLandRelationalTheoryandrelatedtradedressare
trademarksofO'ReillyMedia,Inc.Manyofthedesignations
usedbymanufacturersandsellerstodistinguishtheir
productsareclaimedastrademarks.Wherethose
designationsappearinthisbook,andO'ReillyMedia,Inc.
wasawareofatrademarkclaim,thedesignationshavebeen
printedincapsorinitialcaps.
Whileeveryprecautionhasbeentakeninthepreparationof
thisbook,thepublisherandauthorsassumeno
responsibilityforerrorsoromissions,orfordamages
resultingfromtheuseoftheinformationcontainedherein.



Toallthosewhothinkanexerciselikethisoneis
worthwhile,andinparticulartothememoryofLexde
Haan,whoisverymuchmissed.
Thosewhoareenamoredofpracticewithouttheoryarelike
apilotwhogoesintoashipwithoutrudderorcompassand
neverhasanycertaintywhereheisgoing.Practiceshould
alwaysbebaseduponasoundknowledgeoftheory.
LeonardodaVinci(1452–1519)
Thetroublewithpeopleisnotthattheydon'tknowbutthat
theyknowsomuchthatain'tso.
JoshBillings(1818–1885)
Languagesdie…mathematicalideasdonot.
G.H.Hardy
Unfortunately,thegapbetweentheoryandpracticeisnotas
wideintheoryasitisinpractice.
Anonymous


Preface
SQLisubiquitous.ButSQLishardtouse:It'scomplicated,
confusing,anderrorprone—muchmoreso,Iventureto
suggest,thanitsapologistswouldhaveyoubelieve.Inorder
tohaveanyhopeofwritingSQLcodethatyoucanbesureis
accurate,therefore(meaningitdoesexactlywhatit's
supposedtodo,nomoreandnoless),youmustfollowsome
appropriatediscipline—andit'sthethesisofthisbookthat
usingSQLrelationallyisthedisciplineyouneed.Butwhat
doesthismean?Isn'tSQLrelationalanyway?
Well,it'struethatSQListhestandardlanguageforusewith
relationaldatabases—butthatfactinitselfdoesn'tmakeit
relational.Thesadtruthis,SQLdepartsfromrelational
theoryinalltoomanyways;duplicaterowsandnullsare
twoobviousexamples,butthey'renottheonlyones.Asa
consequence,itgivesyouropetohangyourselfwith,asit
were.Soifyoudon'twanttohangyourself,youneedto
understandrelationaltheory(whatitisandwhy);youneed
toknowaboutSQL'sdeparturesfromthattheory;andyou
needtoknowhowtoavoidtheproblemstheycancause.In
aword,youneedtouseSQLrelationally.Thenyoucan
behaveasifSQLtrulywererelational,andyoucanenjoythe
benefitsofworkingwithwhatis,ineffect,atrulyrelational
system.
Now,abooklikethiswouldn'tbeneededifeveryonewas
usingSQLrelationallyalready—buttheyaren't.Onthe
contrary,IobservemuchbadpracticeincurrentSQLusage.
Ievenobservesuchpracticebeingrecommended,in
textbooksandsimilarpublications,bywriterswhoreally
oughttoknowbetter(nonames,nopackdrill);infact,a
reviewoftheliteratureinthisregardisaprettydispiriting
exercise.Therelationalmodelfirstsawthelightofdayin
1969,andyethereweare,almost40yearson,anditstill
doesn'tseemtobeverywellunderstoodbythedatabase


communityatlarge.Partlyforsuchreasons,thisbookuses
therelationalmodelitselfasanorganizingprinciple;it
explainsvariousfeaturesofthemodelindepth,andshows
ineverycasehowbesttouseSQLtoimplementthefeature
inquestion.

P.1.Prerequisites
Iassumeyou'readatabasepractitionerandtherefore
reasonablyfamiliarwithSQLalready.Tobespecific,I
assumeyouhaveaworkingknowledgeofeithertheSQL
standardor(perhapsmorelikelyinpractice)atleastone
SQLproduct.However,Idon'tassumeyouhaveadeep
knowledgeofrelationaltheoryassuch(thoughIdohope
youunderstandthattherelationalmodelisagoodthingin
general,andadheringtoitwhereverpossibleisadesirable
goal).Inordertoavoidmisunderstandings,therefore,I'llbe
describingvariousfeaturesoftherelationalmodelindetail,
aswellasshowinghowtouseSQLtoconformtothose
features.ButwhatIwon'tdoisattempttojustifyallofthose
features;rather,I'llassumeyou'resufficientlyexperiencedin
databasematterstounderstandwhy,e.g.,thenotionofa
keymakessense,orwhyyousometimesneedtodoajoin,
orwhymanytomanyrelationshipsneedtobesupported.(If
Iweretoincludesuchjustifications,thiswouldbeavery
differentbook—quiteapartfromanythingelse,itwouldbe
muchbiggerthanitalreadyis—andinanycase,thatbook
hasalreadybeenwritten.)
I'vesaidIexpectyoutobereasonablyfamiliarwithSQL.
However,IshouldaddthatI'llbeexplainingcertainaspects
ofSQLindetailanyway—especiallyaspectsthatmightbe
encounteredlessfrequentlyinpractice.(TheSQLnotionof
"possiblynondeterministicexpressions"isacaseinpoint
here.SeeChapter12.)

P.2.DatabaseinDepth


Thisbookisbasedon,andintendedtoreplace,anearlier
onewiththetitleDatabaseinDepth:RelationalTheoryfor
Practitioners(O'Reilly,2005).Myaiminthatearlierbook
wasasfollows(thisisaquotefromthepreface):
Aftermanyyearsworkinginthedatabasecommunityin
variouscapacities,I'vecometorealizethere'sareal
needforabookforpractitioners(notnovices)that
explainsthebasicprinciplesofrelationaltheoryinaway
nottaintedbythequirksandpeculiaritiesofexisting
products,commercialpractice,ortheSQLstandard.I
wrotethisbooktofillthatneed.Myintendedaudienceis
thusexperienceddatabasepractitionerswhoarehonest
enoughtoadmittheydon'tunderstandthetheory
underlyingtheirownfieldaswellastheymight,or
should.Thattheoryis,ofcourse,therelationalmodel—
andwhileit'struethatthefundamentalideasofthat
theoryareallquitesimple,it'salsotruethatthey're
widelymisrepresented,orunderappreciated,orboth.
Often,infact,theydon'tseemtobeunderstoodatall.
Forexample,hereareafewrelationalquestions[1]…How
manyofthemcanyouanswer?
Whatexactlyisfirstnormalform?
What'stheconnectionbetweenrelationsandpredicates?
What'ssemanticoptimization?
What'sanimagerelation?
Whyissemidifferenceimportant?
Whydoesn'tdeferredintegritycheckingmakesense?
What'sarelationvariable?
What'sprenexnormalform?


Canarelationhaveanattributewhosevaluesare
relations?
IsSQLrelationallycomplete?
WhyisTheInformationPrincipleimportant?
HowdoesXMLfitwiththerelationalmodel?
Thisbookprovidesanswerstotheseandmanyrelated
questions.Overall,it'smeanttohelpdatabase
practitionersunderstandrelationaltheoryindepthand
makegooduseofthatunderstandingintheir
professionalday-to-dayactivities.
[1]Forreasonsthataren'timportanthere,I've

replacedafewofthequestionsinthislistbynew
ones.
Asthefinalsentenceinthisextractindicates,itwasmyhope
thatreadersofthatbookwouldbeabletoapplyitsideasfor
themselves,withoutfurtherassistancefrommeasitwere.
ButI'vesincecometorealizethat,contrarytopopular
opinion,SQLissuchadifficultlanguagethatitcanbefar
fromobvioushowtouseitwithoutviolatingrelational
principles.Ithereforedecidedtoexpandtheoriginalbookto
includeexplicit,concreteadviceonexactlythatissue(howto
useSQLrelationally,Imean).Somyaiminthepresentbook
isstillthesameasbefore(i.e.,Iwanttohelpdatabase
practitionersunderstandrelationaltheoryindepthandmake
gooduseofthatunderstandingintheirprofessional
activities),butI'vetriedtomakethematerialalittleeasier
todigest,perhaps,andcertainlyeasiertoapply.Inother
words,I'veincludedagreatdealofSQL-specificmaterial
(andit'sthisfact,morethananythingelse,thataccountsfor
theincreaseinsizeoverthepreviousversion).

P.3.FurtherRemarksontheText


Ineedtotakecareofseveralfurtherpreliminaries.Firstof
all,myownunderstandingoftherelationalmodelhas
evolvedovertheyears,andcontinuestodoso.Thisbook
representsmyverylatestthinkingonthesubject;thus,if
youdetectanytechnicaldiscrepancies—andthereareafew
—betweenthisbookandotherbooksyoumighthaveseen
bymyself(includinginparticulartheonethisbookismeant
toreplace),thepresentbookshouldbetakenas
superseding.ThoughIhastentoaddthatsuchdiscrepancies
aremostlyofafairlyminornature;what'smore,I'vetaken
carealwaystorelatenewtermsandconceptstoearlier
ones,whereverIfeltitwasnecessarytodoso.
Second,Iwill,asadvertised,betalkingabouttheory—but
it'sanarticleoffaithwithmethatTheoryispractical.I
mentionthispointexplicitlybecausesomanypeopleseem
tobelievetheopposite:namely,thatifsomething's
theoretical,itcan'tbepractical.Butthetruthisthattheory
(atleast,relationaltheory,whichiswhatI'mtalkingabout
here)ismostdefinitelyverypracticalindeed.Thepurposeof
thattheoryisnotjusttheoryforitsownsake;thepurpose
ofthattheoryistoallowustobuildsystemsthatare100
percentpractical.Everydetailofthetheoryisthereforsolid
practicalreasons.Asonerevieweroftheearlierbook,
StèphaneFaroult,wrote:"Whenyouhaveabitofpractice,
yourealizethere'snowaytoavoidhavingtoknowthe
theory."What'smore,thattheoryisnotonlypractical,it's
fundamental,straightforward,simple,useful,anditcanbe
fun(asIhopetodemonstrateinthecourseofthisbook).
Ofcourse,wereallydon'thavetolookanyfurtherthanthe
relationalmodelitselftofindthemoststrikingpossible
illustrationoftheforegoingthesis.Infact,itreallyshouldn't
benecessarytohavetodefendthenotionthattheoryis
practical,inacontextsuchasours:namely,amultibillion
dollarindustrytotallyfoundedononegreattheoreticalidea.
ButIsupposethecynic'spositionwouldbe"Yes,butwhat


hastheorydoneformelately?"Inotherwords,thoseofus
whodothinktheoryisimportantmustbecontinually
justifyingourselvestoourcritics—whichisanotherreason
whyIthinkabooklikethisoneisneeded.
Third,asI'vesaid,thebookdoesgointoafairamountof
detailregardingfeaturesofSQLortherelationalmodelor
both.(Itdeliberatelyhaslittletosayontopicsthataren't
particularlyrelational;forexample,thereisn'tmuchon
transactions.)Throughout,I'vetriedtomakeitclearwhen
thediscussionsapplytoSQLspecifically,whentheyapplyto
therelationalmodelspecifically,andwhentheyapplyto
both.Ishouldemphasize,however,thattheSQLdiscussions
inparticulararen'tmeanttobeexhaustive.SQLissucha
complexlanguage,andprovidessomanydifferentwaysof
doingthesamething,andissubjecttosomanyexceptions
andspecialcases,thattobeexhaustive—evenifitwere
possible,whichItendtodoubt—wouldbecounterproductive;
certainlyitwouldmakethebookmuchtoolong.SoI'vetried
tofocusonwhatIthinkarethemostimportantissues,and
I'vetriedtobeasbriefaspossibleontheissuesI'vechosen
tocover.AndI'dliketoclaimthatifyoudoeverythingItell
you,anddon'tdoanythingIdon'ttellyou,thentoafirst
approximationyou'llbesafe:You'llbeusingSQLrelationally.
Butwhetherthatclaimisjustified,ortowhatextentitis,
mustbeforyoutojudge.
TotheforegoingIhavetoaddthat,unfortunately,thereare
somesituationsinwhichSQLjustcan'tbeusedrelationally.
Forexample,someSQLintegritycheckingsimplyhastobe
deferred(usuallytocommittime),eventhoughthe
relationalmodelrejectssuchcheckingaslogicallyflawed.
Thebookdoesofferadviceonwhattodoinsuchcases,but
IfearitoftenboilsdowntojustDothebestyoucan.Atleast
Ihopeyou'llunderstandtherisksinvolvedindepartingfrom
themodel.
Ishouldsaytoothatsomeoftherecommendationsoffered


aren'tspecificallyrelationalanywaybutare,rather,just
mattersofgeneralgoodpractice—thoughsometimesthere
arerelationalimplications(implicationsthatcanbealittle
unobvious,too,perhapsIshouldadd).Avoidcoercionsisa
goodexamplehere.
Fourth,pleasenotethatIusethetermSQLthroughoutthe
booktomeanthestandardversionofthatlanguage
exclusively,notsomeproprietarydialect(barringexplicit
statementstothecontrary).Inparticular,Ifollowthe
standardinassumingthepronunciation"esscueell,"not
"sequel"(thoughthislatteriscommoninthefield),thereby
sayingthingslikeanSQLtable,notaSQLtable.
Fifth,thebookismeanttobereadinsequence,pretty
much,exceptasnotedhereandthereinthetextitself(most
ofthechaptersdorelytosomeextentonmaterialcoveredin
earlierones,soyoushouldn'tjumparoundtoomuch).Also,
eachchapterincludesasetofexercises.Youdon'thaveto
dothoseexercises,ofcourse,butIthinkit'sagoodideato
haveagoatsomeofthematleast.Answers,oftengiving
moreinformationaboutthesubjectathand,aregivenin
AppendixC.
Finally,I'dliketomentionthatIhavesomeliveseminars
availablebasedonthematerialinthisbook.See
http://www.clik.to/chris_date/or
http://www.thethirdmanifesto.com/forfurtherdetails.

P.4.ConventionsUsedinThisBook
Thefollowingtypographicalconventionsareusedinthis
book:
Italic
Usedforemphasis.Indicatesnewterms.Alsoindicates
whenavariable,suchasx,isusedinplaceofsomething


elseduringadiscussioninthemaintextofthebook.

Constantwidth
Usedforcodeexamples.

Constantwidthitalic

Markstheoccurrenceofavariableoruser-supplied
elementinacodeexample.

P.5.UsingCodeExamples
Thisbookisheretohelpyougetyourjobdone.Ingeneral,
youmayusethecodeinthisbookinyourprogramsand
documentation.Youdonotneedtocontactusforpermission
unlessyou'rereproducingasignificantportionofthecode.
Forexample,writingaprogramthatusesseveralchunksof
codefromthisbookdoesnotrequirepermission.Sellingor
distributingaCD-ROMofexamplesfromO'Reillybooksdoes
requirepermission.Answeringaquestionbycitingthisbook
andquotingexamplecodedoesnotrequirepermission.
Incorporatingasignificantamountofexamplecodefromthis
bookintoyourproduct'sdocumentationdoesrequire
permission.
Weappreciate,butdonotrequire,attribution.Anattribution
usuallyincludesthetitle,author,publisher,andISBN.For
example:"SQLandRelationalTheorybyC.J.Date.
Copyright2009C.J.Date,978-0-596-52306-0."
Ifyoufeelyouruseofcodeexamplesfallsoutsidefairuseor
thepermissiongivenabove,feelfreetocontactusat
permissions@oreilly.com.


P.6.CommentsandQuestions
I'vedonemybesttomakethisbookaserror-freeasIcan,
butyoumightfindmistakes.Ifso,pleasenotifythe
publisherbywritingto:
O'ReillyMedia,Inc.
1005GravensteinHighwayNorth
Sebastopol,CA95472
800-998-9938(intheUnitedStatesorCanada)
707-829-0515(internationalorlocal)
707-829-0104(FAX)
Youcanalsosendmessageselectronically.Tobeputonthe
mailinglistorrequestacatalog,sendemailto:
info@oreilly.com
Toasktechnicalquestionsorcommentonthebook,send
emailto:
bookquestions@oreilly.com
Wehaveawebsiteforthisbook,whereyoucanfind
examplesanderrata(previouslyreportederrorsand
correctionsareavailableforpublicviewthere).Youcan
accessthispageat:
http://www.oreilly.com/catalog/9780596523060/
Formoreinformationaboutthisbookandothers,seethe
O'Reillywebsite:
http://www.oreilly.com/

P.7.Safari®BooksOnline
WhenyouseeaSafari®BooksOnlineicononthecoverof
yourfavoritetechnologybook,thatmeansthebookis


availableonlinethroughtheO'ReillyNetworkSafari
Bookshelf.
Safarioffersasolutionthat'sbetterthane-books.It'sa
virtuallibrarythatletsyoueasilysearchthousandsoftop
techbooks,cutandpastecodesamples,downloadchapters,
andfindquickanswerswhenyouneedthemostaccurate,
currentinformation.Tryitforfreeat
http://safari.oreilly.com/.

P.8.Acknowledgments
I'dbeenthinkingforsometimeaboutrevisingtheearlier
booktoincludemoreonSQLinparticular,butthespurthat
finallygotmedowntoitwassittinginonaclass,latein
2007,fordatabasepractitioners.Theclasswastaughtby
ToonKoppelaarsandwasbasedonthebookhewrotewith
LexdeHaan(seeAppendixDofthepresentbook),andvery
gooditwas,too.Butwhatstruckmemostaboutthatclass
wasseeingatfirsthandhowapparentlyincapablethe
attendeeswereofapplyingrelationalandlogicalprinciplesto
theiruseofSQL.Now,Idoassumethoseattendeeshad
someknowledgeofthosetopics—theyweredatabase
practitioners,afterall—butitseemedtometheyreally
neededsomeguidanceintheapplicationofthoseideasto
theirdailydatabaseactivities.AndsoIputthisbook
together.SoI'mthankful,firstofall,toToonandLexfor
providingmewiththenecessaryimpetustogetstartedon
thisproject.I'mgratefulalsotomyreviewersHerb
Edelstein,SheeriKtitzer,AndyOram,PeterRobson,and
BaronSchwartzfortheircommentsonearlierdrafts,and
HughDarwenandJimMeltonforothertechnicalassistance.
Next,I'dliketothankmywifeLindy,asalways,forher
supportthroughoutthisandallofmyotherdatabase
projectsovertheyears.Finally,I'mgratefultoeveryoneat
O'Reilly—especiallyIsabelKunkle,AndyOram,andAdam
Witwer—fortheirencouragement,contributions,andsupport


throughouttheproductionofthisbook.
C.J.DATE
HEALDSBURG,CALIFORNIA
2008



Chapter1.SettingtheScene
ARELATIONALAPPROACHTOSQL:THAT'STHETHEME,
ORONEOFTHETHEMES,OFTHISBOOK.Ofcourse,to
treatsuchatopicadequately,Ineedtocoverrelational
issuesaswellasissuesofSQLperse—andwhilethisremark
obviouslyappliestothebookasawhole,itappliestothis
firstchapterwithspecialforce.Asaconsequence,this
chapterhascomparativelylittletosayaboutSQLassuch.
WhatIwanttodoisreviewmaterialthatforthemostpart,
atanyrate,Ihopeyoualreadyknow.Myintentisto
establishapointofdeparture:inotherwords,tolaysome
groundworkonwhichtherestofthebookcanbuild.But
eventhoughIhopeyou'refamiliarwithmostofwhatIhave
tosayinthischapter,I'dliketosuggest,respectfully,that
younotskipit.Youneedtoknowwhatyouneedtoknow(if
youseewhatImean);inparticular,youneedtobesureyou
havetheprerequisitesneededtounderstandthematerialto
comeinlaterchapters.InfactI'dliketorecommend,
politely,thatthroughoutthebookyounotskipthediscussion
ofsometopicjustbecauseyouthinkyou'refamiliarwiththat
topicalready.Forexample,areyouabsolutelysureyouknow
whatakeyis,inrelationalterms?Orajoin?[2]
[2]There'satleastonepunditwhodoesn't.Thefollowing

isadirectquotefromadocumentpurporting(likethis
book!)toofferadvicetoSQLusers:"Don'tusejoins…
OracleandSQLServerhavefundamentallydifferent
approachestotheconcept…Youcanendupwith
unexpectedresultsets…Youshouldunderstandthe
basictypesofjoinclauses…Equi-joinsareformedby
retrievingallthedatafromtwoseparatesourcesand
combiningitintoone,largetable…Innerjoinsare
joinedontheinnercolumnsoftwotables.Outerjoins
arejoinedontheoutercolumnsoftwotables.Leftjoins
arejoinedontheleftcolumnsoftwotables.Rightjoins


arejoinedontherightcolumnsoftwotables."



1.1.TheRelationalModelIsMuch
Misunderstood
Professionalsinanydisciplineneedtoknowthefoundations
oftheirfield.Soifyou'readatabaseprofessional,youneed
toknowtherelationalmodel,becausetherelationalmodelis
thefoundation(orahugepartofthefoundation,atany
rate)ofthedatabasefieldinparticular.Now,everycoursein
databasemanagement,beitacademicorcommercial,does
atleastpaylipservicetotheideaofteachingtherelational
model—butmostofthatteachingseemstobedonevery
badly,ifresultsareanythingtogoby;certainlythemodel
isn'twellunderstoodinthedatabasecommunityatlarge.
Herearesomepossiblereasonsforthisstateofaffairs:
Themodelistaughtinavacuum.Thatis,forbeginners
atleast,it'shardtoseetherelevanceofthematerial,or
it'shardtounderstandtheproblemsit'smeanttosolve,
orboth.
Theinstructorsthemselvesdon'tfullyunderstandor
appreciatethesignificanceofthematerial.
Perhapsmostlikelyinpractice,themodelassuchisn't
taughtatall—theSQLlanguageorsomespecificdialect
ofthatlanguage,suchastheOracledialect,istaught
instead.
Sothisbookisaimedatdatabasepractitionersingeneral,
andSQLpractitionersinparticular,whohavehadsome
exposuretotherelationalmodelbutdon'tknowasmuch
aboutitastheyoughttoorwouldliketo.It'sdefinitelynot
meantforbeginners;however,itisn'tjustarefresher
course,either.Tobemorespecific,I'msureyouknow
somethingaboutSQL;but—andIapologizeforthepossibly
offensivetonehere—ifyourknowledgeoftherelational


modelderivesonlyfromyourknowledgeofSQL,thenI'm
afraidyouwon'tknowtherelationalmodelaswellasyou
should,andyou'llprobablyknow"somethingsthatain'tso."
Ican'tsayittoostrongly:SQLandtherelationalmodel
aren'tthesamething.Herebywayofillustrationaresome
relationalissuesthatSQLisn'ttooclearon(toputitmildly):
Whatdatabases,relations,andtuplesreallyare
Thedifferencebetweenrelationvaluesandrelation
variables
Therelevanceofpredicatesandpropositions
Theimportanceofattributenames
Thecrucialroleofintegrityconstraints
andsoon(thisisn'tanexhaustivelist).Alloftheseissues,
andmanyothers,areaddressedinthisbook.
Isayagain:Ifyourknowledgeoftherelationalmodel
derivesonlyfromyourknowledgeofSQL,thenyoumight
know"somethingsthatain'tso."Oneconsequenceofthis
stateofaffairsisthatyoumightfind,inreadingthisbook,
thatyouhavetodosomeunlearning—andunlearning,
unfortunately,isveryhardtodo.



1.2.SomeRemarksonTerminology
Youprobablynoticedrightaway,inthatlistofrelational
issuesintheprevioussection,thatIusedtheformalterms
relation,tuple,[3]andattribute.SQLdoesn'tusetheseterms,
however—itusesthemore"userfriendly"termstable,row,
andcolumninstead.AndI'mgenerallysympathetictothe
ideaofusingmoreuserfriendlyterms,iftheycanhelpmake
theideasmorepalatable.Inthecaseathand,however,it
seemstomethat,regrettably,theydon'tmaketheideas
morepalatable;instead,theydistortthem,andinfactdo
thecauseofgenuineunderstandingagravedisservice.The
truthis,arelationisnotatable,atupleisnotarow,andan
attributeisnotacolumn.Andwhileitmightbeacceptableto
pretendotherwiseininformalcontexts—indeed,Ioftendo
exactlythatmyself—Iwouldarguethatit'sacceptableonlyif
weallunderstandthatthemoreuserfriendlytermsarejust
anapproximationtothetruthandfailoveralltocapturethe
essenceofwhat'sreallygoingon.Toputitanotherway:If
youdounderstandthetruestateofaffairs,thenjudicious
useoftheuserfriendlytermscanbeagoodidea;butin
ordertolearnandappreciatethattruestateofaffairsinthe
firstplace,youreallydoneedtocometogripswiththemore
formalterms.Inthisbook,therefore,I'lltendtousethose
moreformalterms—atleastwhenI'mtalkingaboutthe
relationalmodelasopposedtoSQL—andI'llgiveprecise
definitionsforthemattherelevantjuncture.InSQL
contexts,bycontrast,I'lluseSQL'sownterms.
[3]Usuallypronouncedtorhymewithcouple.

Andanotherpointonterminology:HavingsaidthatSQLtries
tosimplifyonesetofterms,Imustaddthatitalsodoesits
besttocomplicateanother.Irefertoitsuseoftheterms
operator,function,procedure,routine,andmethod,allof
whichdenoteessentiallythesamething(with,perhaps,very


minordifferences).InthisbookI'llusethetermoperator
throughout.
TalkingofSQL,incidentally,letmeremindyouthat(as
statedinthepreface)Iusethattermtomeanthestandard
versionofthelanguageexclusively,[4]exceptinafewplaces
wherethecontextdemandsotherwise.However:
[4]InternationalOrganizationforStandardization(ISO):

DatabaseLanguageSQL,DocumentISO/IEC9075:2003
(2003).
SometimesIuseterminologythatdiffersfromthatof
thestandard.Forexample,Iusethetermtable
expressioninplaceofthestandardtermquery
expression,because(a)thevaluesuchexpressions
denoteisatable,notaquery,and(b)queriesaren'tthe
onlycontextinwhichsuchexpressionsareusedanyway.
(Asamatteroffactthestandarddoesusethetermtable
expression,butwithamuchmorelimitedmeaning;to
bespecific,itusesittorefertowhatcomesafterthe
SELECTclauseinaSELECTexpression.)
Followingonfromthepreviouspoint,Ishouldaddthat
notalltableexpressionsarelegalinSQLinallcontexts
wheretheymightbeexpectedtobe.Inparticular,an
explicitJOINinvocation,althoughitcertainlydoes
denoteatable,can'tappearasa"standalone"table
expression(i.e.,attheoutermostlevelofnesting),nor
canitappearasthetableexpressioninparenthesesthat
constitutesasubquery(seeChapter12).Pleasenote
thattheseremarksapplytomanyoftheindividual
discussionsinthebodyofthebook;however,itwould
beverytedioustokeeponrepeatingthem,andIwon't.
(They'rereflectedintheBNFgrammarinChapter12,
however.)
Iignoreaspectsofthestandardthatmightberegarded


asatrifleesoteric—especiallyiftheyaren'tpartofwhat
thestandardcallsCoreSQLordon'thavemuchtodo
withrelationalprocessingassuch.Exampleshereinclude
thesocalledanalyticorwindow(OLAP)functions;
dynamicSQL;recursivequeries;temporarytables;and
detailsofuserdefinedtypes.
Partlyfortypographicalreasons,Iuseastylefor
commentsthatdiffersfromthatofthestandard.Tobe
specific,Ishowcommentsastextstringsinitalics,
bracketedby"/*"and"*/"delimiters.
Beaware,however,thatallSQLproductsincludefeatures
thataren'tpartofthestandardperse.RowIDsprovidea
commonexample.Mygeneraladviceregardingsuchfeatures
is:Byallmeansusethemifyouwantto—butnotifthey
violaterelationalprinciples(afterall,thisbookissupposed
tobedescribingarelationalapproachtoSQL).Forexample,
rowIDsarelikelytoviolatewhat'scalledThePrincipleof
Interchangeability(seeChapter9);andiftheydo,thenI
certainlywouldn'tusethem.But,hereandeverywhere,the
overridingruleis:Youcandowhatyoulike,solongasyou
knowwhatyou'redoing.



1.3.Principles,NotProducts
It'sworthtakingafewmomentstoexaminethequestionof
why,asIclaimedearlier,youasadatabaseprofessional
needtoknowtherelationalmodel.Thereasonisthatthe
relationalmodelisn'tproductspecific;instead,it'sconcerned
withprinciples.WhatdoImeanbyprinciples?Well,here'sa
dictionarydefinition(fromChambersTwentiethCentury
Dictionary):
principle:asource,root,origin:thatwhichisfundamental:
essentialnature:theoreticalbasis:afundamentaltruthon
whichothersarefoundedorfromwhichtheyspring
Thepointaboutprinciplesis:Theyendure.Bycontrast,
productsandtechnologies(andtheSQLlanguage,cometo
that)changeallthetime—butprinciplesdon't.Forexample,
supposeyouknowOracle;infact,supposeyou'reanexpert
onOracle.ButifOracleisallyouknow,thenyourknowledge
isnotnecessarilytransferableto,say,aDB2orSQLServer
environment(itmightevenmakeithardertomakeprogress
inthatnewenvironment).Butifyouknowtheunderlying
principles—inotherwords,ifyouknowtherelationalmodel—
thenyouhaveknowledgeandskillsthatwillbetransferable:
knowledgeandskillsthatyou'llbeabletoapplyinevery
environmentandwillneverbeobsolete.
Inthisbook,therefore,we'llbeconcernedwithprinciples,
notproducts,andfoundations,notfads.ButIrealizeyoudo
havetomakecompromisesandtradeoffssometimes,inthe
realworld.Foroneexample,sometimesyoumighthave
goodpragmaticreasonsfornotdesigningthedatabasein
thetheoreticallyoptimalway.Foranother,considerSQLonce
again.Althoughit'scertainlypossibletouseSQLrelationally
(forthemostpart,atanyrate),sometimesyou'llfind—
becauseexistingimplementationsaresofarfromperfect—


thattherearesevereperformancepenaltiesfordoingso…in
whichcaseyoumightbemoreorlessforcedintodoing
somethingnot"trulyrelational"(likewritingaqueryinsome
unnaturalwaytoforcetheimplementationtouseanindex).
However,Ibelieveveryfirmlythatyoushouldalwaysmake
suchcompromisesandtradeoffsfromapositionof
conceptualstrength.Thatis:
Youshouldunderstandwhatyou'redoingwhenyoudo
decidetomakesuchacompromise.
Youshouldknowwhatthetheoreticallycorrectsituation
is,andyoushouldhavegoodreasonsfordepartingfrom
it.
Youshoulddocumentthosereasons,too,sothatifthey
goawayatsomefuturetime(forexample,becausea
newreleaseoftheproductyou'reusingdoesabetterjob
insomerespect),thenitmightbepossibletobackoff
fromtheoriginalcompromise.
Thefollowingquote—whichisduetoLeonardodaVinci
(1452–1519)andisthussome500yearsold—sumsupthe
situationadmirably:
Thosewhoareenamoredofpracticewithouttheoryare
likeapilotwhogoesintoashipwithoutrudderor
compassandneverhasanycertaintywhereheisgoing.
Practiceshouldalwaysbebasedonasoundknowledge
oftheory.
(OK,Iaddedtheitalics.)



1.4.AReviewoftheOriginalModel
Thepurposeofthissectionistoserveasakickoffpointfor
subsequentdiscussions;itreviewssomeofthemostbasic
aspectsoftherelationalmodelasoriginallydefined.Note
thatqualifier—"asoriginallydefined"!Onewidespread
misconceptionabouttherelationalmodelisthatit'satotally
staticthing.It'snot.It'slikemathematicsinthatrespect:
Mathematicstooisnotastaticthingbutchangesovertime.
Infact,therelationalmodelcanitselfbeseenasasmall
branchofmathematics;assuch,itevolvesovertimeasnew
theoremsareprovedandnewresultsdiscovered.What's
more,thosenewcontributionscanbemadebyanyonewho's
competenttodoso.Likemathematicsagain,therelational
model,thoughoriginallyinventedbyoneman,hasbecomea
communityeffortandnowbelongstotheworld.
Bytheway,incaseyoudon'tknow,thatonemanwasE.F.
Codd,atthetimearesearcheratIBM(EforEdgarandFfor
Frank—buthealwayssignedwithhisinitials;tohisfriends,
amongwhomIwasproudtocountmyself,hewasTed).It
waslatein1968thatCodd,amathematicianbytraining,
firstrealizedthatthedisciplineofmathematicscouldbeused
toinjectsomesolidprinciplesandrigorintoafield,database
management,thatwasalltoodeficientinanysuchqualities
priortothattime.Hisoriginaldefinitionoftherelational
modelappearedinanIBMResearchReportin1969,andI'll
havealittlemoretosayaboutthatpaperinAppendixD.

1.4.1.StructuralFeatures
Theoriginalmodelhadthreemajorcomponents—structure,
integrity,andmanipulation—andI'llbrieflydescribeeachin
turn.Pleasenoterightaway,however,thatallofthe
"definitions"I'llbegivingareveryloose;I'llmakethem


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×

×