I am trying to create a table or sets of tables which will allow me to easily navigate through recorded entities hierarchically. I should also be able to easily migrate a certain parts of the chain from one parent to another when necessary.
organisation | parent | length | other org data |
---|---|---|---|
1 | NULL | 0 | note1 |
2 | NULL | 0 | note2 |
3 | 2 | 1 | NULL |
4 | 2 | 1 | NULL |
5 | 3 | 2 | some stuff |
6 | 4 | 2 | etc |
7 | NULL | 0 | etc |
8 | 2 | 1 | etc |
I was considering the closure table / bridge table method due to its overall easiness of implementation and maintenance, but according to this answer, recursive CTE is better: Closure table equivalent for graph structures in SQL
Can you advise on how to do build the table and how to migrate/update data easily? Is the length column necessary? It can be deduced, but it might save processing time when stored at the time of creation/updating. The levels of the hierarchy should theoretically be unlimited.
I would like to use this method for organisations hierarchy and also separately for tasks and subtasks, inventory assets and parts, as well as for comments and subcomments left on issues raised. Is there anything else I should add? Can you also provide some insight into other types of graphs and where I can read more about them? Thank you.