我正在尝试在MySQL社区服务器5.5.16中创建更新触发器。我有两张桌子:
create table sales
(ono integer primary key,
dnr integer not null,
osum integer);
create table salessum
(dnr integer primary key,
dsum integer);
alter table sales
add constraint fk_sales_salessum foreign key (dnr)
references salessum (dnr);
我需要在表“sales”中的任何更新后更新表“salessum”。我创建了触发器:
Create trigger up_to_date
after update on sales
for each row
begin
update salessum
set dsum = dsum + new.osum;
where dnr=new.dnr;
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 'where
dnr=new.dnr' at line 1
任何人都可以帮助我。感谢。
答案 0 :(得分:3)
你的触发器中有一个额外的;
,并且由于触发器包含分号,你需要临时更改分隔符以添加触发器;
delimiter //
Create trigger up_to_date
after update on sales
for each row
begin
update salessum
set dsum = dsum + new.osum
where dnr=new.dnr;
end//
delimiter ;
答案 1 :(得分:2)
您在;
子句之后有SET
错误:
update salessum
set dsum = dsum + new.osum; <--- Remove that
where dnr=new.dnr;
每当MySQL告诉您检查手册以获取在xxx 附近使用的正确语法时,请先查看 xxx 之前的字符,看看是否有错误。在这种情况下,就在WHERE
子句开始之前。
答案 2 :(得分:1)
您的语法中有一个;
太多
Create trigger up_to_date
after update on sales
for each row
begin
update salessum
set dsum = dsum + new.osum /*removed ; here */
where dnr=new.dnr;
end;
不要忘记触发器周围的分隔符
示例:
delimiter //
CREATE TRIGGER ...
BEGIN
...
END;
//
delimiter;