modeling many to many unary relationship and 1:M unary relationship

我的梦境 提交于 2020-03-05 01:00:16

问题


Im getting back into database design and i realize that I have huge gaps in my knowledge.

I have a table that contains categories. Each category can have many subcategories and each subcategory can belong to many super-categories.

I want to create a folder with a category name which will contain all the subcategories folders. (visual object like windows folders) So i need to preform quick searches of the subcategories.

I wonder what are the benefits of using 1:M or M:N relationship in this case? And how to implement each design?

I have create a ERD model which is a 1:M unary relationship. (the diagram also contains an expense table which stores all the expense values but is irrelevant in this case)

is this design correct?

will many to many unary relationship allow for faster searches of super-categories and is the best design by default?

I would prefer an answer which contains an ERD


回答1:


If I understand you correctly, a single sub-category can have at most one (direct) super-category, in which case you don't need a separate table. Something like this should be enough:

Obviously, you'd need a recursive query to get the sub-categories from all levels, but it should be fairly efficient provided you put an index on PARENT_ID.

Going in the opposite direction (and getting all ancestors) would also require a recursive query. Since this would entail searching on PK (which is automatically indexed), this should be reasonably efficient as well.

For some more ideas and different performance tradeoffs, take a look at this slide-show.




回答2:


In some cases the easiest way to maintain a multilevel hierarchy in a relational database is the Nested Set Model, sometimes also called "modified preorder tree traversal" (MPTT).

Basically the tree nodes store not only the parent id but also the ids of the left-most and right-most leaf:

spending_category
-----------------
parent_id    int
left_id      int
right_id     int
name        char

The major benefit from doing this is that now you are able to get an entire subtree of a node with a single query: the ids of subtree nodes are between left_id and right_id. There are many variations; others store the depth of the node in addition to or instead of the parent node id.

A drawback is that left_id and right_id have to be updated when nodes are inserted or deleted, which means this approach is useful only for trees of moderate size.

The wikipedia article and the slideshow mentioned by Branko explains the technique better than I can. Also check out this list of resources if you want to know more about different ways of storing hierarchical data in a relational database.



来源:https://stackoverflow.com/questions/11584657/modeling-many-to-many-unary-relationship-and-1m-unary-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!