syntax if () in a trigger

0

I'm having difficulty with the if () syntax in phpmyadmin. I have to create a TRIGGER to insert information into a log when the name of an animal in the animals table is changed. But it's giving syntax error in if (). When I take the if it works. Can anyone help?

Follow the code below:

DELIMITER $$
CREATE TRIGGER log_update 
AFTER UPDATE ON animais 
FOR EACH ROW

IF ( OLD.nome_animal <> NEW.nome_animal) THEN

INSERT INTO log_animais(codigo, momento, ocorrencia) VALUES ( OLD.cod_animal, now(), OLD.nome_animal  ) 

END
 $$

DELIMITER ; 

When I put the BEGIN, END and / or END IF it also gives the error.

    
asked by anonymous 19.03.2017 / 18:56

1 answer

1

There is an error in this that is exactly in END . The END is to quit BEGIN , if you want to quit IF use END IF instead.

In a final way it would look like this:

CREATE TRIGGER log_update 
AFTER UPDATE ON animais 
FOR EACH ROW

BEGIN

 IF(OLD.nome_animal <> NEW.nome_animal) THEN

   INSERT 
    INTO log_animais(codigo, momento, ocorrencia) 
     VALUES (OLD.cod_animal, now(), OLD.nome_animal);

 END IF;

END
    
19.03.2017 / 21:44