Saturday, 1 January 2011

SQL Nested Sets


  1. Create a database called TreeTest and open QA and execute Setup SQL
  2. Now you are ready to go. Simply select the test you with to run, then
    copy the code below, paste it into Query Analyser and hit F5!
The tree is structured in this way


Schema
  • TEST 1 - _NestedSet_ConvertToAdjacency
  • TEST 2 - _NestedSet_DeleteAndClose
  • TEST 3 - _NestedSet_DeleteNodeAndLeafs
  • TEST 4 - _NestedSet_GetAll
  • TEST 5 - _NestedSet_GetDepthFullTree
  • TEST 6 - _NestedSet_GetDepthSubTree
  • TEST 7 - _NestedSet_GetLeafs
  • TEST 8 - _NestedSet_GetNextLevel
  • TEST 9 - _NestedSet_GetSinglePath
  • TEST 10 - _NestedSet_GetTree
  • TEST 11 - _NestedSet_GetTree
  • TEST 12 - _NestedSet_InsertOnSameLevel
  • TEST 13 - _NestedSet_Move
  • TEST 14 - _NestedSet_MoveUpDown

Top

Test 1 - _NestedSet_ConvertToAdjacency

Converts a nested set to a Adjacency model

exec __Reset
exec _NestedSet_ConvertToAdjacency

Test1

Top
Test 2 - _NestedSet_DeleteAndClose

Deletes a node then moves any sub nodes up to close the gap

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_DeleteAndClose 7
exec _NestedSet_TestLayout

Test3

Top
Test 3 - _NestedSet_DeleteNodeAndLeafs

Deletes a node and all sub nodes then closes any gaps

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_DeleteNodeAndLeafs 2
exec _NestedSet_TestLayout

Test3

Top
Test 4 - _NestedSet_GetAll

Simply gets all the nodes

exec __Reset
exec _NestedSet_GetAll

Test4

Top
Test 5 - _NestedSet_GetDepthFullTree

Simply gets all the nodes with depths

exec __Reset
exec _NestedSet_GetDepthFullTree

Test5

Top
Test 6 - _NestedSet_GetDepthSubTree

Gets the depths of the selected node and sub nodes

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_GetDepthSubTree 2

Test6

Top
Test 7 - _NestedSet_GetLeafs

Gets all the leafs - that is nodes without sub nodes

exec __Reset
exec _NestedSet_GetLeafs

Test7

Top
Test 8 - _NestedSet_GetNextLevel

Gets all nodes that are directly below the selected node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_GetNextLevel 1

Test8

Top
Test 9 - _NestedSet_GetSinglePath

Gets a full path for a single node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_GetSinglePath 8

Test9

Top
Test 10 - _NestedSet_GetTree

Gets nodes including and below the selected node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_GetTree 2

Test10

Top
Test 11 - _NestedSet_InsertDirectlyBelow

Inserts a node directly below the selected node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_InsertDirectlyBelow 1,'new name'
exec _NestedSet_TestLayout

Test11

Top
Test 12 - _NestedSet_InsertOnSameLevel

Inserts a node on the same level (at the bottom) as the selected node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_InsertOnSameLevel 1,'new name'
exec _NestedSet_TestLayout

Test12

Top
Test 13 - _NestedSet_Move

Moves a node to below another node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_Move 6,3
exec _NestedSet_TestLayout

Test13

Top
Test 14 - _NestedSet_MoveUpDown

Moves a node on the same level either up or down (1 for up, 0 for down) to another node

exec __Reset
exec _NestedSet_TestLayout
exec _NestedSet_MoveUpDown 2,0
exec _NestedSet_TestLayout

Test14

References
  • Mike Hillyer Managing Hierarchical Data in MySQL (I have used his sample data)
  • Joe Celko's Trees and Hierarchies in SQL for Smarties - 2004

  • Alejandro Izaguirre Google groups

I would also like to thank:-

  • Ryan O'Neill
  • Nick Crowther