I want to create a trigger in MySQL. I run following commands:
mysql> delimiter //
mysql> CREATE TRIGGER before_insert_money BEFORE INSERT ON money
-> FOR EACH ROW
-> BEGIN
-> UPDATE accounts SET balance=10.0;
-> END;
-> //
Query OK, 0 rows affected (0.19 sec)
But when I run above SQL in phpmyadmin I get this 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
what's wrong here? How do I create a trigger?
This is caused by not changing the delimiters temporarily as you did via CLI. Try either:
CREATE TRIGGER before_insert_money BEFORE INSERT ON money
FOR EACH ROW UPDATE accounts SET balance=10.0;
or
delimiter //
CREATE TRIGGER before_insert_money BEFORE INSERT ON money
FOR EACH
ROW
BEGIN
UPDATE accounts SET balance=10.0;
END;
//
delimiter ;
See: This question and this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With