简体   繁体   中英

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. When ItemBorrower gets a new entry inserted, the currentDate field in the Item table should be updated to today's date. 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; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 8

I've played around with the delimeter (removing it, adding a ";" after Item.itemID), but to no apparent avail. 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. 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 .

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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