且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数据库中管理类别和产品

更新时间:2023-11-29 22:11:10

假设层次结构的级别可以变化,则不能简单地使用树表.在这里查看: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ [ ^ ]
Supposing that the level of hierarchy can vary, you can not simply use tree tables. Look here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/[^]


由于您有一个带有可能的子类别的类别,因此我将其建模为分层数据( ^ ]).

您最终会得到一个如下表的类别:
(-1表示类别没有父类别,因此是根类别)
Since you have a category with possible subcategories I''d go for modeling this as hierarchical data (More Trees & Hierarchies in SQL[^]).

You''d end up with a table like below for the categories:
(-1 indicates that a category does not have a parent and is thus a root category)
ID    Name      Description        ParentID
1     Dry Clean Dry cleaning stuff -1
2     Men       Clothes for men     1
3     Woman     Clothes for women   1



然后,您需要一种方法来关联哪些产品属于哪个类别,以便创建关联:

ProductsPerCategory



Then you need a means to relate which products are in which category so you''d create a relation:

ProductsPerCategory

CategoryID ProductID
1          1
2          2
3          3



问候,

—曼弗雷德(Manfred)



Regards,

— Manfred