删除与其他表有关系的行 [英] Delete a row with relations with other tables

查看:55
本文介绍了删除与其他表有关系的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下表格:

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 |
+---------+-------+

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

When I delete a product what is the best way to delete the relations it have with color and size? Do I need to do a delete in every table or there any automatic process?

推荐答案

您是否为您的 mysql 设置了适当的 RELATIONSHIP?你可以参考这个问题如何在 MySQL 中创建关系MySQL 外键约束,级联删除

Have you set up proper RELATIONSHIP for your mysql? You can refer to this question How to create relationships in MySQL and MySQL foreign key constraints, cascade delete

这篇关于删除与其他表有关系的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆