1. Trang chủ
  2. » Công Nghệ Thông Tin

DEVELOPING TIME-ORIENTED DATABASE APPLICATIONS IN SQL ppt

528 263 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Developing Time-Oriented Database Applications in SQL
Tác giả Richard T. Snodgrass
Trường học Morgan Kaufmann Publishers
Chuyên ngành Data Management Systems
Thể loại giáo trình
Thành phố San Francisco
Định dạng
Số trang 528
Dung lượng 4,77 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Twodecadesofresearchintotemporaldatabaseshaveunequivocallyshownthat a time-var ying table, containing certain kinds of DATE columns, is a completely different animal thanits cousin, thet

Trang 1

Time-Oriented

Trang 2

DATA MANAGEMENT SYSTEMS

SeriesEditor,JimGray

DevelopingTime-OrientedDatabaseApplicationsin

EditedbyAhmedElmagarmid,Marek

Rusinkiewicz,andAmitSheth

Object-RelationalDBMSs:TrackingtheNextGreat

Wave,SecondEdition

MichaelStonebrakerandPaulBrownwith

CynthiaMaroSaracco

ReadingsinDatabaseSystems,ThirdEdition

EditedbyMichaelStonebrakerandJosephM

ClementT.Y andWeiyiMeng

TheObjectDatabaseStandard:ODMG2.0

EditedbyR.G.G.CattellandDouglasK.Barr y

AdvancedDatabaseSystems

CarloZaniolo,StefanoCeri,ChristosFaloutsos,

RichardT.Snodgrass,V.S.Subrahmanian,and

RobertoZicari

PrinciplesofTransactionProcessing

PhilipA.BernsteinandEricNewcomer

UsingtheNewDB2:IBM'sObject-RelationalDatabase

System

DistributedAlgorithms

NancyA.Lynch

ActiveDatabaseSystems:TriggersandRulesFor

AdvancedDatabaseProcessing

EditedbyJenniferWidomandStefanoCeri

JoeCelko'sSQLforSmar ties:AdvancedSQL

Programming

JoeCelko

MigratingLegacySystems:Gateways,Inter faces,&the

IncrementalApproach

MichaelL.BrodieandMichaelStonebraker

Database:Principles,Programming,andPer for mance

PatrickO'Neil

AtomicTransactions

NancyLynch,MichaelMerritt,WilliamWeihl,and

AlanFekete

Quer yProcessingforAdvancedDatabaseSystems

EditedbyJohannChristophFreytag,DavidMaier,

andGottfriedVossen

TransactionProcessing:ConceptsandTechniques

JimGrayandAndreasReuter

UnderstandingtheNewSQL:ACompleteGuide

JimMeltonandAlanR.Simon

BuildinganObject-OrientedDatabaseSystem:The

EditedbyAhmedK.Elmagarmid

AGuidetoDevelopingClient/Ser verSQLApplications

SetragKhoshaan,Ar volaChan,AnnaWong,and

Harr yK.T.Wong

TheBenchmarkHandbookforDatabaseand

TransactionProcessingSystems,SecondEdition

EditedbyJimGray

CamelotandAvalon:ADistributedTransaction

Facility

EditedbyJeffreyL.Eppinger,LilyB.Mummert,

andAlfredZ.Spector

ReadingsinObject-OrientedDatabaseSystems

EditedbyStanleyB.ZdonikandDavidMaier

Trang 4

DirectorofProduction&Manufacturing YonieOverton

EditorialCoordinator BelindaBreyer

CoverIllustration MichaelBloomenfeld,AEC

TextDesign MarkOng,SideBySideStudios

TechnicalIllustration CheriePlumlee

Chapteropenerartbasedonillustrationsfromthefollowingsources:Chapters1,12,and13fromBrackin,A.J.,

Clocks:ChroniclingTime,EncyclopediaofDiscover yandInvention,LucentBooks,1991;Chapters2,3,4,5,and

6fromWoodward,P MyOwnRightTime:AnExplorationofClockworkDesign,OxfordUniversityPress,1995;

Chapters7,8,and9fromBritton,F.J.,TheEscapements:TheirAction,Constr uction,andPropor tion,Geo.K.Hazlitt

&Co.,reprintedbyArlingtonBookCo,1984;andChapters10and11fromHeadrick,M.V.,ClockandWatch

EscapementMechanics,self-published,1997

Designationsusedbycompaniestodistinguish theirproductsareoftenclaimedastrademarksorregistered

trademarks.InallinstanceswhereMorganKaufmannPublishersisawareofaclaim,theproductnamesappear

ininitialcapitalorallcapitalletters.Readers,however, shouldcontacttheappropriate companiesformore

completeinformationregardingtrademarksandregistration

MorganKaufmannPublishers

EditorialandSalesOfce

340PineStreet,SixthFloor

Ordertollfree 800-745-7323

c 2000byMorganKaufmannPublishers

Allrightsreser ved

PrintedintheUnitedStatesofAmerica

04 03 02 01 00 5 4 3 2 1

Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformorbyany

means—electronic,mechanical,photocopying,recording,orother wise—withoutthepriorwrittenpermission

Trang 5

Ce jour, et toujours

Trang 6

Dolikethesundial:

Countonlythebrighthours

—Ger manproverb

Trang 7

MicrosoftResearch

Precise clocksweredevelopedso thatseafarerscould ndtheir longitude.Precise

temporaldatatechniqueswererecentlydevelopedtohelpdatabasedesignersrecord

andreasonabouttemporalinformation.Itisparadoxicalthatweareonlynow

com-ingtounderstandhowtothinkabouttimeandrepresentitinformalsystems.After

all,timeisthefourthdimension;itisatthecoreofexistence.Yet,itisonlyrecently

thatwehavecometounderstandthefundamentalconceptsofinstants,inter vals,

periods,sequencedchanges,validtime,transactiontime,andabitemporalviewof

information

RichardSnodgrassandhiscolleagueshaveexploredtemporaldataconceptsover

the lasttwodecades Theynow have afairly complete solutiontotheproblems

IndeedtheconceptsarenowbeingaddedtotheSQLlanguagestandard.Thisbook

summarizestheirworkandpresentsitinaver yaccessibleandusefulway

Temporaldatabases,viewedfromthismodernperspective,aresurprisingly

sim-pleandpowerful.Thebookgivesexamplesof85-lineSQLprogramsthatcollapse

to3-lineprogramswhenthenewconceptsareapplied.Itintroducestheconcepts

using concrete examplesand conventional SQL I found this mix of theor yand

practicever yinstructiveandver yeasytofollow

The bookexplainsthattemporaldatabases canbe designedintwo steps.First,

the staticdatabasecan be designed Then,ina secondpass,each tableand

con-straint is given its temporal attributes This makes design much more tractable

This approach is made allthe more attractiveby thefact that thetemporal SQL

language extensionsarejustmodierstostandard queriesandupdates—thisver y

elegantapproachmakestemporalissuesorthogonaltotheotherlanguageissues

I highly recommendthis book to anyone interested in temporaldata—either

designingandbuildingdatabasesthatrecordinformationovertime,orjust

under-standingtheconceptsthatunderlierepresentingtemporalinformation.Thisbook

Trang 9

OracleCorporation

It's about time—time that a book like this was written and time that the SQL

communitygotthebenetsofthecarefulanalysisandthoughtputintothesubject

Rick Snodgrass is oneof therelativelyfew researchers intheeld of temporal

databases and has provedhimself tobe oneof themoreimportant of thosefew,

in partbecause heinsistsonapplyingthetheoretical knowledge gainedfrom his

researchtopracticalapplicationsandtorealproducts

Snodgrass proposedin 1992 thattemporalextensions toSQL bedevelopedby

thetemporaldatabasecommunity.Inresponsetothisproposal,avirtual

commit-teewasformedtodesignextensionstothe1992editionoftheSQLstandard(ANSI

X3.135.-1992 and ISO/IEC 9075:1992); thoseextensions, known as TSQL2, were

developed during 1993 by this committee meeting only via email In late 1993,

Snodgrass rstpresentedthisworktothegroupresponsible fortheAmerican

Na-tionalStandardforDatabaseLanguageSQL,ANSITechnicalCommitteeX3H2(now

knownasNCITSH2)

InresponsetoSnodgrass'spresentation,X3H2proposedtotheInternational

Or-ganization for Standardization (ISO) that theproject to extend the standard for

SQLbeenhancedbyaddingasubprojectfortemporalextensionstothelanguage

This proposalwasacceptedin1994,andaninitial documentforISO/IEC9075-7,

known asSQL/Temporal,wasstarted.Overthenexttwoyears, aseriesof

propos-als from Snodgrass and others were considered by theISO groupresponsible for

SQL (ISO/IECJTC1/SC21/WG3,later ISO/IECJTC1/SC32/WG3),but progresswas

slowedconsiderablybytheneedtofocusonwhathas recentlybeenpublishedas

SQL:1999 Workwill undoubtedly resume onprogressing SQL/Temporalin 1999

forpublicationearlyinthenextmillennium,andSnodgrass willnodoubtplaya

Trang 10

Thebook youholdhas beenalongtimein themaking,notonlybecausethe

subject matter can seem over whelminglycomplex if not presentedcarefully, but

alsobecauseof thegreatnumber ofexamplesthat Snodgrasshas takenfrom real

applicationsystemsandtranslatedintostandardSQLanditsproposedextensions

(Ofcourse,notalloftheexamplescanbeusedinallSQLproductstoday;someof

themaredirectedtowardspecicvendors'systems,whileothersdependonfuture

extensionstothelanguage.)Theresultofthatcareandextensiveuseofexamples

isgreat clarityandfocus,yieldingready comprehensiontoreaders willingtogive

thebooktheattention itdeser ves.Irecommendthisbookver y highlytoallSQL

practitioners,especiallythosewithaninterestinthetemporalsemanticsofdata

Trang 11

ForewordbyJimGray vii

ForewordbyJimMelton ix

2.1 Valid-TimeStateTables 12

2.2 Transaction-TimeStateTables 18

Trang 12

6 QueryingStateTables 143

6.1 ExtractingtheCurrentState 143

Trang 13

8 RetainingaTrackingLog 219

8.1 DeningtheTrackingLog 220

Trang 14

11 TemporalDatabaseDesign 343

11.1 ProperlySequencingtheDesign 343

Trang 15

12.5 DeningValid-TimeStateTables 406

12.6 Quer yingStateTables 411

12.7 ModifyingStateTables 416

12.8 RetainingaTrackingLog 421

12.9 Transaction-TimeStateTables 426

AbouttheAuthor 502

AbouttheCD-ROM 503

Trang 17

Wedevelopadatabaseapplication,andinitially theprojectproceedssmoothly

enough.Therearealternativestoweighduringtheschemadesign,problemsto

con-tendwithwhilewritingtheSQLcode,andconstantrecongurationandinteraction

withotherprogramsandlegacydata,butallinalltheprojectisundercontrol.Then

wedecidethatoneofthetablesneedsanotherDATEcolumn,recordingwhenthe

row wasvalid.(Afterall,we addedabirthdatecolumna fewweeksago,with no

surprises.)Sowereworkthepartoftheapplicationthatmaintainsthattable,

notic-ingthatthecodeisgettingmorecomplicated.Duringtesting,wediscoverthatthe

primar ykeynolongerissufcient.WeaddtheDATEcolumntotheprimar ykey,

acknowledging thatthisisonlyastopgap measure,and hopethattheinputdata

will bewellformed,asthereisn'ttimetowritecodethatchecksthoseconstraints

properly In theback of our mindis thelingering doubt thatperhaps referential

integritycheckingisn'tworkingquiterighteither

We soon realize that we needanother DATE column torecord when therow

was nolongervalid.Indoingsoweencounteraraftofoff-by-onebugs, inwhich

someless-thancomparisonsshouldhavebeen`<=',andotherplaceswhereweneed

to add“+ 1DAY” We thinkwe've foundall thecodelocationsthat needtobe

changed, butwe're not sure Andwe now know fora fact that the primar yand

foreignkeysarewrong,butwedon'tknowhowtoevenapproachthatmess

The code to modify the database is becoming increasingly convoluted Each

modication has to at leastconsider changingtheDATEcolumns, but itisn't at

allclearhowtoapproachsuchchangesinasystematicfashion.Andeventhemost

trivialqueries,suchas“WhowasAaron'smanagerwhenheworkedontheCapital

account?”, whichbeforewecouldcodeinoursleep,nowbecomepainful toeven

contemplatewritinginSQL

Around this time, users start complaining that reports aren't consistent, that

Trang 18

andthedatahadbeenchangedinthemeantime.Wethenrealizethatthereisno

way tocorrelate theend-of-the-yearreport withthecashowreport,unless they

arerunatthesametime.Usersareadoptinganirreverentviewofthesereports:if

youwaitafewdays,maybethenumberswillxthemselves

Toaddresstheinconsistenciesinthereports,someonesuggestsaquickx:add

anotherDATEcolumn.Thedevelopment grouprespondswithastonishmentand

chagrin How can we possibly get thecode working with another DATE column,

when we all know how much work resulted from addingthe previous column?

In fact, some in the group despair of ever getting the code as is, with just two

DATEcolumns,workingcorrectly—therearejusttoomanyarbitrar ydecisions,each

layeredonotherequallyill-motivatedquickxes

Lookingbackonthehistor yofthedevelopmentprocess,ever yonehasavague

ideathattheproblemsstartedwhenthatpeskyDATEcolumnwasrstadded.How

couldonecolumnummoxthewholesystem?Andwhydosomecolumns,such

asthebirthdatecolumn,slideinsmoothly,andotherDATEcolumnscausenoend

ofproblems?

APARADIGM SHIFT

ThomasKuhn,inhisinsightfulandhighlyinuentialbook,TheStr uctureof

Scien-ticRevolutions[64],arguedthatsciencedoesnotproceedinalinear,monotonic

ac-cumulationofknowledge,butratherexhibitsintellectuallyjarringdiscontinuities,

asradical ideasbecometheestablished worldview,replacingthenow-discredited

priorconceptualfoundation

Twodecadesofresearchintotemporaldatabaseshaveunequivocallyshownthat

a time-var ying table, containing certain kinds of DATE columns, is a completely

different animal thanits cousin, thetable withoutsuch columns.Effectively

de-signing, quer ying, and modifying time-var ying tables requires a different set of

approaches and techniques than the traditional onestaught in databasecourses

and trainingseminars Developersarenaturallyunaware of theseresearch results

(andresearchers areoftencluelessastotherealitiesofreal-worldapplications

de-velopment).Assuch,developersoftenreinventconceptsandtechniqueswithlittle

knowledgeoftheelegantconceptualframeworkthathasevolvedandrecently

con-solidated,andresearchers continuetoconcealthisframework withoverlyformal

prose,neverbotheringtomaketheconnectionwithexistingtoolsathand

This book is an attempt to recast theinsights from some 1600 papers inthe

researchliteratureintotermsusablebythosebraveSQLapplicationcodersworking

inthetrenches.Theseconceptsareintegratedwiththestate-of-the-art approaches

utilizedbyfor ward-thinkingdevelopers,asshowcasedinthecasestudiesthatform

Trang 19

wethinkabouttime-var yingdata.Thisshiftimpactshowsuchtablesarespecied,

howtheyaremaintained,andhowtheyarequeried

PREREQUISITES

I assume you are comfortable with the SQL quer y language This book is not a

primer on thatlanguage, thoughI docoverthetemporal datatypesand

tempo-ral constructs of SQL-92in depth There are many excellent books that ser ve as

introductionstoSQL

Ithelpsifyouhaveimplementedanapplicationinvolvingtime-var yingdata,if

onlytorealizersthandhowdifcultandconfusingsuchaprojectcanbe,andthus

toappreciatethedegreetowhichtheapproachpresentedherehelpsclearoutthe

undergrowth andachieveanelegantandunfettered design.Onechapterassumes

familiarity withtheentity-relationship model;therest of thebookfocuses solely

ontherelationalmodel

Theconceptualtoolsintroducedhereareinaspecicandfundamentalway

ex-tensionsofexistingstrategies,soever ythingyou'velearneduntilnow(well,almost

ever ything) willbeusefulinthisbravenewworld.Thehardestpart,forwhichI'll

provide careful guidance,istojettisonthenotionthat thisDATEcolumn“isjust

anothercolumn.”Operatingundertheoldassumptionsunhappilydoesn'twork,as

project afterprojectafterprojecthasshown.Paradigmshifts arealwaysscar y,but

thebenetsarethereforthosewillingtomakethejump

WHATTO READ

The best waytounderstand theprinciplesof time-var yingapplications andtheir

expressioninSQListoworkthroughaseriesoftangible examples.Byexamining

thedesignissuesthatariseandthekindsofconstraints,queries,andmodications

thatwewishtoexpressinimplementingthesespecicapplications,youwillgain

anappreciationoftheabstractprinciplesat play.Forthisreason,thebulkof this

bookiscomprisedofcasestudies

Eachcasestudysetsthestagewithadiscussionoftheapplicationdomain,which

includes oileldrecords,cattlelocation information,andcadastraldata The

rel-evanttablesareintroduced,followedbyadiscussionofthedesign,quer ying,and

modicationofthese(time-var ying)tables.Whiletheapplicationsandthepeople

mentionedinthecasestudiesallexist,thespecicSQLexampleshavebeentailored

tobringouttheissuesunderdiscussion

The case studies were easy tolocate It seems that most databaseapplications

involve time-var ying data Indeed, applications that are inherently not temporal

Trang 20

unintentionallyemphasizestheinherentcomplexityoftime-var yingapplications

To understand the fundamental concepts, you are encouraged to read all the

chapters,evenifyouaren'tanoileldengineeroraveterinarian.Eachcasestudy

bringsoutanewcategor yoftemporaldata,withitsuniquecharacteristicsand

de-mands.Infact, bystudying other elds,youarerelievedoftheminutiaeof your

current environment.By studying aforeign language orculture, a deeper

under-standing of yourownlanguage orculture oftenfollowsas anadditional,oreven

sometimesprimar y,benet.After youhavereadthebook,aproductiveapproach

toaddressanewsetofrequirementsistoask,Towhichcasestudyistheapplication

underdevelopmentmostcloselyrelated?Thentherelevantcodefragmentscanbe

customizedtotheproblemathand

Afew sectionsaremarkedwith anasterisk toindicateadvancedmaterial Feel

freetoskipthesesectionsonarst,orevensecond,reading

CASESTUDIES

Betting thebook's categorization as nonction, thepeople and their situations

areasdescribedherein.Thespecicsoftheir solutionstotheproblemspresented

bytime-var yingdatahavebeenadaptedtobetterillustrategeneralapproachesthat

Iwishtoemphasize.MostoftheSQLcodewaswrittenbyuseforthebook,butitis

reminiscentof thatappearingintheactual applications.Inthediscussion,Ihave

attemptedtonotoversimplify.Muchofthecomplexity inherentinthese

applica-tions is cleverlyhidden inthe details,and any realistic solutionmust ultimately

confronttheenterpriseinallitsglor yandintricacy

CD-ROM

The includedCD-ROM containsthecode fragmentsimplementedina variety of

commercialsystems,includingIBMDB2UniversalDatabase(UDB),Ingres,

Inform-ix–UniversalSer ver,MicrosoftAccess,MicrosoftSQLSer ver,SybaseSQLSer ver,

Ora-cle8Ser ver,andUniSQL Whilethesecodefragmentshavebeentested,theauthor

andthepublishermakenoclaimsastothesuitabilityorcorrectnessofthesecode

Trang 21

theauthoratsnodgrass@mkp.com

The specic content of thisbook, aswell as its overallcomposition, came about

throughcollaborationwithmydearfriendandcolleagueChristianS.Jensen,who

hasassembledanddirectstheworld'sstrongesttemporaldatabaseresearchgroupat

theUniversityofAalborginDenmark.Heispresent onever ysinglepage,looking

overmyshoulder,probing,clarifying,generalizing,exemplifying.Whilethewords

inthisbookaremine(well,exceptfor“exemplifying,”whichisdenitelyoneofhis

words!),theideasarejointlyours,comingledwith thecontributionsofthemany

authors discussed in the “Readings” sections Research is primarily an excuse to

play with ideas, and concepts provide opportunities to interact on a deep level

withothers.MuchofthejoyIhaveexperiencedinthiscrazybusinesshasdirectly

orindirectly involvedChristian,whetherintheheadyprobingof tr yingtogetat

coretruths,orinappreciatingtheshimmeringsimplicityoftheresultinginsights

that,onceuncovered,areinretrospectobvious

Manyothershelpedwiththecasestudies,testedthecodefragments,readdrafts

ofthisbook,andofferednontechnicalsustenance

Cher ylBach,JimBarnett,NigelCorbin,BradDeGroot,JensGadgaard,andChris

Jantonweregenerouswiththeirtimeinexplainingthedetailsoftheirapplications,

whichprovidedtheimpetusforthecasestudies

I thank Anindya Datta, Igor Viguier, and theDepartment of Information

Sys-temsattheUniversityofArizonaforaccesstotheSybaseDBMS.CliffLeungfrom

IBM, Paul Brown from Informix, Nick Kline and Goetz Graefe from Microsoft,

Ra Ahmed from Oracle, and JackReid from UniSQL Customer Support Ser vices

providedhelpwiththeirrespectiveDBMSproducts

I appreciate comments on the manuscript from Ra Ahmed, John Bair, Paul

Brown, Jim Gray, Brad De Groot, Jeff Gross, Robert Brett Gulledge, Christian S

Jensen,andDennisShasha.JimMeltonwentaboveandbeyondthecallofdutyin

providingtwodetailedreviewsoftheentiremanuscript

Amad Arsalan, Scott Calvert, Wen-Ke Chen, Brad De Groot, Alvin Gendrano,

Bruce Huang, Vijaykumar Immanuel, Jie Li, Wei Li, Giedrius Slivinskas, Helen

Thomas, Brad Traweek, and In´es F Vega-Lopez´ provided corrections to the code

fragments.AlanBrucksprovidedinformationontheyear2000problem

I thankthefollowing peoplewho created thecontent of theCD-ROM:HTML

templates (Christopher Cooper, Jose Alvin Gendrano, Rachana Shah, and Jian

Yang),datatypes:IBMDB2UDB(BradTraweekandGiedriusSlivinskas),Informix–

Universal Ser ver (JasonCoxand AnandWagle),Ingres (Wen-KeChen),Microsoft

Trang 22

(Ines´ F.Vega-L´opezand Giedrius Slivinskas), Sybase SQLSer ver(Robert Brett

Gul-ledgeandMiltosVaadis),Oracle8Ser ver(ChristopherCooperandJoseAlvin

Gen-drano), and UniSQL (Qing Yan); valid-time state tables: IBMDB2 UDB

(Vijayku-mar Immanuel and Giedrius Slivinskas), Informix–Universal Ser ver (Jason Cox),

Microsoft Access (Ahmad Arsalan and Ines´ F Vega-L´opez), Microsoft SQL Ser ver

(Ines´ F.Vega-L´opezandGiedriusSlivinskas),SybaseSQLSer ver(WenminChen),

Or-acle8Ser ver(JoseAlvinGendrano,BruceHuang,andWeiLi),andUniSQL(Lincoln

Turnerand CarlosUgarte);temporaljoinandcoalescing: Access(Yuji Nishimura,

DhumilSheth,andLincolnTurner),IBMDB2UDB(JieLiandKristinTolle),Oracle8

Ser ver (Jose AlvinGendrano),and Sybase SQLSer ver (Sameer Verkhedkarand

Xi-anjinYang);trackinglogs:IBMDB2UDB(HelenThomasandGiedriusSlivinskas),

MicrosoftSQLSer ver(In´esF.Vega-L´opezandGiedriusSlivinskas),SybaseSQLSer ver

(Yi-JinShi),Oracle8Ser ver(ScottCalvertandWeiLi),andUniSQL(RachanaShah);

transaction-timestatetables:MicrosoftSQLSer ver(In´esF.Vega-L´opezandGiedrius

Slivinskas) and Oracle8 Ser ver(Scott Calvertand WeiLi);bitemporal tables:

Ora-cle8 Ser ver(ScottCalvert andWei Li);thecapstonecase:Oracle8 Ser ver (WeiLi);

TIMEDB: Andreas Steiner; TIGER: Michael B¨ohlen; Synchrony: John Bair, Hollis

Chin, and Michael Soo; and the white papers: W L A Derks, Heidi Gregersen,

ChristianS.Jensen,LeoMark,JanneSkyt,andJeroenWijnands.JianYangsuperbly

assembledallthelesinto acoherentandconsistentwhole.Ialsoappreciatethe

supportovertheyears fromtheNational Science Foundation,recently viagrants

IRI-9632569 andISI-9817798,andfrom AT&TCorporation,DuPont Corporation,

IBMCorporation,andNCRCorporation,whichenabledtheresearchthatprovides

thefoundationforthisbook

Myeditor,DianeCerra,waswonderfulthroughouttheinvolvedprocessof

writ-ingandproducingthisbook.Iespeciallyappreciateherconstantquestforquality

I'vewritten forseveraleditorsand publishers,andDiane andMorganKaufmann

arebyfarthemostauthor-andbook-friendly

The design of this book is considerablymore complex than that of my other

books.EdwardWadeworkedcloselywithmeonthisdesign.AlthoughIwouldnot

havethoughtitpossible,EdwardcaredasmuchasIdidaboutgettingthedesignjust

so, puttinghis heartinto theproject.Thedesignfeels right,andEdwarddeser ves

mostofthecredit.Edward,itwastrulyajoytoworkwithyou

Finally, I thank my wife, Merrie, and my two children, Eric and Melanie, for

continually emphasizing by their example that life is so much more than book

writing.Theyprovideddelightfuldistractions—howcouldIresistramblingthrough

thepark,orreadingapoembyShelSilverstein,orhelpingoutonapinewoodderby

car?

Oneoftheoft-unexpectedbenetsoftaking photographs,whetherasahobby

oras aprofession, isthat yousee morevividly.The veinswithin afallen leaf

be-comeall-absorbing, andshadowsonabuildingaresuddenlyprofoundand

evoca-tive My experience in writing this book has been similar, in that the words I

Trang 23

resonated with me During the course of thelast two years, I have enjoyed the

unwitting companionship of many ne writers, of books and of music,

includ-ing BethNielsenChapman,Mar yChapin Carpenter,IrisDeMent,LouiseErdrich,

Nanci Grifth, Stephen Jay Gould,Lucy Kaplansky,Barbara Kingsolver (a fellow

Tucsonan!),WilliamLangewiesche,AlisonMoore(anotherTucsonan),RonQuerr y

(Tucsonisblessedwithaccomplishedwriters),AnnaQuindlen,DavaSobel,Dar

Wil-liams,andLucindaWilliams.Theirproseandpoetr yhasenrichedandsustainedme

onthisattimesarduousyetultimatelygratifyingjourney.Myhopeisthatourpaths

continuetocross

Trang 25

t was“as if youred a 15-inchnaval shellat a pieceof tissue paperand the

shellcamerightbackandhityou.”ThusErnestRutherforddescribedhis

aston-ishmentattheresult ofhis undergraduatestudent'sexperimentin1911.The

experimentwasasimpleone:exposethinfoilsofgoldto particlesandwatchfor

appreciablescattering.Thethen-currentmodelofmatterwasthatitwasa“bunch

of electrons and some nondescript smeared out jelly of positive charge.” The

particleweighs some8000timesmorethananelectron,yetwasunexpectedly

de-ectedby thejelly Thisobser vationledtoaradicalchange inourconceptionof

matter, resulting intheRutherford atom,atight nucleussurroundedby orbiting

electrons

ThescatteringofparticlesandwavessuchasXraysprovidesmuchinformation

on theinner structure of matter Diffraction patterns arestunningly beautiful in

theirregularity,reectinginahighlyindirectfashiontheorderingofatomsofthe

cr ystalline solids(suchasTaSe

2,showninFigure1.1) exposedtothebeam.These

patternscanbeanalyzedtounderstandthisgeometricstructureandother

proper-ties.Indeed,muchofwhatisknownaboutthestructureofsolidsisduetoanalysis

ofdiffractionpatterns.Suchastudywascritical,forexample,inunderstandingthe

spiralstaircaseofDNA'sdoublehelix

The diffraction patterns areemphatically not magnications of thecr ystalline

structure; rather, thevarious distances and angles of the blips can be translated

back via sophisticated calculations to the unseen lining up of atoms stuck in a

three-dimensionalgridlock.Onlybyunderstandingthephenomenonofdiffraction

of wave motion, andtheimpact ofa periodicarray of barriersontheimpinging

wave,canphysicistsaccuratelyorienttheatomsandpiecetogethertheunderlying

pattern

AnSQLtablecontainingdatesandtimesalsoexhibitsapleasingregularity,with

datesinonecolumnrecurring inotherrowsinanothercolumn,andthedatesin

Trang 26

Figure 1.1 Electron diffraction pattern of TaSe

2 (Imagereprinted, by permission,

from Structuraland Chemical Analysis of Materials, Figure 11.5(b), by J.P Eberhart

c

1991JohnWiley&Sons,Ltd.)

tiveofaninnerstructure,whichSQLsoeffectivelymasks.Onlybyunderstanding

the ways in which time-var ying behavior can be modeled, and by studying the

mappingofthisinformationintotabularform,cananSQLtableinatime-var ying

applicationbeeffectivelydesigned,queried,andmaintained

1.1 ATRIAD OFTRIPLES

Itis humannaturetodifferentiate,toteaseapart,tocontrast.Identifying

dichot-omies,partitioningintotwomutuallyexclusivegroups,seemstobeafundamental

strategyforcontendingwithdiversity.Wefavorblackandwhite,thisandthat,us

andthem,overshadesofgrey,aspectrumofpossibilities,aglobalcommunity.We

stereotypeourselvesandothersandallthingsbymembershipinidentiedgroups:

plantoranimal,minorityormajority,rightorwrong.Logicandlibrariesemphasize

thedistinctionbetweentrue(nonction)andfalse(ction).Muchoftheprevalence

of computersintoday'ssocietyderivesfromtheclarifyingsimplicity ofstrings of

justtwovalues,0and1,encodingever ythingfromnamestotherelativestrength

ofachessconguration

Asprevalentasthisbinar ystructureis,acollectionofthreeitemsofsimilar

im-portseems toresonate even deeper While a divisioninto two parts is appealing

Trang 27

not either-or.Atriadcannotbereducedtoblackandwhite,butisforeverresigned

tocontainambiguityandcomplexity.Three-levellogicsembracethevalueof

“un-known.” TheGreeks viewed the world ascomprising the earth, thesea, and the

sky(heaven).ChristiansrejoiceintheTrinity;theyalsospeakofearth,heaven,and

hell.InBuddhism,therearethethreerootsofevil:lust,hatred,anddelusion.Many

religionsdifferentiate themind,thebody,andthesoul.Pythagorascelebratedthe

triangle,thesimplestgeometricgure.Weperceivethreespatialdimensions

Rain-bowsarecombinationsofthreeprimar ycolors.Manygovernmentsarepartitioned

into threebranches,forasimilarreasonthatastoolhasthreelegs.Theharmonic

basis ofWesternmusicisachordofthreetonesconsistingofarootwithitsthird

andfth.Aliterar ytrilogycarrieswithitasatisfyingcompleteness

In thisbookwe examinehow toimplementatime-var ying applicationinthe

SQLstructuredquer ylanguage.Wefocusonthreesetsoforthogonalconcepts:

 Temporaldatatypes

 Kindsoftime

 Temporalstatements

These conceptsareencounteredin ever ytime-var ying application.IfSQL

ade-quately supported these concepts, ourtask,and yours inactuallydeveloping the

application, would havebeen much easier:just use SQL inthe appropriate

fash-ion tobringforththedesiredbehavior.Despite thenear universality oftimeand

thetime-var yingnatureoftheenterprisebeingmodeled—astaticandunmalleable

congurationisrareanduninteresting—SQLquitefranklydoesalousyjobin

cap-turing those aspects that are changing intime, orin providing constructsto

ef-fectively model,quer y,ormodifysuchinformation.Instead,you,theapplication

developer,aresaddledwiththetaskoftransformingtheseconceptsintosomething

thatSQLcandealwith.Thisbookwillemphasizethebestwaytothinkabout

time-var yingdataandwillshowwithmanyexampleshowtomaptheseconceptsintoa

temporallyunfriendlySQL

Each of these concepts itself consists of three orthogonal elements There are

threefundamentaltemporaldatatypes:

 Instant:somethinghappenedat aninstantoftime(e.g.,“now,”whichhappens

tobeJune29,1998,4:06:39P.M whenIamwritingthis,orsometime,perhaps

muchlater,whenyouarereadingthis)

 Inter val:alengthoftime(e.g.,threemonths)

 Period:ananchoredduration oftime(e.g.,thefallsemester,August24through

December18,1998)

SQL-92includessupportforinstantsandinter vals,thoughinplacesitconfounds

Trang 28

simulatedwith integers oroating-pointnumerics Periodsare alwaysleft tothe

applicationdevelopertosimulateusingsupplieddatatypes

There are three fundamental kinds of time We'll dene more precisely and

illustratethesetermsshortly,inthenextchapter

 User-denedtime:anuninterpretedtimevalue

 Validtime:whenafactwastrueinthemodeledreality

 Transactiontime:whenafactwasstoredinthedatabase

Thesekindsoftimeareorthogonal:atablecanbeassociatedwithnone,one,two,

orevenallthreekindsoftime.Understandingeachkindoftimeanddetermining

whichis presentinanapplicationis absolutelycritical.Wewill characterize each

indetail.SQL-92hasrudimentar ysupportonlyforuser-denedtime;thelanguage

providesnohelpwhatsoeverwiththeother twotypesoftime.Thatisleftforyou

tomanage,manually,inyourapplication.We'llseeexactlyhowtodoso

Therearethreebasickindsoftime-orientedstatements:

 Current:now

 Sequenced:ateachinstantoftime

 Nonsequenced:ignoringtime

Thetrichotomyappliesequallywelltoqueries,modicationstatements,views,and

integrity constraints The most useful is sequenced, for which SQL-92 provides

absolutely no help In fact, getting SQL to express a sequenced statement is

of-tenquitepainful, yet thatis usuallywhat isrequired bytheapplication.We will

showexactlyhowtowriteallthreekindsofstatements.SQL-92providessome

sup-portfornonsequencedstatements; currentstatementsareagainentirelyuptothe

programmer

So,theseveralhundredpagesofthisbookattempttoconveythreesetsofthree

orthogonalconcepts,mostofwhich SQLiswoefully ignorant.Thosenotions

for-eigntoSQLmustbetransformedfromtheir clean,crispmanifestationintoan

of-tenbaroqueexpressioninSQL.Toaddtothechallenge,noDBMSsupportstheSQL

standardinits entirety;instead,thereareinfuriatinginconsistenciesand

substitu-tionseach vendorhaschosentoimposeonitsusers.Wehelp younavigatethese

treacherouswaters,andavoidtheever-presentshoals

SQLisactuallyaseriesofstandards.SQL-86includednotemporaldatatypes,even

thoughsomecommercialimplementationsinthelate1980sdidsupportsuchdata

Trang 29

TIME-STAMP,andINTERVAL.SQL3iscurrentlyindraftform(andhasbeensoforseveral

years;thepathfromdrafttonalacceptedstandardisaponderousone).Portionsof

SQL3areexpectedtobeapprovedasastandardinlate1999.Part7,SQL/Temporal,

introducedanewconstructor,PERIOD Inthisbook,“SQL”refers toSQL-92,and

“SQL/Temporal”referstothisdraftpartofSQL3.Weemphasizefacilitiescurrently

availableindatabaseproducts,butalsomentionfeaturesonthehorizon

“Atleastone”queriescanbe

easilystatedusinganadditional

correlationnameintheFROM

clause

Inthecasestudies, foreach kindofquer y(modication,

asser-tion,constraint,view),thegeneralprinciplebehindthequer yis

discussed.For somecomplexqueries,pseudocode maybe

pro-vided.Followingthepseudocode,aparticularquer yisgivenasa

codefragment.Notablefeaturesofthequer yarethenexamined

Codefragmentsareoftenreferencedlaterinthediscussion.The

referencesareabbreviatedas,forexample,CF-1.1 Importantpointsareemphasized

aspullquotes(thosepiecesofinformationsetofffromtextandextendingintothe

pagemargins)

As anillustration ofthestylistic conventionsused throughout thisbook,

con-sider(conventional)queriesoftheform“ atleastone .”—forexample,“Which

managermakeslessthanatleastoneoftheirsubordinates?”Thisquer ymustnda

suitablesubordinate foreachmanagerlisted Suchqueriescan bewrittenwithan

EXISTSorINsubquer y

CodeFragme t1.1 Whichmanagersmakelessthanatleastoneoftheirsubordinates?

SELECT DISTINCT EID

FROM Employee AS M

WHERE EXISTS ( SELECT *

FROM Employee AS E

WHERE E.Mgr = M.EID AND E.Salary > M.Salary )

While thisquer y insomeways parallels theEnglish version,the effectcan be

moreeasilyobtainedbyaugmentingtheFROMclauseandtheWHEREclause

“Atleastone”queries

Mentionthetableprovidingthesought-afterentityintheFROMclause

ReferencethattableintheWHEREclausetolocatetheentity

Trang 30

CodeFragme t1.2 Whichmanagersmakelessthanatleastoneoftheirsubordinates

(withoutusingEXISTS)?

SELECT DISTINCT M.EID

FROM Employee AS M, Employee AS E

WHERE E.Mgr = M.EID AND E.Salary > M.Salary

ThenestedcorrelationnamehasbeenmoveduptothemainFROMclause,

result-inginamoresuccinctquer y,aprocesstermed“decorrelation.”

Astylizedimageofanescapement(thecriticalcomponentofamechanicalclock)

is displayed ontheopeningpage of each chapterin thisbook.Such clocks have

anescapewheelconnectedtoaweight(suchasina grandfatherclock)oracoiled

spring(suchasinawristwatch).Theescapewheel,whichisconnectedbygearsto

thehandsoftheclock,wouldspincontinuouslyifnotretardedbytheescapement,

whichperiodicallystopsandreleasestheescapewheel

Overthelast500years,horologistshavedevisedallmannerofingenious

escape-ments.ExamplesincludeArnold'schronometerescapement,Bond'sgravity

escape-ment, Brocot'spin palletescapement, Congreve'sextreme detached escapement,

theDebaufreescapement,Froment'selectricalescapement,Graham'sdead-beat

es-capement,Grimthorpe'sgravityescapement(usedinBigBen),Harrison's

grasshop-perescapement,andthever yearlyvergeandfoliotescapement,someofwhichare

illustratedinthechapteropeners

Interspersedthroughoutthecasestudieswillbebriefsidebarsonamultitudeof

calendarsand onthefascinating alchemy

ofart,science, andengineering that

char-acterizes thedevelopment of increasingly

accurateclocksthroughtheages.Theclock

descriptionsareaccompaniedbyastylized

“sun”icon,thecalendar descriptionsbya

“moon”icon

Finally, each chapter ends with a

sec-tion on implementation considerations,

identifyingwaysinwhichcommercial

sys-tems deviate from the standard and

pro-viding adaptations of the chapter's code

fragmentsthat will run onthese systems

A nal section, Readings, provides

point-ers that elaborate on the material in that

chapterandintheclockandcalendar

side-bars,indicatingthecorrespondencetothe

sidebars with the sun and moon icons,

C le d rs

Calendars aremankind's wayofcontending with

years and lunar months composed ofa

noninte-gralnumberofdays.AsStephenJayGouldso

elo-quentlywrites“IfGodwerePythagorasinGalileo's

universe,calendricswouldneverhavebecomean

intellectual subject at all The relevant cycles for

naturaltimekeepingwouldallbenice,crisp,easy

multiples of each other ButGod, thank

good-ness, includesboth LokiandOdin,the comedian

andthescholar;thejesterandthe saint.Goddid

notfashionaver yregularuniverseafterall.Andwe

poor sods ofhis imagearetherefore condemned

tostrugglewithcalendricalquestionstillthecows

comehome.”[35,p.134]

Trang 31

Gn mo ic

Man's rst subdiurnal clock was most likely his

shadow: when it started gettinglonger, the day

was half over The next advance was to

substi-tute a gnomon, or staff of known length whose

shadowcanbemeasured(gnomonisaGreekword

for“pointer”).Obelisksattowncenters,which

pro-videamoreaccuratedesignationofnoon,byvirtue

of their height, were used in Egypt by around

1450B.C.E.forthemeasurementoftimeandthe

construction of calendars, thereby signaling the

beginningofthescienceofsundials,orgnomonics

Inthethirdcentur yB.C.E aChaldeanpriestby

thenameofBerossoshollowedoutahalf-spherein

arectangularblockofstone,positionedagnomon

inthecenter,andinscribedlinesdividingthearcof

theshadowinto12hours,inaccordancewiththe

12constellationscrossedsuccessivelybythe sun,

thezodiac(Figure1.2).Thishemispheriumwasthe

rstsundial tomeasurehours.Berossosthen

real-izedthatthebottomhalfofthespherewasnever

used,soheremovedthisuselessportion,resulting

inthelighterandthusmoreportablehemicyclium

(Figure1.3)

Figure 1.2 Berossos's hemispherium (From

Rohr, R R J., Sundials: History, Theory, and

Practice.DoverPublications,NY,1996.)

Figure 1.3 Berossos's hemicyclium (From

Rohr, R R J., Sundials: History, Theory, and

Practice.DoverPublications,NY,1996.)

TheSQLcodeforthefragmentsisalmostentirelyinstandardSQL-92(any

excep-tionswill beclearlynoted).Unfortunately, duetothenoncomplianceofall

Trang 32

exist-studyconcludeswitha discussionof howthegeneralapproachcanbe appliedto

variousspecic DBMSs,including IBMDB2Universal Database (UDB),Informix–

Universal Ser ver, Microsoft Access and Microsoft SQL Ser ver, Sybase SQLSer ver,

Oracle8 Ser ver, and UniSQL Eachof these products supports a different variant

of SQL,introducinglimitations thatmust beworkedaroundandextensions that

can beexploited.EachalsoimplementsthevariousconstructsinSQLdifferently,

so an approachthat is impractical onone productmay be thepreferred oneon

anotherproduct

The included CD-ROM contains the code fragments implemented on one or

moreDBMSs.Thespecicversions onwhich thefragmentsweretestedwereIBM

DB2UDB;Informix–UniversalSer ver9.12;MicrosoftAccess95,Access97,and

Ac-cess 2000;MicrosoftSQLSer ver6.5and7.0; SybaseSQLSer ver10;Oracle8 Ser ver;

andUniSQL.However,becausevendorsworkver yhardtoensuretheirproductsare

upwardcompatible,thesefragmentscanbeexpectedtocontinuetoapplyinfuture

versionsofthesesystems

ThedescriptionsofthespecicDBMSs paralleleach other,so eachcanbe read

independentlyof the rest.Indeed,itis expectedthatyou will be usingonlyone

DBMS;thematerialontheotherproductsmaybesafelyskipped

TheofcialdesignationofSQL-86isAmericanNationalStandardsInstitute(ANSI)

X3.135-1986andInternationalOrganizationforStandardization(ISO)9075-1987,

“Database LanguageSQL.” Thisstandard,at 110 pages, isrelatively brief.SQL-89

is ANSI X3.135-1989 and ISO/IEC 9075:1989; this language addedreferential

in-tegrity In addition, ANSI published X3.168-1989, “Database languages—

Embedded SQL,”which made specications forembedding SQL inconventional

programming languagesnormative (required);ISO chose notto publish an

anal-ogousstandard.SQL-92,which doeshave normativeembedding, isANSI

X3.135-1992 and ISO/IEC9075:1992,“Database languagesSQL”[44 ] MeltonandSimon

provide acomprehensive,readable presentationof SQL-92,including a thorough

explanationoftheSQLstandardizationprocess(JimMeltonistheeditoroftheSQL

standards)[71 ].Thestandarditselfisaprecise,thoughsoporic580pages

In 1995 ISO standardized ISO/IEC 9075-3:1995, “Database languages—SQL–

Part3:Call-LevelInterface”;thenextyear,ISO/IEC9075-4:1996,“Database

langua-ges—SQL–Part4:PersistentStoredModules,”appeared.Thesewereoriginally

con-sideredpartsofthedraftSQL3specication,butwerestandardizedbeforethecore

partofthatlanguage.Thereisalsoa150-page“DatabaselanguageSQL—Technical

Corrigendum3”thatprovides(mostlyminor)correctionsanddisambiguationsto

Trang 33

stan-dardization,asjustmentioned.Thecoreportionsofthelanguage,SQL/Framework

(Part1),SQL/Foundation(Part2),SQL/CLI(CallLevelInterface:Part3),SQL/PSM

(PersistentStoredModules:Part4),andSQL/Bindings(HostLevelBindings:Part5)

arenearingtheFDISballotstage,whentheSQLcommitteesofthemember

coun-trieswillvoteonthequestionofwhetherthespecicationisreadytobean

inter-national standard.SQL/Temporal(Part7)will notgointoballotinguntilthenext

millennium SQL/Foundation by itself is 850 pages; together all 10 parts of this

specicationexceedaback-straining2000pages

Apage maintained byKeith Hare (www.jcc.com/sqlstnd.htm) isa central source

of information abouttheSQLstandard.Theinformation availablethereincludes

thecurrentstatusofthestandard,informationaboutthestandardscommitteesand

thestandardsprocess,andpointerstootherstandardspages.EisenbergandMelton

provideacrisplywrittenover viewofdatabasestandards[29 ]

Ernest Rutherford, whose modelof theatom constituted anessential step

to-wardourcurrentunderstandingofmatter,didnotreceivetheNobelPrizeforthat

contribution,onlybecausehehadalreadyreceivedthisultimaterecognitionsome

three yearsearlierin1908,attheripe oldageof37,forhisworkwithradioactive

elements andXrays.Thecontextof theseexperiments isablydescribedby

Abra-hamPais(whoprovidesthesecondquoteintherstparagraphofthischapter)in

his superbbiographyof theDanishphysicistNeilsBohr[77 ,p.123].Bohrrened

and extended Rutherford's model to arrive at ourcurrent understanding (which

is often called the “Bohr atom”),attaining the Nobel Prize in 1922, also at the

ageof 37.Rutherford'sundergraduate student,Henr yG.J.Moseley,subsequently

used Rutherford'smodel, Bohr's theor y, and his own X-ray diffraction studies to

understandtheperiodictableoftheelementsintermsofatomicnumbers

Stephen Jay Gould has written a delightful and highly recommended

mono-graphentitledQuestioningtheMillennium:ARationalist'sGuidetoaPreciselyArbitrar y

Countdown “Ifweregardmillennialpassioninparticular,andcalendrical

fascina-tioningeneral,asdrivenbythepleasureoforderingandthejoyofunderstanding,

then this strangelittle subject becomes a wonderful microcosm forever ything

thatmakeshumanbeingssodistinctive,sopotentiallynoble,andoftensoactually

funny”[35,pp.157–158]

Rene´Rohr'sSundials:Histor y,Theor y,andPracticeprovidesjustthat:afascinating

2500-year chronology, a readable explicationof the mathematics behindsundial

design, and sage advice on positioning the gnomon and drawing the arcs [80 ]

Along theway,51 photographsand over 100 gures illustrate the myriad forms

Trang 34

O V E R V I E W

We wend our way through the

fundamen-talconceptsoftime-varyingdatabase

applica-tionsviaourrstcasestudy,temporarily

skirt-ingthecomplexitiesoftheactual

implementa-tion.Theseconceptswillbeexaminedindepth,

along with theirexpression in SQL, in

subse-quentchapters

Trang 35

hey started getting sick inearly June of1997 Some justhad a bad

stom-achache;othershadseverecrampingandwerepassingblood.Theysuffered

fromapotentiallylethalstrainofthebacteriumEscherichiacoli(O157:H7)

Bymid-Augustsomedozen-oddcases,allinColorado,weretracedbacktoa

process-ing plantinColumbus,Nebraska.Theplant'soperator,HudsonFoods,eventually

recalled25millionpoundsoffrozenhamburgertoattempttostemtheoutbreak

Thatparticularplantprocessesabout400,000poundsofhamburgerdaily

Ironi-cally,thisplanthadreceivedhighmarksforitscleanlinessandadherencetofederal

food-processing standards.Whatledtotherecall ofaboutone-fthoftheplant's

annual outputwas thelackof adatabasethat couldtrack thepattiesback tothe

slaughterhousesthatsupplycarcassestotheColumbusplant.Itisbelievedthatthe

meatwascontaminatedinoneoftheseslaughterhouses,butwithoutsuchtracking,

allweresuspect

Put simply, thelack of an adequatetime-var ying databasecost Hudson Foods

$25million

Dr Brad De Groot is a veterinarian working in Clay Center, Nebraska, about

60 miles southeastof Columbus He is interestedin improving thehealth

main-tenance ofcows ontheirway toyourfreezer.He hopestoestablish thetemporal

relationships betweenputativeriskfactorexposure(e.g.,apreviouslyhealthycow

sharing apenwithasickanimal)andsubsequenthealth events(e.g.,thehealthy

cowlatersuccumbstoadisease).Theserelationshipscanleadtoanunderstanding

of howdiseaseis transferredtoand amongcattle,and ultimatelytobetter

detec-tionandpreventionregimes.Asinputtothisepidemiologicstudy,Bradiscollecting

datafromcommercialfeedyardrecord-keepingsystemstoextractthemovementof

Trang 36

2.1 VALID-TIMESTATETABLES

It'spresentever ywhere,butoccupiesnospace

Wecanmeasureit,butwecan'tseeit,touchit,

getridofit,orputitinacontainer

Ever yoneknowswhatitisandusesitever yday,

butnoonehasbeenabletodeneit

Wecanspendit,saveit,wasteit,orkillit,

butwecan'tdestroyitorevenchangeit,

andthere'sneveranymoreorlessofit

—JespersenandFitz-Randolph,FromSundialstoAtomicClocks

Inafeedyard,cattlearegroupedinto “lots,”withsubsetsoflotsmovedfrompen

topen.OneofBrad'stables,theLOTLOCtable,recordshowmanycattlefromeach

lot reside ineach penof each feedyard The full schemafor thistable has nine

columns;we'lljustconsiderafewofthem

Bradwishestocapturethehistor yofwhichcattlewerecoresident,tostudyhow

diseasemovesfromcowtocow.Headdstwocolumns,FROMDATEandTODATE,to

thistable:

LOTLOC(LOTIDNUM, PENID, HDCNT, FROMDATE, TODATE)

Thesetwocolumnswill enablemanyinterestingqueriestobeexpressed(someof

considerableintricacy),whileenormouslycomplicatingpreviouslyinnocuous

con-structssuchasprimar yandforeignkeys.Thesecolumnsrenderthetablea

“valid-timestatetable”:itrecordsinformationvalidatsometimeinthemodeledreality,

anditrecordsstates,thatis,factsthataretrueoveraperiodoftime.TheFROMDATE

andTODATEcolumnsdelimitthe“periodofvalidity”oftheinformationintherow

The“temporalgranularity”ofthistableisaday

Therst three columnsareintegercolumns Thelasttwocolumns areoftype

DATE.SQLsupportsthreekindsofinstants,DATE,TIME,andTIMESTAMP,which

differ intheir rangeof values(e.g., DATEs rangeover9999 years, whereas TIMEs

range overonly 24hours) and their temporalgranularity (a dayforDATEand a

secondfordefaultTIMEs).Chapter3coverstheseandtheINTERVALdatatypesin

alltheirglor y(andgrubbiness)

Thelasttwocolumnsdenotethestartinginstant (actually, thestarting day)of

theperiod ofvalidityof therow andtheterminating instant of theperiod of

va-lidity Unfortunately, SQL-92 does not support periods, so theperiod of validity

mustbeimplementedwithtwodelimitinginstants.Chapter4liststhevariousways

periods can be implementedwith thedata typesthatSQL doesprovide,andthe

operations(predicatesandconstructors)thatmaybeappliedtoperiods

Table2.1recordsthemovementofthree lotsofcattleinthefeedyard.Inthis

Trang 37

Table2.1 TheLOTLOCtable.

LOTIDNUM PENID HDCNT FROMDATE TODATE

offthefeedyardonFebruar y18(SQL-92DATEliteralsareexpressedas

year-month-day) Also, 14 head of cattle from lot 374 are still inpen 1 (we use “forever” to

denotecurrentlyvalidrows),and23headofcattlefromlot219weremovedfrom

pen1topen2onMarch1,withtheremaining20headofcattleinthatlotmoved

topen2onMarch14,wheretheystillreside

Without the timestamp columns (FROMDATE and TODATE), theprimar y keyof

LOTLOCisthepair(LOTNUMID,PENID),whichcanbeinformallyexpressedas“the

(lot identier, penidentier)pairisuniquetoa singlerow.” Withthetimestamp

columns, thiscanbe generalizedto“atany pointintime, the(lot identier,pen

identier) pair is unique to a single row.” It is unfortunate that SQL's PRIMARY

KEY construct is inadequate for valid-time state tables; expressing this manifest

constraint inSQL-92requiresacomplexassertion,aswillbe showninChapter5,

whichcoversallmannerofdenitionalrequirementsofvalid-timestatetables

2.1.1 Queries

Queriesoverconventionaltablesask,“Whatis?”Queriesovertime-var yingtables

can be placedinthreebroad classes.Foreach conventional(nontemporal) quer y

over a table without these two DATE columns, there exist “current” (“What is

now?”),“sequenced”(“Whatwas,andwhen?”),and“nonsequenced”(“Whatwas,

atanytime?”)variantsoverthecorrespondingvalid-timestatetable

Considerthenontemporalquer y“Howmanyheadofcattlefromlot219infeed

yard1areineachpen?”ThecurrentanalogovertheLOTLOCvalid-timestatetable

is“Howmanyheadofcattlefromlot219are(currently)ineachpen?”Forsucha

quer y,weonlyareconcernedwithcurrentlyvalidrows,andweneedonlytoadda

predicaterequestingsuchrows.Thisquer yreturnsthefollowingresult,statingthat

allthecattleinthelotarecurrentlyinasinglepen:

PENID HDCNT

Trang 38

Thesequencedvariantis“Givethehistor yofhowmanyheadofcattlefromlot

219wereineachpen.”Theresult(Table2.2)providestherequestedhistor y.Wesee

thatlot219movedaroundabit

The nonsequencedvariant is“How manyhead of cattlefrom lot219 were, at

sometime,ineach pen?”Herewedon't carewhen thedata wasvalid.Notethat

the quer ydoesn't ask fortotals; itis interested inwhenever a portionof the

re-quested lot was in a pen Table 2.3 shows the result Nonsequenced queries are

oftenawkwardtoexpressinEnglish,butcansometimesbeuseful

Asanotherexample,considerthenontemporalquer y“Whichlotsarecoresident

inapen?”Suchaquer ycouldbe arststepindeterminingexposuretoputative

risks.Indeed,theentireepidemiologicinvestigationrevolvesaroundsuchqueries,

whichturnouttobenotoriouslydifculttoexpressinSQL-92

Thecurrentversion,“Whichlotsarecurrentlycoresidentinapen?”,willreturn

the empty table when evaluated on Table 2.1, as none of the lots are currently

coresident(lots219and374arecurrentlyinthefeedyard,butindifferentpens)

The nonsequenced variant is “Which lots were in the same pen, perhaps at

differenttimes?”TheresultisTable2.4:allthreelotshadoncebeeninpen1

Notehoweverthatat notimewereany cattlefromlot 137coresident with

ei-theroftheothertwolots.Todeterminecoresidency,thesequencedvariantisused:

“Givethehistor yoflotsbeingcoresidentinapen.”Thisrequiresthecattleto

actu-allybeinthepentogether,atthesametime.Theresultofthisquer yonTable2.1

isthefollowing:

L1 L2 PENID FROMDATE TODATE

219 374 1 1998-02-25 1998-03-01

As we will see in Chapter 6, current and nonsequenced queries are relatively

easytoexpressinSQL,butsequencedqueries,whichareprevalent,aresurprisingly

arduous.That chapterprovidesmanyexamplestoillustrate how suchqueriesare

phrasedinSQL

2.1.2 Modications

Modications (that is, insertions, deletions, and updates) comprise the bulk of

many applications andare challenging whenapplied totime-var ying data.We'll

illustratemodicationsontheLOTtable,whichcapturesthegenderofthecattlein

eachlot.Surprisingly(especiallytothecattle!),thegenderattributeistime-var ying

Asanasideonterminology,a“bull”isamalebovineanimal(thetermalsodenotes

amalemoose).A“cow”isafemalebovineanimal(orafemalewhale).A“calf” is

theyoungofacow(orayoungelephant).A“heifer”isacowthathasnotyetborne

Trang 39

Table2.2 Thehistor yoflot219.

PENID HDCNT FROMDATE TODATE

A “steer” is a castrated male of thecattle family To steer an automobile or a

committeeisemphaticallydifferentfromsteeringacalf.Cowsandheifersarenot

steered, theyare “spayed,” or generically, neutered, rendering them a “neutered

cow.”Thereisnosingletermforneuteredcowparallelingtheterm“steer,”perhaps

because spaying is a more invasive surgical procedure than steering, or perhaps

becausethosedoingthenamingarecowboys

Bullsaresteeredtoreduceinjuriestothemselves(bullsarequiteaggressive

ani-mals)aswellastoenhancemeatquality.Basically,allthatghtingreducesglycogen

inthemusclebers,whichincreasesthewatercontentofthemeat,whichresultsin

lessmeatperpound.Heifersarespayedonlyiftheywillfeedinopenelds,because

calvinginthefeedyardisexpensiveanddangeroustothecow

Figure 2.1illustrates thetransitionsingenderthatarepossible,allofwhichare

irreversible.(Andyouthoughtthatthisbookwasgoingtobeaboutdatabases!)

Capturing the(time-var ying) gender of cattle is important in epidemiological

studies, forthegendercanaffectdiseasetransfertoandbetweencattle.Hence,in

Trang 40

Figure2.1 Gendertransitions.

A slice of the LOT table is shown in Table 2.5 (in this excerpt, we've omitted

severalcolumnsnotrelevanttothisdiscussion).TheGNDRCODEisanintegercode

Forexpositor ypurposes,wewillusesingleletters,withcindicatingthelotconsists

ofbullcalves,hindicatingthelotareheifers,andsindicatingthelotaresteers.The

FROMDATEandTODATEinconcertspecifythetimeperiodoverwhichthevaluesof

alltheothercolumnsoftherowwerevalid

In this table, onMarch 23, 1998,a rather momentousevent occurred forthe

cattleinlot101:theyweresteered.Lot234consists ofcalves;aTODATEofforever

denotesarowthatiscurrentlyvalid.Lot234arrivedinthefeedyardonFebruar y

17;lot799arrivedonMarch12

Brad collects data from the feed yard to populate his database In doing so

he makes a seriesof modications tohis tables,including theLOTtable As with

queries, there are three generalclasses of modications: current, sequenced, and

nonsequenced

“Lot433arrives today”isacurrentinsertion “Lot101leavesthefeedyard

to-day”isacurrentdeletion.ThetwomodicationsinconcertresultinTable2.6.All

information onlot 234after todayhas beendeleted.(Asthisis beingwrittenon

Januar y13,1999,“today”isshowninSQLas1999-01-13,exposingthenonlinear

fashioninwhichthisbookevolved.)

“Thecattleinlot799arebeingsteeredtoday”isacurrentupdate,withtheresult

showninTable2.7

A current modication applies from “now” to“forever.” A sequenced

modi-cationgeneralizesthistoapplyoveraspeciedperiod,termedthe“periodof

appli-cability.”Thisperiodcouldbeinthepast,inthefuture,oroverlap“now.”

“Lot426, a collection ofheifers, wasonthefeedyard fromMarch 26 toApril

14”isasequencedinsertion.“Lot234willbeabsentfromthefeedyardfortherst

Ngày đăng: 23/03/2014, 16:21

TỪ KHÓA LIÊN QUAN