Manually call cascade delete?

☆樱花仙子☆ 提交于 2019-12-11 06:25:14

问题


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 ?


回答1:


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.




回答2:


delete all children with associated foreign key to the parent..

authorname = Bill, authorid = 5

we need to delete all books by bill

DELETE FROM books WHERE authorid=5

EDIT: sorry, re-read.. you could implement this logic on UPDATE of the status



来源:https://stackoverflow.com/questions/7663724/manually-call-cascade-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!