简体   繁体   English

创建触发器时出现 SQL 语法错误

[英]SQL syntax error when creating trigger

This error appear when I create the trigger创建触发器时出现此错误

CREATE TRIGGER addperson after insert on person
for each row 
begin 
if(new.persontype='donor') then
  insert into donor('personid','donor-id')values(new.personid,new.personid);
  else 
  insert into enterpreneur('personid','enter-id') values(new.personid,new.personid);
 end;**

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''donor' ('personid','donor-id')values(new.personid,new.personid)' at line 5检查与您的 MySQL 服务器版本相对应的手册,在第 5 行的“donor”('personid','donor-id')values(new.personid,new.personid)'附近使用正确的语法

You should not to quote your tablenames and fieldnames with apostroph你不应该用撇号引用你的表名和字段名
Use ` character.使用 ` 字符。

delimiter //
CREATE TRIGGER addperson AFTER INSERT ON person
FOR EACH ROW 
begin 
  IF NEW.persontype = 'donor' THEN
    insert into `donor`(`personid`,`donor-id`)values(new.personid,new.personid);
  ELSE
    insert into `enterpreneur`(`personid`,`enter-id`) values(new.personid,new.personid);
  END IF;
END;//
delimiter ;

My SQLFiddle not worked... :(我的 SQLFiddle 不起作用... :(

Here my home mysql:这是我家的mysql:

$ mysql
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> CREATE TABLE person (a varchar(25));
Query OK, 0 rows affected (0.47 sec)

mysql> CREATE TABLE donor (a varchar(25));
Query OK, 0 rows affected (0.05 sec)

mysql> CREATE TABLE enterpreneur (a varchar(25));
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> delimiter //
mysql> CREATE TRIGGER addperson AFTER INSERT ON person
    -> FOR EACH ROW 
    -> begin 
    ->   IF NEW.a = 'donor' THEN
    ->     insert into `donor`(`a`)values(new.a);
    ->   ELSE
    ->     insert into `enterpreneur`(`a`) values(new.a);
    ->   END IF;
    -> END;//
Query OK, 0 rows affected (0.03 sec)

mysql> delimiter ;
mysql> 
mysql> 
mysql> INSERT INTO person VALUES ('donor'), ('not a donor');
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from donor;
+-------+
| a     |
+-------+
| donor |
+-------+
1 row in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM