手动调用级联删除?
假设我有 3 个表,“child1”、“child2”和“child3”,它们都有一个指向“parent”表的外键。 表“父”有一个字段“状态”,我希望如果某个元素的状态设置为“0”,则其所有子元素都将被删除。 (我使用触发器来检测状态变化)。 所以这就像调用“级联删除”,但不删除对象......
有没有一个好的方法来做到这一点?
Let's say I have 3 tables, "child1", "child2" and "child3", that all have a foreign key to the table "parent".
Table "parent" got a field "status", and I would that if status is set to "0" for an element, all its child are deleted. (I use trigger to detect status changes).
So it's like calling "cascade delete", but without deleting the object ...
Is there a good way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
触发器检测到更改,您需要使用它来触发存储过程。这是一个很好的教程: http://markalexanderbain.suite101.com/mysql- storage-procedures-and-triggers-a71091
或者,如果您使用 php、coldfusion 或其他中间层,您可以将记录读入变量。请删除该行,然后重新插入记录。但是您最好使用存储过程来完成这项工作。
Trigger detects the change, you need to use it to fire a stored procedure. Here's a good tutorial: http://markalexanderbain.suite101.com/mysql-stored-procedures-and-triggers-a71091
Alternatively if you are using php, coldfusion or some other middle tier, you could read the record into a variable. Do delete the row, then re-insert the record. But you are better off with the stored procedure doing the work.
删除与父级关联的外键的所有子级..
我们需要按帐单删除所有书籍
编辑:抱歉,重新阅读..您可以在状态的
UPDATE
上实现此逻辑delete all children with associated foreign key to the parent..
we need to delete all books by bill
EDIT: sorry, re-read.. you could implement this logic on
UPDATE
of the status