MySQL Workbench;创建中间表并映射它们

MySQL Workbench; creating intermediate tables and mapping them

我在MySqlWorkbench中有两个table;类别和产品。我创建了第三个 table,其中包含来自前两个 table 的 PK。我如何区分哪个产品属于哪个类别。我需要创建第四个 table 吗?此外,很明显,一个类别有很多产品。如何将此数据放入 table?

我在公司使用它的方式是我有一个 Category table 就像你有一个 Brands table 和一个 Products table(这是基本设置,我有更多 table 用于价格历史、产品历史等)。

Products table 是主键,包含每个 CategoriesBrands 的外键。

Table tbl_categorytbl_brand结构相同:

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

Table tbl_product:

+-----------+---------------+------+-----+---------+----------------+
| Field     | Type          | Null | Key | Default | Extra          |
+-----------+---------------+------+-----+---------+----------------+
| id        | int(11)       | NO   | PRI | NULL    | auto_increment |
| category  | int(11)       | YES  | MUL | NULL    |                |
| brand     | int(11)       | YES  | MUL | NULL    |                |
| spec      | varchar(100)  | YES  |     | NULL    |                |
| descript  | varchar(1000) | YES  |     | NULL    |                |
| unit      | varchar(10)   | YES  |     | PÇ      |                |
| cost      | decimal(16,2) | YES  |     | NULL    |                |
| ..................................................................|
|                                                                   |
+-----------+---------------+------+-----+---------+----------------+

我有以下外键:

  KEY `product_category_fk` (`category`),
  KEY `product_brand_fk` (`brand`),
  CONSTRAINT `product_brand_fk` FOREIGN KEY (`brand`) REFERENCES `tbl_brand` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `product_category_fk` FOREIGN KEY (`category`) REFERENCES `tbl_category` (`id`) ON UPDATE CASCADE

我使用的推理是:任何给定的产品只有一个类别和一个品牌,因此我可以将该信息放在产品记录本身中。


这是一个很好的例子起点(取自here