SQL 触发器插入或更新不起作用,但似乎可以单独工作

问题描述 投票:0回答:1

这是我遇到的错误 单独插入和单独更新都有效 为什么插入或更新对触发器不起作用?

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 2 行 '

OR INSERT ON review FOR EACH ROW BEGIN DECLARE ratings INT; SET ratings = NE
' 附近使用的正确语法

这是创建数据库的代码

drop database if exists nithin;
create database nithin;
use nithin;
create table artist (
    artistid int primary key,
    artistfname varchar(50),
    artistlname varchar(50),
    salary int(10)
);

create table review (
    filmid int,
    noofstarratings int,
);

INSERT INTO review (filmid, noofstarratings)
VALUES (1, 4),
       (2, 3),
       (3, 5);

delimiter / 
drop procedure if exists LimitStarRatings;
create procedure LimitStarRatings(IN ratings INT)
BEGIN
if ratings>10 then set ratings  =10; end if;
if ratings<0 then set ratings =0; end if;
select ratings;
END;
-- the query thats givin me headache
drop trigger if exists  LimitRatingTable/
create trigger LimitRatingTable 
Before insert or update on review
for each row
begin
declare ratings int;
set ratings = new.noofstarratings;
call LimitStarRatings(ratings);
new.noofstarratings=ratings;
end; /

delimiter ;

我期待它能一起工作,但它似乎单独工作ig

sql mysql triggers mysql-workbench
1个回答
0
投票

MySQL 触发器只能命名一个事件。

BEFORE INSERT OR UPDATE
没有语法。您需要为每个触发器创建一个单独的触发器,即使两个触发器主体中的代码是相同的。

© www.soinside.com 2019 - 2024. All rights reserved.