Personally, I prefer nested sets when it comes to hierarchies stored in sql. Depending on your limitations, there are many variations of the method, and there are many resources over the network regarding sql CRUD routines.
I have to say that when you have large trees, you will have a small performance penalty when creating / updating / deleting main parents, but you have excellent reading performance, and I think this is preferable to a recursive model where you have another way.
Edit:
I see a problem with multiple parents. I would suggest changing your approach by combining 2nd and 3rd levels. You will have something like
**Node** | **Left** | **Right** | **Level** Color 1 26 1 LightRed 2 5 2 LightGreen 6 9 2 LightBlue 10 13 2 DarkRed 14 17 2 DarkGreen 18 21 2 DarkBlue 22 25 2 Car1 3 4 3 Car2 7 8 3 Car3 11 12 3 Car4 15 16 3 Car5 19 20 3 Car6 23 24 3
The request for all cars that have color will be something like
select * from hierarchy where left > 1 and right < 26 and level = 3
Edit2:
a-horse-with-no-name correctly indicated that your DBMS can support recursive models. If so, then this may be the best solution for you. There is documentation for Sql Server here.
source share