Mysql trigger - Syntax error

Asked

Viewed 146 times

0

Would anyone know where I’m going wrong Trigger down below?

CREATE TRIGGER atualiza_produto 
AFTER INSERT 
ON item_venda FOR EACH ROW
BEGIN
UPDATE produto SET vendido = 's' WHERE id = NEW.id_produto;
END

The error message:

Você tem um erro de sintaxe no seu SQL próximo a '' na linha 5

1 answer

0


The MySql has the ; end of each query, in which case he’s trying to execute two querys rather than one, precisely because the ; is separating.

To prevent this from happening, it is necessary to show MySql what the ; is not finalizing the query and yes it is part of the context, for that there is the DELIMITER, in your case would look like this:

DELIMITER $$
    CREATE TRIGGER atualiza_produto AFTER INSERT ON item_venda FOR EACH ROW
    BEGIN
        UPDATE produto SET vendido = 's' WHERE id = NEW.id_produto;
    END
$$

Anything that stands between DELIMITER $$ and $$ shall be executed jointly and not by separating querys each ;.

See more here.

  • Thanks, it worked! One more thing: would you know how to turn off the Constraints? I need to give Truncate of my entire database before climbing to production but apparently set_checked_constraints = 0 does not work in mysql 5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.