It depends on how you want to use your data and whether you want to support requests for updates and insertions. Your solution is called an adjacent list model and allows you to insert or update data very easily. Queries can be tricky if you have unlimited siblings depth, but again, it depends on how you plan to use this data structure. If you only want to show all the siblings of one node, that's fine. on the other hand, if you want to show the whole tree designed and do it in one query, you have a headache.
Another solution would be to use a concatenated string representing your hierarchy. For instance:
- Europe 1.1 France 1.1.1 Paris 1.1.2 Marseille
- America 2.1 United States 2.1.1 Washington
DDL will be something like this:
CREATE TABLE `category` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `link` varchar(30) NOT NULL DEFAULT '.', PRIMARY KEY (`id`), )
This data structure simplifies your queries, but updates are slower
Another solution is a nested set model in which you register the node identifier to the right and left of the current node. This is the most efficient query structure, but makes it more difficult to insert and update.
http://en.wikipedia.org/wiki/Nested_set_model
I recommend you Joe Selco's book on trees and hierarchies
source share