Upload
season
View
54
Download
0
Embed Size (px)
DESCRIPTION
Hierarchies Evan Koch Senior Architect [email protected]. Agenda. Traditional Approach Nested Sets Approach HierarchyID Approach Questions. Traditional Approach - Model. Larry. Matt. David. Mark. Melinda. Jennifer. Craig. Tom. Wendy. Sample Org Chart. - PowerPoint PPT Presentation
Citation preview
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Agenda
• Traditional Approach• Nested Sets Approach• HierarchyID Approach• Questions
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Traditional Approach - Model
Sample Org ChartLarry
Matt David Mark
Craig Tom Melinda Jennifer
Wendy
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Traditional Approach - TableID Parent
IDEmployee Name
1 NULL Larry
2 1 Matt
3 2 Craig
4 2 Tom
5 1 David
6 5 Melinda
7 1 Mark
8 7 Jennifer
9 8 Wendy
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Nested Sets Approach - Model
Larry
Matt
David
Mark
Craig Tom
Melinda
Jennifer
Wendy
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Nested Sets Approach - Model
Sample Org ChartLarry
Matt David Mark
Craig Tom Melinda Jennifer
Wendy
1
2
3 4 5 6
8
9 10
11 12
13
14 15
16
177
18
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Nested Sets Approach - Model
Larry
Matt
David
Mark
Craig Tom
Melinda
Jennifer
Wendy
1 2 3 4 5 6 7
89 10 11
12 13 14 15 16 17 18
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Nested Sets Approach - Table
EmployeeName – varchar(50)LeftNode – int
RightNode - int
Table Schema
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Nested Sets Approach - TableEmployee Name LeftNode RightNodeLarry 1 18
Matt 2 7
Craig 3 4
Tom 5 6
David 8 11
Melinda 9 10
Mark 12 17
Jennifer 13 16
Wendy 14 15
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
HierarchyID Approach - Model
Sample Org ChartLarry /
Matt /1/ David /2/ Mark /3/
Craig /1/1/ Tom /1/2/ Melinda /2/1/ Jennifer /3/1/
Wendy /3/1/1/
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
HierarchyID Approach - Model
Larry /
Matt /1/
David /2/
Mark /3/
Craig/1/1/
Tom/1/2/
Melinda/2/1/
Jennifer /3/1/
Wendy/3/1/1/
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
HierarchyID Approach - Table
EmployeeID – intOrgNode - hierarchyid
EmployeeName – varchar(50)
Table Schema
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
HierarchyID Approach - Table ID OrgNode OrgNodeAsString EmployeeName1 0x / Larry2 0x58 /1/ Matt3 0x68 /2/ David4 0x78 /3/ Mark5 0x5AC0 /1/1/ Craig6 0x5B40 /1/2/ Tom7 0x6AC0 /2/1/ Melinda8 0x7AC0 /3/1/ Jennifer9 0x7AD6 /3/1/1 Wendy
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
HierarchyID Functions
1.GetAncestor2.GetDescendant3.GetLevel4.GetRoot5.IsDescendantOf6.Parse7.GetReparentedValue8.ToString9.Read *10.Write *
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
Queries
1. Retrieve a subtree given the root2. Find all leaf nodes3. Retrieve a single path4. Find immediate subordinates5. Find immediate peers
TM
© 2012 Computer Technology Solutions, Inc.www.askcts.com
References
Joe Celko’s SQL For Smarties – Advanced SQL Programming
http://www.amazon.com/Joe-Celkos-SQL-Smarties-Fourth/dp/0123820227/ref=sr_1_1?s=books&ie=UTF8&qid=1342876431&sr=1-1&keywords=sql+for+smarties