Handling Hierarchy Data in Database

后端 未结 6 1839
面向向阳花
面向向阳花 2021-02-03 14:05

I\'m curious to know what the best way (best practice) to handle hierarchies are in regards to database design. Here is a small example of how I usually handle them.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 14:18

    There are some vendor-specific extensions to do this, but my favorite db-neutral way comes from Joe Celko - google 'Joe Celko Trees and Hierarchies' or buy this book: link text

    This is a very clever set-based way to go. Easy to query hierarchy. I added the 'parentID' field you have just because I ask the 'direct children' and 'parent' questions a lot and that speeds those up. But this is a wonderful way to get a 'ancestry' or 'descdent' query.

提交回复
热议问题