1

如果我有以下表格:

Product
+----+------+
| id | name |
+----+------+
|  1 | box  |
|  2 | car  |
|  3 | ball |
+----+------+

Color
+----+-------+
| id | name  |
+----+-------+
|  1 | red   |
|  2 | green |
|  3 | blue  |
+----+-------+

Size
+----+--------+
| id | number |
+----+--------+
|  1 |      1 |
|  2 |      5 |
|  3 |     10 |
+----+--------+

Color Options (#product | #color)
+---------+-------+
| product | color |
+---------+-------+
|       1 |     1 |
|       1 |     3 |
|       3 |     1 |
|       3 |     2 |
|       2 |     3 |
+---------+-------+

Size Options (#product | #size)
+---------+-------+
| product | color |
+---------+-------+
|       1 |     1 |
|       1 |     2 |
|       3 |     1 |
|       3 |     2 |
|       2 |     2 |
|       2 |     3 |
+---------+-------+

当我删除产品时,删除它与颜色和尺寸的关系的最佳方法是什么?我需要在每个表中进行删除还是有任何自动过程?

4

3 回答 3

5

您是否为您的 mysql设置了正确的关系?可以参考这个问题 How to create Relations in MySQL and MySQL foreign key constraint, cascade delete

于 2013-05-02T10:30:21.827 回答
1
CREATE TABLE SIZE_OPTIONS(PRODUCT REFERENCES PRODUCT(ID), COLOR REFERENCES COLOR_OPTIONS(COLOR) ON DELETE CASCADE);

使用与 COLOR_OPTIONS 相同的语句类型。

于 2013-05-02T10:33:46.447 回答
-1

INNER JOIN 是一个答案

DELETE
FROM Product p
  INNER JOIN Color_Options co
    ON co.product = p.id
  INNER JOIN Color c
    ON co.color = c.id
  INNER JOIN Size_Options so
    ON co.product = p.id
  INNER JOIN Size s
    ON so.color = s.id
于 2013-05-02T10:28:30.283 回答