我正在尝试触发一个触发器,以检查更新后员工的薪水是否低于其经理的薪水,如果更新,则中止更新。
CREATE TRIGGER Employee_BeforeUpdate
BEFORE UPDATE ON Employee
FOR EACH ROW
BEGIN
IF EXISTS (
SELECT * FROM Manager, Manages
WHERE NEW.eid = Manages.eid
AND Manager.mid = Manages.mid
AND NEW.salary > Manager.salary)
THEN SIGNAL SQLSTATE '45000' SET message_text = 'Error message';
END IF;
END;
我在第9行的''附近有语法错误(1064),我不知道为什么。也是触发触发器的正确方法吗?
(MySql版本5.7.23-0ubuntu.18.04.1)
CREATE TABLE Employee (eid char(5), salary integer, primary key(eid));
CREATE TABLE Manager (mid char(5), salary integer, primary key(mid));
CREATE TABLE Manages (mid char(5), eid char(5) , primary key (mid,eid),
foreign key(mid) references Manager(mid),
foreign key(eid) references Employee(eid));
答案 0 :(得分:1)
$$
),而不是({;
)。DROP TRIGGER IF EXISTS
)DELIMITER
重新定义为;
将触发器更改如下:
DELIMITER $$
DROP TRIGGER IF EXISTS Employee_BeforeUpdate $$
CREATE TRIGGER Employee_BeforeUpdate
BEFORE UPDATE ON Employee
FOR EACH ROW
BEGIN
IF EXISTS (
SELECT * FROM Manager, Manages
WHERE NEW.eid = Manages.eid
AND Manager.mid = Manages.mid
AND NEW.salary > Manager.salary)
THEN SIGNAL SQLSTATE '45000' SET message_text = 'Error message';
END IF;
END $$
DELIMITER ;