简体   繁体   English

我该如何处理数据库中的子类别?

[英]How can I deal with the sub category in database?

I have this csv file in the following format: 我有以下格式的csv文件:

1043374544±Collectibles
1043374544±Decorative & Holiday
1043374544±Decorative by Brand
1043374544±Christopher Radko
1043397455±Collectibles
1043397455±Decorative & Holiday
1043397455±Decorative by Brand
1043397455±Enesco
1043397455±Precious Moments

and the number is the itemID and '±' is the delimiter, after the delimiter is the category where the item belongs. 在分隔符是项目所属的类别之后,数字是itemID,“±”是分隔符。 And each category in the bottom is the previous one's sub category. 底部的每个类别都是上一个类别。 So it's like Collectibles --> Decorative & Holiday --> Decorative by Brand --> Christopher Radko in this case. 因此,在这种情况下,就像收藏品->装饰和假日->品牌装饰->克里斯托弗·拉德科。 The problem is items will have different numbers of categories. 问题是项目将具有不同数量的类别。

So how can I create table that I can be able to enquire and know what items are in each category or sub-category. 因此,如何创建可以查询并知道每个类别或子类别中有哪些项目的表。

When an item can have any number of categories and a category can include any number of items, then you have what is commonly referred to as a n:m relation. 当一个项目可以具有任何数量的类别并且一个类别可以包括任何数量的项目时,则您具有通常称为n:m关系。

The usual method to solve this issue is by adding a third table "relation" where the primary key is the table of the two others. 解决此问题的常用方法是添加第三个表“关系”,其中主键是另外两个表。

Here is an Example (* means column which is part of the primary key) 这是一个示例(*表示作为主键一部分的列)

Table products 餐桌产品

id* | name            | price
----+-----------------+--------
1   | Thingamy        | 3.45
2   | Whatchamacallit | 2.99
3   | FooBarWidget    | 1.00

Table categories: 表格类别:

id* | name
----+-------------
1   | Collectibles
2   | Holiday
3   | Decorative

Table category_product 表类别_产品

product_id* | category_id*
------------+---------------
1           | 1             // Thingamy is a Collectible item
2           | 1             // Whatchamacallit is a Collectible item
2           | 2             // Whatchamacallit is also a Holiday item

Because both colums of the relation-table are part of the primary key, it can have exactly one entry for each possible combination. 因为关系表的两个列都是主键的一部分,所以对于每种可能的组合,它都可以只有一个条目。 Having the entry means having that category assignment. 具有条目意味着具有该类别分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM