创建触发器时出错
这是我试图创建的一个简单的触发器:
CREATE TRIGGER add_item_id BEFORE INSERT ON products
FOR EACH ROW
BEGIN
DECLARE max_id INTEGER;
SELECT MAX(item_id) INTO @max_id FROM products;
SET NEW.item_id = @max_id + 1;
END;
我在 phpMyAdmin SQL 窗口和 mysql 提示符上都尝试了它,并得到了如下相同的错误:
#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 '' at line 4
This is a simple trigger I'm trying to create:
CREATE TRIGGER add_item_id BEFORE INSERT ON products
FOR EACH ROW
BEGIN
DECLARE max_id INTEGER;
SELECT MAX(item_id) INTO @max_id FROM products;
SET NEW.item_id = @max_id + 1;
END;
I tried it both on phpMyAdmin SQL window and mysql prompt and get the same error as below:
#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 '' at line 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些注意事项:
delimiter //
,MySQL 会看到 CREATE TRIGGER 语句以..FROM PRODUCTS;
结尾,这使其无效Some notes:
delimiter //
, MySQL sees the CREATE TRIGGER statement ending at..FROM PRODUCTS;
, which makes it invalid您还可以这样做:
注意:您可以在几乎所有 RDBMS 中声明 auto_incremented 字段。
You could also do:
Note: you can declare auto_incremented fields in almost all RDBMS.