Managing Hierarchical Data in a Relational Database
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
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
The nature of hierarchies Organized in levels Number of levels Width of levels Single Parent vs. Multiple Parent Different ID requirements
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
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’
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 Road Bikes Mountain Bikes 4 Drinking sports 4.1 Vocal Games 4.2 Skill Games 4.3 Card Games
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.
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
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.
HierarchyID methods GetAncestor GetDescendant GetLevel GetRoot IsDescendantOf Parse Read GetReparentedValue ToString Write
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.
Attribute tables Item Attributes Example fields Item Description Weight QuantityOnHand Price Alternate tables Employees BillsOfMaterial ChartOfAccounts Relationship Attributes Used by Obsolete
Sample Schema
Demos Children by Name Children by ID Ancestors by Name Ancestors by ID populateItemRelationshipPath
Thank you!