可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
So I have a table that holds a list of cuisines and a total of how many restaurants there are with that cuisine. (e.g. Italian | 7)
I'm trying to set-up a trigger in phpMyAdmin that will increment the total every time a new restaurant is added to the database. Here is what I have so far:
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant BEGIN UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine; END;
But I keep getting an error message and it's telling me that it has to do with my Syntax. Where am I going wrong? I followed the example I saw pretty well.
Here is the error message:
#1064 - 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 'BEGIN UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE' at line 2
回答1:
do you have to have for each row
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant FOR EACH ROW UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
回答2:
You need to use FOR EACH ROW and get rid of the BEGIN...END like this:
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant FOR EACH ROW UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
BEGIN...END is only used for multiple queries in the same trigger and requires redefinition of the termination character. So your first ; is terminating the statement and leaving the BEGIN open without an END.
回答3:
try with "for each row":
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant FOR EACH ROW BEGIN UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine; END;
Haven't tried it myself so not sure if it works...