61
172
Chapter14. UsingdatabasesandStructuredQueryLanguage(SQL)
14.7 Basicdatamodeling
Therealpowerofarelationaldatabaseiswhenwemakemultipletablesandmake
linksbetweenthosetables. Theactofdecidinghowtobreakupyourapplication
dataintomultipletablesandestablishingtherelationshipsbetweenthetwotables
iscalleddatamodeling. Thedesigndocumentthatshowsthetablesandtheir
relationshipsiscalledadatamodel.
Datamodelingisarelativelysophisticatedskillandwewillonlyintroducethe
mostbasicconceptsofrelationaldatamodelinginthissection.Formoredetailon
datamodelingyoucanstartwith:
http://en.wikipedia.org/wiki/Relational_model
Let’ssayforourTwitterspiderapplication, insteadofjustcountingaperson’s
friends,wewantedtokeepalistofalloftheincomingrelationshipssowecould
findalistofeveryonewhoisfollowingaparticularaccount.
Sinceeveryonewillpotentiallyhavemanyaccountsthatfollowthem,wecannot
simplyaddasinglecolumntoour
Twitter
table. Sowecreateanewtablethat
keepstrackofpairsoffriends. Thefollowingisasimplewayofmakingsucha
table:
CREATE TABLE E Pals (from_friend TEXT, , to_friend TEXT)
Eachtimeweencounterapersonwho
drchuck
isfollowing,wewouldinserta
rowoftheform:
INSERT INTO O Pals s (from_friend,to_friend) ) VALUES S (
'
drchuck
'
,
'
lhawthorn
'
)
Asweareprocessingthe100friendsfromthe
drchuck
Twitterfeed,wewillinsert
100recordswith“drchuck”asthefirstparametersowewillendupduplicating
thestringmanytimesinthedatabase.
Thisduplicationofstringdataviolatesthebestpracticesfordatabasenormal-
izationwhichbasicallystatesthatweshouldneverputthesamestringdatainthe
databasemorethanonce.Ifweneedthedatamorethanonce,wecreateanumeric
keyforthedataandreferencetheactualdatausingthiskey.
Inpracticalterms,astringtakesupalotmorespacethananintegeronthedisk
andinthememoryofourcomputerandtakesmoreprocessortimetocompareand
sort.Ifweonlyhaveafewhundredentriesthestorageandprocessortimehardly
matters.Butifwehaveamillionpeopleinourdatabaseandapossibilityof100
millionfriendlinks,itisimportanttobeabletoscandataasquicklyaspossible.
WewillstoreourTwitteraccountsinatablenamed
People
insteadofthe
Twitter
tableusedinthepreviousexample. The
People
tablehasanadditionalcolumn
tostorethenumerickeyassociatedwiththerowforthisTwitteruser.SQLitehas
afeaturethatautomaticallyaddsthekeyvalueforanyrowweinsertintoatable
usingaspecialtypeofdatacolumn(
INTEGER PRIMARY Y KEY
).