1

i have read so many blogs and links to save the hierarchical data in mysql database like nested set modal *Transitive Clousure Modal* Child Parent Hierchy . but i am little bit confuse can any body please suggest me what is the best method to store hierarchical for more than one root.

e.g
Root1
|
|---Child 1
|    |--Child 1 of 1
|    |--Child 2 of 2
|
Root 2
|    
|--Child 2
|    |--Child 1 of 2
|    |--Child 2 of 2

Thanks in adavance :)

Ajay Patel
  • 791
  • 11
  • 24

1 Answers1

0

When you use a table to store a hierarchy, each object in the hierarchy needs a parent. So your node might have these columns:

 nodeid    int not null not zero              the id of the node in this row
 parentid  int not null, but can be zero      the id the node's parent
 nodename  varchar                            the node's name
 etc etc.                                     other attributes of the node

With this table layout any parentless node (that is, any node with parentid = 0) is a root node. You can have as many of these in your table as your application requires.

The example you showed might be represented like this:

 nodeid  parentid  nodename
 ------  --------  --------
 1       0         Root1
 2       1         Child 1
 3       2         Child 1 of 1
 4       2         Child 2 of 1
 5       0         Root2
 6       5         Child 2
 7       6         Child 1 of 2
 8       6         Child 2 of 2
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • i know this it one of the coolest method but i am searching for good alternative because it is hard to do some operation like finding leaf node , finding parent etd for N-th level tree. i really appreciate your answer. – Ajay Patel May 10 '13 at 18:50