簡體   English   中英

創建觸發器時出現 SQL 語法錯誤

[英]SQL syntax error when creating 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;**

您的 SQL 語法有錯誤; 檢查與您的 MySQL 服務器版本相對應的手冊,在第 5 行的“donor”('personid','donor-id')values(new.personid,new.personid)'附近使用正確的語法

你不應該用撇號引用你的表名和字段名
使用 ` 字符。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM