简体   繁体   中英

MySQL Before insert trigger syntax error?

I'm trying to create a simple Before Insert MySQL Trigger, to check if any duplicates exists in the table. But I get syntax errors in the lines with the (*) . What's wrong ?

       delimiter ;

(*)    CREATE TRIGGER `BookLanguages_BeforeInsertTrigger`
       BEFORE INSERT ON `BookLanguages`
       FOR EACH ROW
       BEGIN
            IF (exists(select * from Languages bl where bl.BookID = new.BookID and bl.LanguageID = new.LanguageID)) THEN
               SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred';
            END IF;
(*)    END;

I'm using the Community server v5.5.21

Use a different delimiter, for example:

DELIMITER //

...and then use // in your END // line.

The reason being that the semicolon in your second-last line is sending everything up to that point back to the server, so the server doesn't see your final END; and creates the trigger without seeing that last line. That's a syntax error, in its view.

The purpose of the DELIMITER command in this context is to distinguish between the end of each statement in the trigger (for the server), and the end of the trigger (for the client to send it to the server). While they're different, you can still use semicolons in the body of the trigger without the client thinking you've terminated the statement.

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