简体   繁体   English

mySQL8.0触发语句

[英]mySQL8.0 trigger statements

I am trying to create as following given: 我正在尝试创建如下给定:

Create a trigger on the Company table after an update that sets a company's budget to 0 if it is less than 0. 更新后将公司预算设置为0(如果小于0的话),则在Company表上创建触发器。

But I struggle with errors such as being unable to update, set the table. 但是我为无法更新表等错误而苦苦挣扎。 I know it is because of I need to change from 'after update' to 'before update'. 我知道这是因为我需要从“更新后”更改为“更新前”。 But I cannot dismiss the expected code. 但是我无法消除预期的代码。 What could I do? 我能做什么?

The mySQL code I have is: 我拥有的mySQL代码是:

 delimiter $$

 drop procedure if exists after_company_update $$ 

 create trigger after_company_update after update on Company 

 for each row begin 

 update Company set budget = 0 where budget < 0; 

 end $$ 

 delimiter ;

Error: ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG: Can't update table 'Company' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 错误:ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG:无法更新存储函数/触发器中的表“ Company”,因为调用该存储函数/触发器的语句已使用该表。

You can't update the whole table which is already used by the trigger but you can update the column which is been modified. 您无法更新触发器已使用的整个表,但可以更新已修改的列。

Assuming your table has Primary Key which is company_id 假设您的表的主键为company_id

Like 喜欢

update Company 
set budget = 0 
where company_id=new.company_id and budget < 0;

'In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated' - https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html “在UPDATE触发器中,可以在更新前使用OLD.col_name引用行的列,而在更新后可以使用NEW.col_name引用行的列” -https://dev.mysql。 com / doc / refman / 8.0 / en / trigger-syntax.html

A before trigger would work but I'm not sure that it would be appropriate to have a trigger at all - do you really want to do this every time an update occurs to company (or only on specific occasions) , for example would it be appropriate if you changed address(assuming address is in the company table) 之前的触发器会起作用,但是我不确定完全有触发器是合适的-您是否真的想在公司发生每次更新时(或仅在特定情况下)执行此操作,例如如果您更改了地址(假设地址在公司表中),则适用

delimiter $$
 drop trigger if exists after_company_update $$ 
 create trigger after_company_update BEFORE update on Company 
 for each row begin 
   set NEW.budget = 0 where OLD.budget < 0; 
 end $$ 
DELIMITER ;

Such a trigger should logically be done before the update rather than afterwards: 从逻辑上讲,这种触发应该在更新之前而不是之后进行:

delimiter $$

drop trigger if exists before_company_update $$ 

create trigger before_company_update after update on Company 
for each row
begin 
    if old.budget < 0 then
        set new.budget := 0
    end if;
end $$ 

delimiter ;

You can express this as an after update trigger: 您可以将其表示为update 触发器:

create trigger after_company_update after update on Company 
for each row
    update Company
        set budget = 0
        where Company.Id = new.Id;

The syntax works, but the trigger doesn't. 语法有效,但触发器无效。 It returns the error: 它返回错误:

Can't update table 'Company' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 无法更新存储函数/触发器中的表“公司”,因为调用该存储函数/触发器的语句已使用该表。

You can see this here . 你可以在这里看到。

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

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