简体   繁体   English

MySQL删除前触发器的正确​​语法是什么?

[英]What is MySQL's correct syntax for before delete trigger?

I'd like to create a before delete trigger that deletes rows from two different tables. 我想创建一个删除之前触发器,该触发器从两个不同的表中删除行。 But I can't figure out which parameters to use. 但是我不知道要使用哪些参数。

I got a house table, and when I delete a row, I'd like to delete every row in my two other tables: user_house and firm_house, which contains same house id as the one triggering the event. 我有一个房屋表,当我删除一行时,我想删除其他两个表中的每一行:user_house和firm_house,其中包含与触发事件的房屋ID相同的房屋ID。

What does FOR EACH ROW mean? FOR EACH ROW是什么意思? And how can I properly set my trigger up? 以及如何正确设置触发器?

USE `mydb`;
DELIMITER $$
CREATE TRIGGER `deleteUnions` BEFORE DELETE ON `house` 
FOR EACH ROW
BEGIN
    DELETE FROM user_house WHERE ?? = ??; 
    DELETE FROM firm_house WHERE ?? = ??;
END

Some details about the structure: 有关该结构的一些细节:

  • user_house is joined by user_id and house_id ; user_houseuser_idhouse_id加入;
  • firm_houise is joined by firm_id and house_id . firm_houisefirm_idhouse_id

Refer to the record that gets deleted in the trigger with OLD . 使用OLD引用在触发器中删除的记录。 Then use the id to delete from the other tables. 然后使用id从其他表中删除。

DELETE FROM user_house WHERE house_id = OLD.house_id; 
DELETE FROM firm_house WHERE house_id = OLD.house_id; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM