如何在phpmyadmin中创建MySQL触发器

时间:2021-10-22 09:06:55

I want to create a trigger in MySQL. I run following commands:

我想在MySQL中创建一个触发器。我运行以下命令:

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:

但是当我在phpmyadmin中运行SQL时,我收到此错误:

#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?

这有什么不对?如何创建触发器?

1 个解决方案

#1


10  

This is caused by not changing the delimiters temporarily as you did via CLI. Try either:

这是因为您没有像通过CLI那样暂时更改分隔符。尝试:

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.

看:这个问题和这个问题。

#1


10  

This is caused by not changing the delimiters temporarily as you did via CLI. Try either:

这是因为您没有像通过CLI那样暂时更改分隔符。尝试:

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.

看:这个问题和这个问题。