I am new for sql . It that that any best suited to manage and dynamic create hierarchical data in database with efficiently and simply way?
Say for a sample , I am using grid view to view hierarchical data. My grid view need to display like below.
Red <---Fruit 1
Apple
strawberry
Yellow <-----Fruit 2
banana
Papaya
Please assist me how to design database structure in this scenario. On the other hand, how can I perform sorting also. For example:
I change the sequence number becoming
Yellow <-----Fruit 2 become Fruit 1
banana
Papaya
Red <---Fruit 1 become Fruit 2
Apple
strawberry
Here is my table(parent and child column)
Parent Child
null Red
Red Apple
Red Strawberry
null Yellow
Yellow banana
banana papaya
Please guild me some idea and provide me some example. THanks
Check my answer to a similar post. Solution used a recursive CTE. This solution also involved an id column, and an additional sort key column, which you could add to your source table (or query) for custom sorting.