user2570937 user2570937 - 6 months ago 21
SQL Question

Error creating MySQL Trigger

I'm trying to create this trigger but i'm getting


[Err] 1064 - 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 '; CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW BEGIN ' at line 1


delimiter $$
DROP TRIGGER IF EXISTS ca_passwd_trigger ;
$$
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END;$$
delimiter ;

Answer

You added query terminator ; with the $$ in few places. The below query is having proper delimiters.

DELIMITER $$
DROP TRIGGER IF EXISTS ca_passwd_trigger;
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
    IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
        SET NEW.passwd_modified_at = NOW();
    END IF;
END$$
DELIMITER ;
Comments