Tải bản đầy đủ

OReilly SQL cookbook dec 2005 ISBN 0596009763

SQLCookbook
ByAnthonyMolinaro
...............................................
Publisher:O'Reilly
PubDate:December2005
PrintISBN-10:0-596-00976-3
PrintISBN-13:978-0-59-600976-2
Pages:628

TableofContents|Index

YouknowtherudimentsoftheSQLquerylanguage,yetyoufeelyouaren'ttakingfull
advantageofSQL'sexpressivepower.You'dliketolearnhowtodomoreworkwithSQL
insidethedatabasebeforepushingdataacrossthenetworktoyourapplications.You'dlike
totakeyourSQLskillstothenextlevel.
Let'sfaceit,SQLisadeceptivelysimplelanguagetolearn,andmanydatabasedevelopers
nevergofarbeyondthesimplestatement:SELECTFROMWHERE.Butthereissomuch
moreyoucandowiththelanguage.IntheSQLCookbook,experiencedSQLdeveloper
AnthonyMolinaroshareshisfavoriteSQLtechniquesandfeatures.You'lllearnabout:

Windowfunctions,arguablythemostsignificantenhancementtoSQLinthepast

decade.Ifyou'renotusingthese,you'remissingout
Powerful,database-specificfeaturessuchasSQLServer'sPIVOTandUNPIVOT
operators,Oracle'sMODELclause,andPostgreSQL'sveryusefulGENERATE_SERIES
function
Pivotingrowsintocolumns,reverse-pivotingcolumnsintorows,usingpivotingto
facilitateinter-rowcalculations,anddouble-pivotingaresultset
Bucketization,andwhyyoushouldneverusethatterminBrooklyn.
Howtocreatehistograms,summarizedataintobuckets,performaggregationsover
amovingrangeofvalues,generaterunning-totalsandsubtotals,andother
advanced,datawarehousingtechniques
Thetechniqueofwalkingastring,whichallowsyoutouseSQLtoparsethroughthe
characters,words,ordelimitedelementsofastring


WritteninO'Reilly'spopularProblem/Solution/Discussionstyle,theSQLCookbookissure
toplease.Anthony'scredois:"Whenitcomesdowntoit,weallgotowork,weallhave
billstopay,andweallwanttogohomeatareasonabletimeandenjoywhat'sstill
availableofourdays."TheSQLCookbookmovesquicklyfromproblemtosolution,saving
youtimeeachstepoftheway.


SQLCookbook
ByAnthonyMolinaro
...............................................
Publisher:O'Reilly
PubDate:December2005
PrintISBN-10:0-596-00976-3
PrintISBN-13:978-0-59-600976-2
Pages:628

TableofContents|Index















































Copyright
Dedication
Preface
WhyIWroteThisBook
ObjectivesofThisBook
AudienceforThisBook
HowtoUseThisBook
What'sMissingfromThisBook
StructureofThisBook
PlatformandVersion
TablesUsedinThisBook
ConventionsUsedinThisBook
UsingCodeExamples
CommentsandQuestions
Safari®Enabled
Acknowledgments
Chapter1.RetrievingRecords
Recipe1.1.RetrievingAllRowsandColumnsfromaTable
Recipe1.2.RetrievingaSubsetofRowsfromaTable
Recipe1.3.FindingRowsThatSatisfyMultipleConditions
Recipe1.4.RetrievingaSubsetofColumnsfromaTable
Recipe1.5.ProvidingMeaningfulNamesforColumns
Recipe1.6.ReferencinganAliasedColumnintheWHEREClause
Recipe1.7.ConcatenatingColumnValues













































































Recipe1.8.UsingConditionalLogicinaSELECTStatement
Recipe1.9.LimitingtheNumberofRowsReturned
Recipe1.10.ReturningnRandomRecordsfromaTable
Recipe1.11.FindingNullValues
Recipe1.12.TransformingNullsintoRealValues
Recipe1.13.SearchingforPatterns
Chapter2.SortingQueryResults
Recipe2.1.ReturningQueryResultsinaSpecifiedOrder
Recipe2.2.SortingbyMultipleFields
Recipe2.3.SortingbySubstrings
Recipe2.4.SortingMixedAlphanumericData
Recipe2.5.DealingwithNullswhenSorting
Recipe2.6.SortingonaDataDependentKey
Chapter3.WorkingwithMultipleTables
Recipe3.1.StackingOneRowsetatopAnother
Recipe3.2.CombiningRelatedRows
Recipe3.3.FindingRowsinCommonBetweenTwoTables
Recipe3.4.RetrievingValuesfromOneTableThatDoNotExistinAnother
Recipe3.5.RetrievingRowsfromOneTableThatDoNotCorrespondtoRowsin
Another
Recipe3.6.AddingJoinstoaQueryWithoutInterferingwithOtherJoins
Recipe3.7.DeterminingWhetherTwoTablesHavetheSameData
Recipe3.8.IdentifyingandAvoidingCartesianProducts
Recipe3.9.PerformingJoinswhenUsingAggregates
Recipe3.10.PerformingOuterJoinswhenUsingAggregates
Recipe3.11.ReturningMissingDatafromMultipleTables
Recipe3.12.UsingNULLsinOperationsandComparisons
Chapter4.Inserting,Updating,Deleting
Recipe4.1.InsertingaNewRecord
Recipe4.2.InsertingDefaultValues
Recipe4.3.OverridingaDefaultValuewithNULL
Recipe4.4.CopyingRowsfromOneTableintoAnother
Recipe4.5.CopyingaTableDefinition
Recipe4.6.InsertingintoMultipleTablesatOnce
Recipe4.7.BlockingInsertstoCertainColumns
Recipe4.8.ModifyingRecordsinaTable
Recipe4.9.UpdatingwhenCorrespondingRowsExist
Recipe4.10.UpdatingwithValuesfromAnotherTable
Recipe4.11.MergingRecords















































































Recipe4.12.DeletingAllRecordsfromaTable
Recipe4.13.DeletingSpecificRecords
Recipe4.14.DeletingaSingleRecord
Recipe4.15.DeletingReferentialIntegrityViolations
Recipe4.16.DeletingDuplicateRecords
Recipe4.17.DeletingRecordsReferencedfromAnotherTable
Chapter5.MetadataQueries
Recipe5.1.ListingTablesinaSchema
Recipe5.2.ListingaTable'sColumns
Recipe5.3.ListingIndexedColumnsforaTable
Recipe5.4.ListingConstraintsonaTable
Recipe5.5.ListingForeignKeysWithoutCorrespondingIndexes
Recipe5.6.UsingSQLtoGenerateSQL
Recipe5.7.DescribingtheDataDictionaryViewsinanOracleDatabase
Chapter6.WorkingwithStrings
Recipe6.1.WalkingaString
Recipe6.2.EmbeddingQuotesWithinStringLiterals
Recipe6.3.CountingtheOccurrencesofaCharacterinaString
Recipe6.4.RemovingUnwantedCharactersfromaString
Recipe6.5.SeparatingNumericandCharacterData
Recipe6.6.DeterminingWhetheraStringIsAlphanumeric
Recipe6.7.ExtractingInitialsfromaName
Recipe6.8.OrderingbyPartsofaString
Recipe6.9.OrderingbyaNumberinaString
Recipe6.10.CreatingaDelimitedListfromTableRows
Recipe6.11.ConvertingDelimitedDataintoaMulti-ValuedIN-List
Recipe6.12.AlphabetizingaString
Recipe6.13.IdentifyingStringsThatCanBeTreatedasNumbers
Recipe6.14.ExtractingthenthDelimitedSubstring
Recipe6.15.ParsinganIPAddress
Chapter7.WorkingwithNumbers
Recipe7.1.ComputinganAverage
Recipe7.2.FindingtheMin/MaxValueinaColumn
Recipe7.3.SummingtheValuesinaColumn
Recipe7.4.CountingRowsinaTable
Recipe7.5.CountingValuesinaColumn
Recipe7.6.GeneratingaRunningTotal
Recipe7.7.GeneratingaRunningProduct
Recipe7.8.CalculatingaRunningDifference








































































Recipe7.9.CalculatingaMode
Recipe7.10.CalculatingaMedian
Recipe7.11.DeterminingthePercentageofaTotal
Recipe7.12.AggregatingNullableColumns
Recipe7.13.ComputingAveragesWithoutHighandLowValues
Recipe7.14.ConvertingAlphanumericStringsintoNumbers
Recipe7.15.ChangingValuesinaRunningTotal
Chapter8.DateArithmetic
Recipe8.1.AddingandSubtractingDays,Months,andYears
Recipe8.2.DeterminingtheNumberofDaysBetweenTwoDates
Recipe8.3.DeterminingtheNumberofBusinessDaysBetweenTwoDates
Recipe8.4.DeterminingtheNumberofMonthsorYearsBetweenTwoDates
Recipe8.5.DeterminingtheNumberofSeconds,Minutes,orHoursBetween
TwoDates
Recipe8.6.CountingtheOccurrencesofWeekdaysinaYear
Recipe8.7.DeterminingtheDateDifferenceBetweentheCurrentRecordandthe
NextRecord
Chapter9.DateManipulation
Recipe9.1.DeterminingifaYearIsaLeapYear
Recipe9.2.DeterminingtheNumberofDaysinaYear
Recipe9.3.ExtractingUnitsofTimefromaDate
Recipe9.4.DeterminingtheFirstandLastDayofaMonth
Recipe9.5.DeterminingAllDatesforaParticularWeekdayThroughoutaYear
Recipe9.6.DeterminingtheDateoftheFirstandLastOccurrenceofaSpecific
WeekdayinaMonth
Recipe9.7.CreatingaCalendar
Recipe9.8.ListingQuarterStartandEndDatesfortheYear
Recipe9.9.DeterminingQuarterStartandEndDatesforaGivenQuarter
Recipe9.10.FillinginMissingDates
Recipe9.11.SearchingonSpecificUnitsofTime
Recipe9.12.ComparingRecordsUsingSpecificPartsofaDate
Recipe9.13.IdentifyingOverlappingDateRanges
Chapter10.WorkingwithRanges
Recipe10.1.LocatingaRangeofConsecutiveValues
Recipe10.2.FindingDifferencesBetweenRowsintheSameGrouporPartition
Recipe10.3.LocatingtheBeginningandEndofaRangeofConsecutiveValues
Recipe10.4.FillinginMissingValuesinaRangeofValues
Recipe10.5.GeneratingConsecutiveNumericValues
Chapter11.AdvancedSearching














































































Recipe11.1.PaginatingThroughaResultSet
Recipe11.2.SkippingnRowsfromaTable
Recipe11.3.IncorporatingORLogicwhenUsingOuterJoins
Recipe11.4.DeterminingWhichRowsAreReciprocals
Recipe11.5.SelectingtheTopnRecords
Recipe11.6.FindingRecordswiththeHighestandLowestValues
Recipe11.7.InvestigatingFutureRows
Recipe11.8.ShiftingRowValues
Recipe11.9.RankingResults
Recipe11.10.SuppressingDuplicates
Recipe11.11.FindingKnightValues
Recipe11.12.GeneratingSimpleForecasts
Chapter12.ReportingandWarehousing
Recipe12.1.PivotingaResultSetintoOneRow
Recipe12.2.PivotingaResultSetintoMultipleRows
Recipe12.3.ReversePivotingaResultSet
Recipe12.4.ReversePivotingaResultSetintoOneColumn
Recipe12.5.SuppressingRepeatingValuesfromaResultSet
Recipe12.6.PivotingaResultSettoFacilitateInter-RowCalculations
Recipe12.7.CreatingBucketsofData,ofaFixedSize
Recipe12.8.CreatingaPredefinedNumberofBuckets
Recipe12.9.CreatingHorizontalHistograms
Recipe12.10.CreatingVerticalHistograms
Recipe12.11.ReturningNon-GROUPBYColumns
Recipe12.12.CalculatingSimpleSubtotals
Recipe12.13.CalculatingSubtotalsforAllPossibleExpressionCombinations
Recipe12.14.IdentifyingRowsThatAreNotSubtotals
Recipe12.15.UsingCaseExpressionstoFlagRows
Recipe12.16.CreatingaSparseMatrix
Recipe12.17.GroupingRowsbyUnitsofTime
Recipe12.18.PerformingAggregationsoverDifferentGroups/Partitions
Simultaneously
Recipe12.19.PerformingAggregationsoveraMovingRangeofValues
Recipe12.20.PivotingaResultSetwithSubtotals
Chapter13.HierarchicalQueries
Recipe13.1.ExpressingaParent-ChildRelationship
Recipe13.2.ExpressingaChild-Parent-GrandparentRelationship
Recipe13.3.CreatingaHierarchicalViewofaTable
Recipe13.4.FindingAllChildRowsforaGivenParentRow




Recipe13.5.DeterminingWhichRowsAreLeaf,Branch,orRootNodes


Chapter14.Odds'n'Ends

Recipe14.1.CreatingCross-TabReportsUsingSQLServer'sPIVOTOperator

Recipe14.2.UnpivotingaCross-TabReportUsingSQLServer'sUNPIVOT

Operator

Recipe14.3.TransposingaResultSetUsingOracle'sMODELClause

Recipe14.4.ExtractingElementsofaStringfromUnfixedLocations

Recipe14.5.FindingtheNumberofDaysinaYear(anAlternateSolutionfor

Oracle)

Recipe14.6.SearchingforMixedAlphanumericStrings

Recipe14.7.ConvertingWholeNumberstoBinaryUsingOracle

Recipe14.8.PivotingaRankedResultSet

Recipe14.9.AddingaColumnHeaderintoaDoublePivotedResultSet

Recipe14.10.ConvertingaScalarSubquerytoaCompositeSubqueryinOracle

Recipe14.11.ParsingSerializedDataintoRows

Recipe14.12.CalculatingPercentRelativetoTotal

Recipe14.13.CreatingCSVOutputfromOracle

Recipe14.14.FindingTextNotMatchingaPattern(Oracle)

Recipe14.15.TransformingDatawithanInlineView

Recipe14.16.TestingforExistenceofaValueWithinaGroup

AppendixA.WindowFunctionRefresher

RecipeA.1.Grouping

RecipeA.2.Windowing

AppendixB.RozenshteinRevisited

RecipeB.1.Rozenshtein'sExampleTables

RecipeB.2.AnsweringQuestionsInvolvingNegation

RecipeB.3.AnsweringQuestionsInvolving"atMost"

RecipeB.4.AnsweringQuestionsInvolving"atLeast"

RecipeB.5.AnsweringQuestionsInvolving"Exactly"

RecipeB.6.AnsweringQuestionsInvolving"Any"or"All"

AbouttheAuthor

Colophon

Index


Copyright©2006O'ReillyMedia,Inc.Allrightsreserved.
PrintedintheUnitedStatesofAmerica.
PublishedbyO'ReillyMedia,Inc.,1005GravensteinHighway
North,Sebastopol,CA95472.
O'Reillybooksmaybepurchasedforeducational,business,or
salespromotionaluse.Onlineeditionsarealsoavailablefor
mosttitles(safari.oreilly.com).Formoreinformation,contact
ourcorporate/institutionalsalesdepartment:(800)998-9938or
corporate@oreilly.com.
Editor:

JonathanGennick

ProductionEditor:

DarrenKelly

ProductionServices:

nSight,Inc.

CoverDesigner:

KarenMontgomery

InteriorDesigner:

DavidFutato

PrintingHistory:



December2005:

FirstEdition.

NutshellHandbook,theNutshellHandbooklogo,andthe
O'ReillylogoareregisteredtrademarksofO'ReillyMedia,Inc.
TheCookbookseriesdesignations,SQLCookbook,theimageof
anAgamidlizard,andrelatedtradedressaretrademarksof
O'ReillyMedia,Inc.
Manyofthedesignationsusedbymanufacturersandsellersto


distinguishtheirproductsareclaimedastrademarks.Where
thosedesignationsappearinthisbook,andO'ReillyMedia,Inc.
wasawareofatrademarkclaim,thedesignationshavebeen
printedincapsorinitialcaps.
Whileeveryprecautionhasbeentakeninthepreparationofthis
book,thepublisherandauthorassumenoresponsibilityfor
errorsoromissions,orfordamagesresultingfromtheuseof
theinformationcontainedherein.
ISBN:0-596-00976-3
[M]




Dedication
Tomymom:
You'rethebest!Thankyouforeverything.



Preface
SQListhelanguageinthedatabaseworld.Ifyou'redeveloping
fororreportingfromrelationaldatabases,yourabilitytoput
dataintoadatabaseandthengetitbackoutagainultimately
comesdowntoyourknowledgeofSQL.Yetmanypractitioners
useSQLinaperfunctorymanner,andareunawareofthepower
attheirdisposal.Thisbookaimstochangeallthat,byopening
youreyestowhatSQLcanreallydoforyou.
Thebookyou'reholdinginyourhandsisacookbook.It'sa
collectionofcommonSQLproblemsandtheirsolutionsthatI
hopeyou'llfindhelpfulinyourday-to-daywork.Recipesare
categorizedintochaptersofrelatedtopics.Whenfacedwitha
newSQLproblemthatyouhaven'tsolvedbefore,findthe
chapterthatbestseemstoapply,skimthroughtherecipetitles,
andhopefullyyouwillfindasolution,oratleastinspirationfor
asolution.
Morethan150recipesareavailableinthis600-pluspagebook,
andI'veonlyscratchedthesurfaceofwhatcanbedoneusing
SQL.ThenumberofdifferentSQLsolutionsavailableforsolving
ourdailyprogrammingproblemsiseclipsedonlybythenumber
ofproblemsweneedtosolve.Youwon'tfindallpossible
problemscoveredinthisbook.Indeed,suchcoveragewouldbe
impossible.Youwill,however,findmanycommonproblemsand
theirsolutions.Andinthosesolutionslietechniquesthatyou'll
learnhowtoexpanduponandapplytoother,newproblems
thatIneverthoughttocover.

MypublisherandIareconstantlyonthelookoutfornew,cookbookworthySQLrecipes.IfyoucomeacrossagoodorcleverSQLsolution
toaproblem,considersharingit;considersendingitinforinclusionin
thenexteditionofthisbook.See"CommentsandQuestions"forour
contactinformation.


WhyIWroteThisBook
Queries,queries,queries.Mygoalfromthebeginningofthis
projecthasnotbeensomuchtowritea"SQLCookbook"asto
writea"QueryCookbook."I'veaimedtocreateabook
comprisedofqueriesrangingfromtherelativelyeasytothe
relativelydifficultinhopesthereaderwillgraspthetechniques
behindthosequeriesandusethemtosolvehisownparticular
businessproblems.IhopetopassonmanyoftheSQL
programmingtechniquesI'veusedinmycareersothatyou,
thereader,willtakethem,learnfromthem,andeventually
improveuponthem;throughthiscycleweallbenefit.Being
abletoretrievedatafromadatabaseseemssosimple,yetin
theworldofInformationTechnology(IT)it'scrucialthatthe
operationofdataretrievalbedoneasefficientlyaspossible.
Techniquesforefficientdataretrievalshouldbesharedsothat
wecanallbeefficientandhelpeachotherimprove.
Considerforamomenttheoutstandingcontributionto
mathematicsbyGeorgCantor,whowasthefirsttorealizethe
vastbenefitofstudyingsetsofelements(studyingthesetitself
ratherthanitsconstituents).Atfirst,Cantor'sworkwasn't
acceptedbymanyofhispeers.Intime,though,itwasnotonly
accepted,butsettheoryisnowconsideredthefoundationof
mathematics!Moreimportantly,however,itwasnotthrough
Cantor'sworkalonethatsettheorybecamewhatitistoday;
rather,bysharinghisideas,otherssuchasErnstZermelo,
GottlobFrege,AbrahamFraenkel,ThoralfSkolem,KurtGödel,
andJohnvonNeumanndevelopedandimprovedthetheory.
Suchsharingnotonlyprovidedeveryonewithabetter
understandingofthetheory,itmadeforabettersettheory
thanwasfirstconceived.


ObjectivesofThisBook
Ultimately,thegoalofthisbookistogiveyou,thereader,a
glimpseofwhatcanbedoneusingSQLoutsideofwhatis
consideredthetypicalSQLproblemdomain.SQLhascomea
verylongwayinthelasttenyears.Problemstypicallysolved
usingaprocedurallanguagesuchasCorJAVAcannowbe
solveddirectlyinSQL,butmanydevelopersaresimplyunaware
ofthisfact.Thisbookistohelpmakeyouaware.
Now,beforeyoutakewhatIjustsaidthewrongway,letme
statethatIamafirmbelieverin,"Ifitain'tbroke,don'tfixit."
Forexample,let'ssayyouhaveaparticularbusinessproblem
tosolve,andyoucurrentlyuseSQLtosimplyretrieveyourdata
whileapplyingyourcomplexbusinesslogicusingalanguage
otherthanSQL.Ifyourcodeworksandperformanceis
acceptable,thengreat.Iaminnowaysuggestingthatyou
scrapyourcodeforaSQL-onlysolution;Ionlyaskthatyou
openyourmindandrealizethattheSQLyouprogrammedwith
in1995isnotthesameSQLbeingusedin2005.Today'sSQL
candosomuchmore.


AudienceforThisBook
Thistextisuniqueinthatthetargetaudienceiswide,butthe
qualityofthematerialpresentedisnotcompromised.Consider
thatbothcomplexandsimplesolutionsareprovided,andthat
solutionsforfivedifferentvendorsareavailablewhena
commonsolutiondoesnotexist.Thetargetaudienceisindeed
wide:

TheSQLnovice
PerhapsyouhavejustpurchasedatextonlearningSQL,or
youarefreshintoyourfirstsemesterofarequired
databasecourseandyouwanttosupplementyournew
knowledgewithsomechallengingrealworldexamples.
Maybeyou'veseenaquerythatmagicallytransformsrows
tocolumns,orthatparsesaserializedstringintoaresult
set.Therecipesinthisbookexplaintechniquesfor
performingtheseseeminglyimpossiblequeries.

Thenon-SQLprogrammer
Perhapsyourbackgroundisinanotherlanguageandyou've
beenthrownintothefireatyourcurrentjobandare
expectedtosupportcomplexSQLwrittenbysomeoneelse.
Therecipesshowninthisbook,particularlyinthelater
chapters,breakdowncomplexqueriesandprovideagentle
walk-throughtohelpyouunderstandcomplexcodethatyou
mayhaveinherited.

TheSQLjourneyman


FortheintermediateSQLdeveloper,thisbookisthegoldat
theendoftherainbow(OK,maybethat'stoostrong;please
forgiveanauthor'senthusiasmforhistopic).Inparticular,if
you'vebeencodingSQLforquitesometimeandhavenot
foundyourwayontowindowfunctions,you'reinforatreat.
Forexample,thedaysofneedingtemporarytablestostore
intermediateresultsareover;windowfunctionscangetyou
toananswerinasinglequery!Allowmetoagainstatethat
Ihavenointentionoftryingtoforce-feedmyideastoan
alreadyexperiencedpractitioner.Instead,considerthisbook
asawaytoupdateyourskillsetifyouhaven'tcaughtonto
someoftheneweradditionstotheSQLlanguage.

TheSQLexpert
Undoubtedlyyou'veseentheserecipesbefore,andyou
probablyhaveyourownvariations.Why,then,isthisbook
usefultoyou?Perhapsyou'vebeenaSQLexpertonone
platformyourwholecareer,say,SQLServer,andnowwish
tolearnOracle.Perhapsyou'veonlyeverusedMySQL,and
youwonderwhatthesamesolutionsinPostgreSQLwould
looklike.Thistextcoversdifferentrelationaldatabase
managementsystems(RDBMSs)anddisplaystheir
solutionssidebyside.Here'syourchancetoexpandyour
knowledgebase.




HowtoUseThisBook
Besuretoreadthisprefacethoroughly.Itcontainsnecessary
backgroundandotherinformationthatyoumightotherwise
missifyoudiveintoindividualrecipes.Thesectionon"Platform
andVersion"tellsyouwhatRDBMSsthisbookcovers.Pay
specialattentionto"TablesUsedinThisBook,"sothatyou
becomefamiliarwiththeexampletablesusedinmostofthe
recipes.You'llalsofindimportantcodingandfontconventions
in"ConventionsUsedinThisBook."Allthesesectionscome
laterinthispreface.
Rememberthatthisisacookbook,acollectionofcode
examplestouseasguidelinesforsolvingsimilar(oridentical)
problemsthatyoumayhave.DonottrytolearnSQLfromthis
book,atleastnotfromscratch.Thisbookshouldactasa
supplementto,notareplacementfor,acompletetexton
learningSQL.Additionally,followingthetipsbelowwillhelpyou
usethisbookmoreproductively:
Thisbooktakesadvantageofvendor-specificfunctions.SQL
PocketGuidebyJonathanGennickhasallofthemandis
convenienttohaveclosetoyouincaseyoudon'tknow
whatsomeofthefunctionsinmyrecipesdo.
Ifyou'veneverusedwindowfunctions,orhavehad
problemswithqueriesusingGROUPBY,readAppendixA
first.ItwilldefineandprovewhatagroupisinSQL.More
importantly,itgivesabasicideaofhowwindowfunctions
work.WindowfunctionsareoneofthemostimportantSQL
developmentsofthepastdecade.
Usecommonsense!Realizethatitisimpossibletowritea
bookthatprovidesasolutiontoeverypossiblebusiness


probleminexistence.Instead,usetherecipesfromthis
bookastemplatesorguidelinestoteachyourselfthe
techniquesrequiredtosolveyourownspecificproblems.If
youfindyourselfsaying,"Great,thisrecipeworksforthis
particulardataset,butmineisdifferentandthustherecipe
doesn'tworkquitecorrectly,"that'sexpected.Inthatcase,
trytofindcommonalitybetweenthedatainthebookand
yourdata.Breakdownthebook'squerytoitssimplestform
andaddcomplexityasyougo.Allqueriesstartwith
SELECT…FROM…,sointheirsimplestform,allqueriesare
thesame.Ifyouaddcomplexityasyougo,"building"a
queryonestep,onefunction,onejoinatatime,youwill
notonlyunderstandhowthoseconstructschangetheresult
set,butyouwillseehowtherecipeisdifferentfromwhat
youactuallyneed.Andfromthereyoucanmodifythe
recipetoworkforyourparticulardataset.
Test,test,andtest.Undoubtedlyanytableofyoursis
biggerthanthe14rowEMPtableusedinthisbook,so
pleasetestthesolutionsagainstyourdata,attheveryleast
toensurethattheyperformwell.Ican'tpossiblyknowwhat
yourtableslooklike,whatcolumnsareindexed,andwhat
relationshipsarepresentinyourschema.Soplease,donot
blindlyimplementthesetechniquesinyourproductioncode
untilyoufullyunderstandthemandhowtheywillperform
againstyourparticulardata.
Don'tbeafraidtoexperiment.Becreative!Feelfreetouse
techniquesdifferentfromwhatI'veused.Imakeitapoint
tousemanyofthefunctionssuppliedbythedifferent
vendorsinthisbook,andoftenthereareseveralother
functionsthatmayworkaswellastheoneI'vechosento
useinaparticularrecipe.Feelfreetoplugyourown
variationsintotherecipesofthisbook.
Newerdoesnotalwaysmeanbetter.Ifyou'renotusing


someofthemorerecentfeaturesoftheSQLlanguage(for
example,windowfunctions),thatdoesnotnecessarily
meanyourcodeisnotasefficientasitcanbe.Thereare
manycasesinwhichtraditionalSQLsolutionsareasgood
orbetterthananynewsolution.Pleasekeepthisinmind,
particularlyintheAppendixB,RozenshteinRevisited.After
readingthisbook,youshouldnotcomeawaywiththeidea
thatyouneedtoupdateorchangeallyourexistingcode.
Instead,onlyrealizetherearemanynewandextremely
efficientfeaturesofSQLavailablenowthatwerenot
available10yearsago,andtheyareworththetimetaken
tolearnthem.
Don'tbeintimidated.Whenyougettothesolutionsection
ofarecipeandaquerylooksimpossibletounderstand,
don'tfear.I'vegonetogreatlengthstonotonlybreakdown
eachquerystartingfromitssimplestform,buttoshowthe
intermediateresultsofeachportionofaqueryaswework
ourwaytothecompletesolution.Youmaynotbeableto
seethebigpictureimmediately,butonceyoufollowthe
discussionandseenotonlyhowaqueryisbuilt,butthe
resultsofeachstep,you'llfindthatevenconvoluted-looking
queriesarenothardtograsp.
Programdefensivelywhennecessary.Inanefforttomake
thequeriesinthisbookasterseashumanlypossible
withoutobscuringtheirmeaning,I'veremovedmany
"defensivemeasures"fromtherecipes.Forexample,
consideraquerycomputingarunningtotalforanumberof
employeesalaries.Itcouldbethecasethatyouhave
declaredthecolumnoftypeVARCHARandare(sadly)
storingamixofnumericandstringdatainonefield.You'll
findtherunningtotalrecipeinthisbookdoesnotcheckfor
suchacase(anditwillfailasthefunctionSUMdoesn't
knowwhattodowithcharacterdata),soifyouhavethis
typeof"data"("problem"isamoreaccuratedescription),
youwillneedtocodearounditor(hopefully)fixyourdata,


becausetherecipesprovideddonotaccountforsuch
designpracticesasthemixingofcharacterandnumeric
datainthesamecolumn.Theideaistofocusonthe
technique;onceyouunderstandthetechnique,
sidesteppingsuchproblemsistrivial.
Repetitionisthekey.Thebestwaytomastertherecipesin
thisbookistositdownandcodethem.Whenitcomesto
code,readingisfine,butactuallycodingisevenbetter.You
mustreadtounderstandwhythingsaredoneacertainway,
butonlybycodingwillyoubeabletocreatethesequeries
yourself.
Beadvisedthatmanyoftheexamplesinthisbookare
contrived.Theproblemsarenotcontrived.Theyarereal.
However,I'vebuiltallexamplesaroundasmallsetoftables
containingemployeedata.I'vedonethattohelpyouget
familiarwiththeexampledata,sothat,havingbecomefamiliar
withthedata,youcanfocusonthetechniquethateachrecipe
illustrates.Youmightlookataspecificproblemandthink:"I
wouldneverneedtodothatwithemployeedata."Buttryto
lookpasttheexampledatainthosecasesandfocusonthe
techniquethatI'millustrating.Thetechniquesareuseful.My
colleaguesandIusethemdaily.Wethinkyouwilltoo.


What'sMissingfromThisBook
Duetoconstraintsontimeandbooksize,itisn'tpossiblefora
singlebooktoprovidesolutionsforallthepossibleSQL
problemsyoumayencounter.Thatsaid,herearesome
additionalitemsthatdidnotmakethelist:

DataDefinition
AspectsofSQLsuchascreatingindexes,adding
constraints,andloadingdataarenotcoveredinthisbook.
Suchtaskstypicallyinvolvesyntaxthatishighlyvendorspecific,soyou'rebestoffreferringtovendormanuals.In
addition,suchtasksdonotrepresentthetypeof"hard"
problemforwhichonewouldpurchaseabooktosolve.
Chapter4,however,doesproviderecipesforcommon
problemsinvolvingtheinsertion,updating,anddeletingof
data.

XML
ItismystrongopinionthatXMLrecipesdonotbelongina
bookonSQL.StoringXMLdocumentsinrelational
databasesisbecomingincreasinglypopular,andeach
RDBMShastheirownextensionsandtoolsforretrieving
andmanipulatingsuchdata.XMLmanipulationoften
involvescodethatisproceduralandthusoutsidethescope
ofthisbook.RecentdevelopmentssuchasXQUERY
representcompletelyseparatetopicsfromSQLandbelong
intheirownbook(orbooks).


Object-OrientedExtensionstoSQL
Untilalanguagemoresuitablefordealingwithobjects
comesalong,Iamstronglyagainstusingobject-oriented
featuresanddesignsinrelationaldatabases.Atthepresent
time,theobject-orientedfeaturesavailablefromsome
vendorsaremoresuitableforuseinprocedural
programmingthaninthesortofsetorientedproblemsolvingforwhichSQLisdesigned.

DebatesonPointsofTheory
Youwon'tfindargumentsinthisbookaboutwhetherSQLis
relational,oraboutwhetherNULLvaluesshouldexist.
Thesesortoftheoreticaldiscussionshavetheirplace,but
notinabookcenteredondeliveringSQLsolutionstoreallifeproblems.Tosolvereal-lifeproblems,yousimplyhave
toworkwiththetoolsavailabletoyouatthetime.Youhave
todealwithwhatyouhave,notwhatyouwishyouhad.

Ifyouwishtolearnmoreabouttheory,anyof
ChrisDate's"RelationalDatabaseWritings"
bookswouldbeagoodstart.Youmightalso
pickupacopyofhismostrecentbook,
DatabaseinDepth(O'Reilly).

VendorPolitics
ThistextprovidessolutionsforfivedifferentRDBMSs.Itis
onlynaturaltowanttoknowwhichvendor'ssolutionis
"best"or"fastest."Thereisplentyofinformationthateach


vendorwouldgladlyprovidetoshowthattheirproductis
"best";Ihavenointentionofdoingsohere.

ANSIPolitics
Manytextsshyawayfromtheproprietaryfunctions
suppliedbydifferentvendors.Thistextembraces
proprietaryfunctions.Ihavenointentionofwriting
convoluted,poorlyperformingSQLcodesimplyforthesake
ofportability.Ihaveneverworkedinanenvironmentwhere
theuseofvendor-specificextensionswasprohibited.You
arepayingforthesefeatures;whynotusethem?
Vendorextensionsexistforareason,andmanytimesoffer
betterperformanceandreadabilitythanyoucould
otherwiseachieveusingstandardSQL.IfyoupreferANSIonlysolutions,fine.AsImentionedbefore,Iamnothereto
tellyoutoturnallyourcodeupsidedown.Ifwhatyouhave
isstrictlyANSIanditworksforyou,great.Whenitcomes
downtoit,weallgotowork,weallhavebillstopay,and
weallwanttogohomeatareasonabletimeandenjoy
what'sstillleftofourdays.So,I'mnotsuggestingthat
ANSI-onlyiswrong.Dowhatworksandisbestforyou.But,
Iwanttomakeclearthatifyou'relookingforANSI-only
solutions,youshouldlookelsewhere.

LegacyPolitics
Therecipesinthistextmakeuseofthenewestfeatures
availableatthetimeofwriting.Ifyouareusingoldversions
oftheRDBMSsthatIcover,manyofmysolutionswill
simplynotworkforyou.Technologydoesnotstandstill,
andneithershouldyou.Ifyouneedoldersolutions,you'll
findthatmanyoftheSQLtextsavailablefromyearspast


haveplentyofexamplesusingolderversionsofthe
RDBMSscoveredinthisbook.




StructureofThisBook
Thisbookisdividedinto14chaptersand2appendices:
Chapter1,RetrievingRecords,introducesverysimple
queries.ExamplesincludehowtouseaWHEREclauseto
restrictrowsfromyourresultset,providingaliasesfor
columnsinyourresultset,usinganinlineviewtoreference
aliasedcolumns,usingsimpleconditionallogic,limitingthe
numberofrowsreturnedbyaquery,returningrandom
records,andfindingNULLvalues.Mostoftheexamplesare
verysimple,butsomeofthemappearinmorecomplex
recipes,soit'sagoodideatoreadthischapterifyou're
relativelynewtoSQLoraren'tfamiliarwithanyofthe
exampleslistedforthischapter.
Chapter2,SortingQueryResults,introducesrecipesfor
sortingqueryresults.TheORDERBYclauseisintroduced
andisusedtosortqueryresults.Examplesincreasein
complexityrangingfromsimple,single-columnordering,to
orderingbysubstrings,toorderingbasedonconditional
expressions.
Chapter3,WorkingwithMultipleTables,introducesrecipes
forcombiningdatafrommultipletables.Ifyouarenewto
SQLorareabitrustyonjoins,Istronglyrecommendyou
readthischapterbeforereadingChapter5andlater.Joining
tablesiswhatSQLisallabout;youmustunderstandjoins
tobesuccessful.Examplesinthischapterinclude
performingbothinnerandouterjoins,identifyingCartesian
productions,basicsetoperations(setdifference,union,
intersection),andtheeffectsofjoinsonaggregate
functions.


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

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

×

×