How to store data in a database to build lists and inner lists (sublist) for a web app from it?
I have to st开发者_StackOverflowore messages data in a database & make a hierarchy of list and inner list of messages from it for a web application. The data to be stored is entered by user and have varchar datatype. Currently user uses same text box on a webpage to enter both parent message & child node message(bec child node functionality is not implemented yet). My question is how should I store the messages & sub-messages in a database so that I could make a list & inner list hierarchy of messages from it. I mean it should be stored in different tables or same table's different fields OR etc. I have four categories of messages to be stored & any message in each category can have associated sub-messages entered by user as its inner list item. Like 'A' is one category of messages & any message of A category can have at max two child message nodes like
- Parent msg
- 1st level child
- 2nd level child
Managing Hierarchical Data in MySQL is an excellent article on storing hierarchical data in MySQL using nested sets.
The nice thing about nested sets is that you do not have a limit to the depth of nesting you can accomplish. If you limit the depth, the complexity of setting up nested sets might not be worth the effort.
In reality, nested sets allows for very fast reads of data throughout the hierarchy but has overhead for inserts/updates in the tree. So depending on the operations you're going to be performing it might not be the best solution.
精彩评论