MySQL:链接表
我一直在为网站创建第一个数据库,并创建了下图以供使用。这看起来适用于业务和类别以及类别和子类别之间的多对多关系吗?
你能看出它有什么问题吗?
I have been creating my first database for a website and I have created the following diagram to work from. Does this look like it would work for many to many relationship between business and categories as well as categories and subcategories?
Can you see any issues with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来不错,尽管category_link_table 可能有问题。除非您的业务要求允许链接到类别或子类别,否则不要在链接表中同时存储主类别和子类别 ID。仅存储子类别 ID。
如果两者都存在,您可能会遇到类别 ID/子类别 ID 不匹配的情况,即您从类别“A”中获得某些内容,而在类别“B”中获得子类别“p”。
Looks ok, though the category_link_table can be problematic. Unless your business requirements allow for linking to a category OR a subcategory, then do not store both the main category and sub category IDs in the link table. Store only the the subcategory ID.
With both in there, you could potentially end up with categoryID/subcategoryIDs mismatches, where you've got something from a category 'A', and a subcategory 'p' in category 'B'.
几点评论:
我认为 Link_table 中不需要 ID 字段。 business_id,catagory_id 应该是唯一的。
您的子类别表设置似乎很奇怪,您有一个链接表。我原以为每个子类别都会有一个父类别的字段(catagory_id)。我更喜欢这种类型,如果有一个带有parent_catagory_id 的目录表,并在一个表上进行自连接。
A couple of comments:
I don't see a need for the ID field in Link_table. business_id,catagory_id should be unique.
Your Sub-category table setup seems odd, you have a link table. I'd have thought that each subcategory would simply have a field for the parent category (catagory_id). My preference for this kindn if thing is to have a catagory table with a parent_catagory_id and do self joins on the one table.
一般来说,我会确定表的命名约定。与“business_data”相比,我更喜欢“business”——它反映了业务领域,而不是技术问题。
我还会以不同的方式命名您的链接表 - 通常,“business_category”更清晰,“category_subcategory”也是如此。
As a general point, I'd settle on naming conventions for your tables. I'd much prefer "business" to "business_data" - it reflects the business domain, rather than the technical concerns.
I'd also name your link tables differently - typically, "business_category" is cleaner, as is "category_subcategory".