My application uses some kind of "virtual folders" so I need to store the tree structure in the database. The table holding the data is quite simple, it has 3 columns:
My question is: which is the best way to implement the root?
Thank you.
I would argue for option 1.
If you choose option 2, then your logic for displaying sub-folders would need confusing checks to make sure that the id_folder and id_parent don't match.
Option 1:
SELECT * --don't kill me for using *, it's just for an example
FROM Folders
WHERE id_parent = @folderId
Option 2:
SELECT * --don't kill me for using *, it's just for an example
FROM Folders
WHERE id_parent = @folderId AND id_parent <> id_folder