简体   繁体   English

如何更正MySQL“插入前”触发器的语法?

[英]How to correct a MySQL “BEFORE INSERT” trigger's syntax?

I'm creating a trigger from the table ItemBorrower that is meant to update a field in the table Item. 我正在从表ItemBorrower创建一个触发器,该触发器用于更新表Item中的字段。 When ItemBorrower gets a new entry inserted, the currentDate field in the Item table should be updated to today's date. 当ItemBorrower插入新条目时,Item表中的currentDate字段应更新为今天的日期。 Here is my trigger: 这是我的触发器:

CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower

FOR EACH ROW UPDATE Item
BEGIN
    SET Item.checkoutDate = CURDATE()
    WHERE NEW.itemID = Item.itemID

END;

And there error I'm getting is about the last line: 我得到的错误是关于最后一行:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 8 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第8行的“ END”附近使用

I've played around with the delimeter (removing it, adding a ";" after Item.itemID), but to no apparent avail. 我试过了定界符(将其删除,在Item.itemID之后添加“;”),但没有任何用处。 Could be that I just did that totally wrong, too. 可能是我也完全犯错了。

Any help appreciated! 任何帮助表示赞赏!

Thanks, 谢谢,

Jona 乔纳

I am not 100% sure what are you trying to accomplish from the trigger. 我不是100%知道您要如何从触发器中实现目标。 I assumed that you have a table called item and you want to update the checkoutDate column with current date where the item.itemID = the last insert id in ItemBorrower . 我假设你有一个表称为项目,要更新checkoutDate与在当前日期列item.itemID = the last insert idItemBorrower

Try this code 试试这个代码

delimiter //
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW
BEGIN
    UPDATE Item
    SET checkoutDate = CURDATE()
    WHERE itemID = NEW.itemID;

END; //
delimiter ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM