dqba94394 2014-04-10 16:05
浏览 133
已采纳

创建触发器时SQL语法错误

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; 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

  • 写回答

1条回答 默认 最新

  • douci2022 2014-04-10 16:11
    关注

    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... :(

    Here my home 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?