mysql trigger code deploying from TOAD but not from myphpadmin or from mysql console
-
06-06-2021 - |
Question
I have a very simple trigger. I cretaed it from Toad for mysql tool and deployed it and its working perfect without any problem. when I gave it to the admin to deploy in the production server they are getting errors.
Phpmyadmin error:
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 '' at line 4
Mysql console error:
ERROR 1064 (42000): 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 1
When asked, they are running the code from phpmyadmin and also they tried to run from mysql console they are getting errors
Its really frustrating and irritating why the same thing is running from a GUI tool and not working from a webtool or a console. 3 different behaviors in from three different tools
Then I tried the same thing and I got the error too. Its suprising me why
DROP TRIGGER IF EXISTS TRG_ ;
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) into X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END;
Can some one tell me what I am doing wrong? I am not sure but I guess its something to do with delimiter keyword which I never understood the purpose.
Solution
Have a look at this question - MySQL: How do I use delimiters in triggers?
You should use delimiters when create source objects like triggers. The DELIMITER is not a MySQL statement, it a console command, and many MySQL clients supports this command. You may try this code in the MySQL console -
DROP TRIGGER IF EXISTS TRG_ ;
DELIMITER $$
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) INTO X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END$$
DELIMITER ;
As I know some old phpmyadmin versions do not support delimiters.