我有2个MySQL表:如何创建产品表,其中每个产品在MySQL中的多个类别下?
categories (id int, name varchar(1000))
而且
products (id int, name varchar(1000))
每个产品可以在多个类别。我正在考虑将分类“category_ids”添加到分类为ID分类的表'products'中,但这种方法不便于MySQL查询。
任何其他可能的方法?
我有2个MySQL表:如何创建产品表,其中每个产品在MySQL中的多个类别下?
categories (id int, name varchar(1000))
而且
products (id int, name varchar(1000))
每个产品可以在多个类别。我正在考虑将分类“category_ids”添加到分类为ID分类的表'products'中,但这种方法不便于MySQL查询。
任何其他可能的方法?
使第三表,有refernce到两个表,如下面图像
创建匹配产品类别表:
product_id category_id
1 1
1 2
2 5
3 5
3 2
等希望它帮助:)
添加结合表连接两个:
**product_categories**
productid (FK ref product.id)
categoryid (FK ref categories)
这似乎是一个多对多的关系....
为了映射多对多的关系,你将需要另一台
categories_products(id, category_id, product_id)
这样一个产品可以来在许多类别和类似的一类可以容纳很多产品。
产品表将有一个与categories_products表 分类表中的许多关系也将有一对多relationip与categories_products表
那实现多对多关系的标准方式
聚集复合主键(CATEGORY_ID,PRODUCT_ID)将更好地执行完整性和更高性能 - 沟渠无意义的代理PK和阅读索引类型http://stackoverflow.com/questions/4419499/mysql-nosql-help-me-to-choose-the-right-one- on-a/4421601#4421601 –
哦,太棒了,我怎么能忘记这个映射2个表格的方法,tks提醒我 – jondinham