You are on page 1of 20

ModelingTreesinSQL

RichardBroersmaJr. ManganInc.SystemIntegrator

Fromadatamodelingperspective, whatisaTree?

AspecializedformofGraph(datastructure).

Fromadatamodelingperspective, whatisaGraph?

ForasetofEntities(Vertexes),asetof relationships(edges)canbedefinedto representallassociationsbetweenentities.

SimpleGraphIllustration

Notice: Providesaflexiblewaytorelateentities Multiplerelationshippathsexistforentities GraphAnalysisisinefficientusingSQL

GraphAnalysisInPractice

Whichpathbetweentwoterminals: hasthesmallesttransittime hastheleastnumberofstops passesthroughacertainterminal

OkaybutwhataboutTREES!

Notice: Havearootthatallentitiesrelateto Limitssingularrelationshipsbetweenentities Relationshipsfromroottoleafarehierarchical constraintsmakeSQLanalysisperformant

TreesInPractice:OrgChart

ModelstheHaszeroormore: Whoisthemanagerofanygivenemployee Whichempsdirectlyreporttoamanager Whichempsultimatelymanagedbyamanager

TreesInPractice:Assembly(BOM)

ModelstheContainszeroormore: Whatsubassembliescomposeanassembly Whatisanassemblyultimatelycomposedof

LogicEntityRelationshipDiagram

Barker's Notation/Style

ChenlikeStyle

GoodPracticeTreeDesign
LiberaladdCheckConstraintstoeliminate possiblesourcesoftreecorruption Decoupleentityandrelationshipinseparate tables. EliminatesEntity/RelationshipDenormalization Allowsmultipletreerelationshiptablestoexist torepresentdifferenttypesofrelationships

ConsiderationforTreeOperations
Modifyingtreestructureissignificantlymore intensivethanordinarytableInsert/Update/Deletes Operationstightlycoupledwithproblemdomain. Alloperationsshouldbedesignedandrolled togetherintoStoredProceduresasanapplication framework. Throughlytestallstoredprocedurestopreventtree structurecorruptionanomalies

PhysicalDesigns

AdjacencyList Model A.K.A.BOMModel

NestedSetModel A.K.A.CelkoTree

PathEnumeration Model (ImpetusFor PostgreSQLLTREE ContribModule)

First,asimpleTree

Albert Bert Donna Chuck Eddie Fred

AdjacencyListModel
CREATETABLEOrgChart( empVARCHAR(10)NOTNULLPRIMARYKEY REFERENCESPersonnel(emp), bossVARCHAR(10)nullmeansroot REFERENCESPersonnel(emp), ...);

emp Albert Bert Chuck Donna Eddie Fred

boss NULL Albert Albert Chuck Chuck Chuck

AdjacencyListModel
PerformswellwithInserts&Deletes. UsingSQL92,canonlyreturnatmostonelevel ofthetreeperstatement(untilPG8.4CTE)and canperformbadly. WithSQL03,WithRecursivemakesALMagood choice.

NestedSetModel
CREATETABLEOrgChart( lftINTEGERNOTNULL, rgtINTEGERNOTNULL, CONSTRAINTlftlessthanrgt CHECK(lft<rgt), empVARCHAR(10)NOTNULLPRIMARYKEY REFERENCESPersonnel(emp) ...);

lft 1 2 4 5 7 9

rgt 12 3 11 6 8 10

emp Albert Bert Chuck Donna Eddie Fred

NestedSetModel
PerformsbadlywithnumerousInserts&Deletes. SelectsPerformverywell UsingSQL92,easilyreturnsalllevelsofthetree perstatement.(hardtolimittoimmediatelevels) NatureofNestedSetallowforeasyanalysisof treestructure ex.WHERElft>=4ANDrgt<=11

PathEnumerationModel
CREATETABLEOrgChart( pathVARCHARNOTNULL, empVARCHAR(10)NOTNULLPRIMARYKEY REFERENCESPersonnel(emp), CONSTRAINTvalidPath CHECK(pathLIKE'%'||emp), ...);

path Albert AlbertBert AlbertChuck AlbertChuckDonna AlbertChuckEddie AlbertChuckFred


emp Albert Bert Chuck Donna Eddie Fred

PathEnumerationModel
PerformswellnumerousInserts&Deletes. SelectsperformsverybuthastypicalLIKE'%.%' gotchas.(LTREEimprovesthiswithgistsupport) UsingSQL92,easilyreturnsalllevelsofthetree perstatementandcanlimittoimmediatelevels Analyzingtreestructureiscomplicated ex.WHEREempLIKE'AlbertChuck%'

References

Batini,Carol,StefanoCeri,andShamkantB.Navathe.ConceptualDatabaseDesign:AnEntityRelationshipApproach.Boston: BenjaminCummingsCompany,1991. Celko,Joe.JoeCelko'sSQLforSmarties:AdvancedSQLProgramming.3rded.Greensboro:MorganKaufmann,2005. Celko,Joe.JoeCelko'sThinkinginSets:Auxiliary,Temporal,andVirtualTablesinSQL.NewYork:ElsevierScience& TechnologyBooks,2008. Celko,Joe.JoeCelko'sTreesandHierarchiesinSQLforSmarties.Greensboro:MorganKaufmann,2004. Douglas,Korry.PostgreSQL.2nded.Indianapolis:Sams,2005.

You might also like