创建触发器时出现此错误
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;**
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第5行使用''donor'('personid','donor-id')值(new.personid,new.personid)附近的正确语法
答案 0 :(得分:1)
你不应该用撇号引用你的表名和字段名 使用`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 ;
我的SQLFiddle没有用...... :(
这里是我的主页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)