简体   繁体   English

MySQL触发器-更新后删除

[英]MySQL Trigger - delete after update

For test case in one project I must delete record if one of fields meets one of conditions. 对于一个项目中的测试用例,如果其中一个字段满足条件之一,则我必须删除记录。 Easiest seems be simple trigger: 最简单的触发似乎是:

delimiter $$
drop trigger w_leb_go $$

create trigger w_leb_go after update on test
for each row
begin
set @stat=NEW.res;
set @id=NEW.id;
IF @stat=3 THEN
  delete from test where id=@id;
END IF;
end$$

delimiter ;

But as I suspect got error: 但是我怀疑有错误:

update test set res=3 where id=1; 更新测试集res = 3,其中id = 1;
ERROR 1442 (HY000): Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 错误1442(HY000):无法更新存储函数/触发器中的表“ test”,因为调用该存储函数/触发器的语句已使用该表。

How else can be done only by having access to the database? 只能通过访问数据库才能完成其他操作? I mean that I should not change the tested application. 我的意思是我不应该更改经过测试的应用程序。

What you may need to do is use the 'BEFORE INSERT' of the trigger to check if the record being added is not valid. 您可能需要做的是使用触发器的“插入前”检查要添加的记录是否无效。

This SO question provides some detail on how to dipose of the invalid record: Prevent Insert 这个SO问题提供了有关如何处置无效记录的一些详细信息: 防止插入

The alternate is to modify your program to insert a record into another table which has a trigger to purge the desired table. 另一种方法是修改程序,以将记录插入另一个具有触发清除所需表的触发器的表中。

My few cents:- Adding 'deleted' column is considered change in the table structure. 我的几分钱:-添加“已删除”列被认为是表结构的更改。 Hence, not recommended unless it is new development. 因此,除非是新开发,否则不建议使用。

If it is legacy application, it is better to - add a deletion reminder to some temporary table and test that table after your update statement. 如果它是旧版应用程序,则最好-在某些临时表中添加删除提醒,并在更新语句之后测试该表。

Inside a trigger you cannot alter the table to which the trigger belongs. 在触发器内部,您无法更改触发器所属的表。
Nor can you something which indirectly alters that table. 您也无法间接更改该表。

There are however a few other things you can do. 但是,您还可以执行其他一些操作。 If you don't delete a row, but add a field deleted then you can mark it as deleted like so. 如果您不删除行,而是添加一个deleted的字段,则可以标记为已删除。

delimiter $$
drop trigger w_leb_go $$

CREATE TRIGGER w_leb_go BEFORE UPDATE ON test FOR EACH ROW
BEGIN
  IF NEW.res=3 THEN
    SET NEW.deleted = 1;
  END IF;
END$$

delimiter ;

Note that the trigger must be before if you want to alter anything in the row. 请注意,如果要更改行中的任何内容,触发器必须在触发器before

Alternatively you can add a deletion reminder to some temporary table and test that table after your update statement. 另外,您可以在某些临时表中添加删除提醒,并在更新语句之后测试该表。

CREATE TRIGGER au_test_each AFTER UPDATE ON test FOR EACH ROW
BEGIN
  IF NEW.res=3 THEN
    INSERT INTO deletion_list_for_table_test (test_id) VALUE (NEW.id);
  END IF;
END$$

Now you need to change your update statement to: 现在,您需要将更新语句更改为:

START TRANSACTION;
UPDATE test SET whatever to whichever;
DELETE test FROM test
INNER JOIN deletion_list_for_table_test dt ON (test.id = dt.test_id);
DELETE FROM deletion_list_for_table_test WHERE test_id > 0 /*i.e. everything*/
COMMIT;

Of course if you mark your rows you can simplify the deletion code to: 当然,如果您标记行,则可以将删除代码简化为:

START TRANSACTION;
UPDATE test SET whatever to whichever;
DELETE FROM test WHERE deleted = 1;
COMMIT;

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

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