2011-01-25 137 views
0

我想創建一個觸發器。首先,我創建表的員工和表employees_audit:錯誤在MySQL觸發器

CREATE TABLE employees (
    employeeNumber int(11) NOT NULL, 
    lastName varchar(50) NOT NULL, 
    firstName varchar(50) NOT NULL, 
    extension varchar(10) NOT NULL, 
    email varchar(100) NOT NULL, 
    officeCode varchar(10) NOT NULL, 
    reportsTo int(11) default NULL, 
    jobTitle varchar(50) NOT NULL, 
    PRIMARY KEY (employeeNumber) 
) 

CREATE TABLE employees_audit ( 
id int(11) NOT NULL AUTO_INCREMENT, 
employeeNumber int(11) NOT NULL, 
lastname varchar(50) NOT NULL, 
changedon datetime DEFAULT NULL, 
action varchar(50) DEFAULT NULL, 
PRIMARY KEY (id) 
) 

然後我創建觸發器:

DELIMITER $$ 
CREATE TRIGGER before_employee_update 
BEFORE UPDATE ON employees 
FOR EACH ROW BEGIN 
INSERT INTO employees_audit 
SET action = 'update', 
employeeNumber = OLD.employeeNumber, 
lastname = OLD.lastname, 
changedon = NOW(); END$$ 
DELIMITER ; 

兩個第一臺和第二個是創建,但是當我執行觸發器 我得到一個錯誤

#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 'DELIMITER $$ 
CREATE TRIGGER before_employee_update 
BEFORE UPDATE ON employe' at line 1 

在我的代碼中是否有任何錯誤,或者我是否需要另一個MySQL版本?

+0

我已更新我的答案。看起來你忽略了一些微不足道的東西。 – Nishant 2011-01-25 16:25:50

回答

0

你是從phpMyAdmin運行這個嗎?如果是這樣,分隔符應該設置在SQL textarea下方的字段中,而不是在文本區域中。

而且沒有。與已被刪除的答案相反,在DELIMITER $$之後不需要;,因爲那會將分隔符設置爲$$;

0

好吧,我已經知道你在做錯了什麼。請參見下面的執行:

mysql> CREATE TABLE employees_audit (
-> id int(11) NOT NULL AUTO_INCREMENT, 
-> employeeNumber int(11) NOT NULL, 
-> lastname varchar(50) NOT NULL, 
-> changedon datetime DEFAULT NULL, 
-> action varchar(50) DEFAULT NULL, 
-> PRIMARY KEY (id) 
-> ) 
-> 
-> DELIMITER $$ 
-> CREATE TRIGGER before_employee_update 
-> BEFORE UPDATE ON employees 
-> FOR EACH ROW BEGIN 
-> INSERT INTO employees_audit 
-> SET action = 'update', 
-> employeeNumber = OLD.employeeNumber, 
-> lastname = OLD.lastname, 
-> changedon = NOW(); END$$ 

ERROR 1064(42000):你在你的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本使用附近的「DELIM ITER $$

搖鈴正確的語法?看起來像嗎?

老弟,你有(最有可能)忘了把;年底創建表指令。下面的代碼工作:

mysql> 
    mysql> CREATE TABLE employees_audit (
     -> id int(11) NOT NULL AUTO_INCREMENT, 
     -> employeeNumber int(11) NOT NULL, 
     -> lastname varchar(50) NOT NULL, 
     -> changedon datetime DEFAULT NULL, 
     -> action varchar(50) DEFAULT NULL, 
     -> PRIMARY KEY (id) 
     -> ); 
    Query OK, 0 rows affected (0.09 sec) 

    mysql> 
    mysql> DELIMITER $$ 
    mysql> CREATE TRIGGER before_employee_update 
     -> BEFORE UPDATE ON employees 
     -> FOR EACH ROW BEGIN 
     -> INSERT INTO employees_audit 
     -> SET action = 'update', 
     -> employeeNumber = OLD.employeeNumber, 
     -> lastname = OLD.lastname, 
     -> changedon = NOW(); END$$ 
    Query OK, 0 rows affected (0.01 sec) 

    mysql> DELIMITER ; 

OLD ANSWER

試試這個

DELIMITER $$ 
CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees 
FOR EACH ROW 
BEGIN 
    INSERT INTO employees_audit 
    SET action = 'update', 
    employeeNumber = OLD.employeeNumber, 
    lastname = OLD.lastname, 
    changedon = NOW(); 
END;$$ 
DELIMITER ; 

失蹤 ; END後,我想,

編輯:固定foramtting

EDIT1:再回答。

+0

不,不應該有`;`那裏。 – Mchl 2011-01-25 09:04:06