Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a MySQL trigger in phpmyadmin

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?

like image 360
Moein Hosseini Avatar asked Jul 25 '13 22:07

Moein Hosseini


1 Answers

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.

like image 84
Brainless Box Avatar answered Sep 22 '22 11:09

Brainless Box