我想創建一個觸發器。首先,我創建表的員工和表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版本?
我已更新我的答案。看起來你忽略了一些微不足道的東西。 – Nishant 2011-01-25 16:25:50