0% found this document useful (0 votes)
116 views20 pages

Modeling Trees in SQL: Richard Broersma Jr. Mangan Inc. System Integrator

The document discusses different ways to model tree structures in SQL databases. A tree is a specialized form of graph with a root node and hierarchical relationships from the root to leaf nodes. The document compares and contrasts three common physical designs for modeling trees in SQL: the adjacency list model, nested set model, and path enumeration model. It analyzes the pros and cons of each model in terms of performance for different SQL operations and analyzing tree structure.

Uploaded by

junkyard
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views20 pages

Modeling Trees in SQL: Richard Broersma Jr. Mangan Inc. System Integrator

The document discusses different ways to model tree structures in SQL databases. A tree is a specialized form of graph with a root node and hierarchical relationships from the root to leaf nodes. The document compares and contrasts three common physical designs for modeling trees in SQL: the adjacency list model, nested set model, and path enumeration model. It analyzes the pros and cons of each model in terms of performance for different SQL operations and analyzing tree structure.

Uploaded by

junkyard
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy