Download presentation
Presentation is loading. Please wait.
Published bySolomon Snow Modified over 8 years ago
1
Managing Hierarchical Data in a Relational Database
2
Who’s the old guy? Robert Hatton Data Architect at ARI Network Services Building databases since the ‘70s Author of ‘The Business Intelligence Team Handbook’ Feel free to contact me at rob@convitali.com
3
What is ‘Hierarchical Data’ Data elements related by Parent/Child and sibling relationships Chart of Accounts Bills of Material Organization Charts Catalogs Outline style Hierarchy is separate from data it organizes Your reporting structure only helps folks navigate to your Employee record
4
The nature of hierarchies Organized in levels Number of levels Width of levels Single Parent vs. Multiple Parent Different ID requirements
5
Hierarchy Terminology Node An individual member of a hierarchy ROOT The parent or ancestor of all other nodes The Root has no parent Leaf Node A node without children It’s a place to hang all of the data folks are interested in Parent / Child / Sibling They mean what they usually do
6
Node identification String (or binary) A single number won’t do! Every level of hierarchy must be represented Location in level has to be there too Examples: ‘/54/432/64/23/45’ ‘sporting goods, bicycles, mountain bikes, fat tire bikes, Fat Albert’
7
Example Sporting Goods 1 Court sports 1.1 Tennis 1.2 Basketball 2 Field sports 2.1 Football 2.2 Baseball 3 Outdoor sports 3.1 Camping Gear 3.2 Bicycles 3.2.1 Road Bikes 3.2.2 Mountain Bikes 4 Drinking sports 4.1 Vocal Games 4.2 Skill Games 4.3 Card Games
8
Single Parent VS Multi-Parent Hierarchies Know your data! How you design your solution depends on data Single Parent If there is (or could be) a single node that could be in more than one branch, you can’t use this model! Single parent model can house relationship key with the nodes. Example: Chart of Accounts Multi Parent Relationship table has to be kept separate from nodes. Example: Bills of Material.
9
Bicycles Bicycles.Felt Bicycles.Felt.Mountain Bicycles.Felt.Mountain.All Mountain Bicycles.Felt.Mountain.All Mountain.Compulsion 10 Bicycles.Felt.Mountain.All Mountain.Compulsion 30 Bicycles.Felt.Mountain.Trail 27.5 Bicycles.Felt.Mountain.Trail 27.5.Decree 1 Bicycles.Felt.Mountain.Trail 27.5.Decree FRD Bicycles.Felt.Road Bicycles.Felt.Road.Aero Bicycles.Felt.Road.Aero.AR FRD Bicycles.Felt.Road.Aero.AR1 W Bicycles.Felt.Road.Aero.AR2 Bicycles.Felt.Road.Race Bicycles.LiteSpeed Bicycles.LiteSpeed.Carbon Bicycles.LiteSpeed.Titanium Bicycles.LiteSpeed.Titanium.Mountain Bicycles.LiteSpeed.Titanium.Mountain.All Mountain Bicycles.LiteSpeed.Titanium.Mountain.All Mountain.Compulsion 10 Bicycles.LiteSpeed.Titanium.Mountain.All Mountain.Compulsion 30 Bicycles.LiteSpeed.Titanium.Mountain.Trail 27.5 Bicycles.LiteSpeed.Titanium.Mountain.Trail 27.5.Decree 1 Bicycles.LiteSpeed.Titanium.Mountain.Trail 27.5.Decree FRD Sample Hierarchy
10
Microsoft’s solution: HierarchyID data type The hierarchyID data type is a variable length, system data type. Example: /1/1/ or /1/2/ or /1/1.5 Root is represented by / Supports ordering of siblings. Intended to be housed in table holding hierarchy nodes. Examples only show support for single parent hierarchies. Supported in SQL Server 2008 and newer.
11
HierarchyID methods GetAncestor GetDescendant GetLevel GetRoot IsDescendantOf Parse Read GetReparentedValue ToString Write
12
Relationship table solution Use a dedicated table to map relationships Example relationshipID parentID itemID Key concept! The parentID doesn’t point to the table housing node data. It uses relationshipID from the mapping table The itemID does point to the table housing node data.
13
Attribute tables Item Attributes Example fields Item Description Weight QuantityOnHand Price Alternate tables Employees BillsOfMaterial ChartOfAccounts Relationship Attributes Used by Obsolete
14
Sample Schema
15
Demos Children by Name Children by ID Ancestors by Name Ancestors by ID populateItemRelationshipPath
16
Thank you! rob@convitali.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.