插入或更新之前的错误

Error for before insert or update on

我是 dbms 查询的新手,所以如果有人能弄清楚应该在这里做什么。另外,我正在使用 Mysql。

代码:

-> create trigger age_trigger before insert or update of age on employee
-> for each row
-> when( new.age <25)
-> begin
-> select 'Age can not be less than 25'
-> end;

错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'or update of age on employee for each row when( new.age <25) begin select 'Age c' at line 1

你不能想出自己的语法:)

看看说明书:CREATE TRIGGER Syntax

您不能指定多个 trigger_events。您需要单独的插入和更新触发器。而这个of age也没有生意。

总而言之,你想做的事情可以在 MySQL 中完成(google 对于 SIGNAL,如果你坚持的话),但最好的做法是,这种逻辑放在应用层。不要把这样的逻辑放在数据库中。

下面可以是实施BEFORE INSERT以获得您想要的结果的选项之一。

CREATE TRIGGER age_trigger
BEFORE INSERT ON table1
FOR EACH ROW 
BEGIN 
IF NEW.age < 25 
THEN SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Age can not be less than 25'; 
END IF; 
END;

类似的语法可用于 before update 触发器。

DEMO