mysql 触发器错误
我正在尝试创建一个触发器。首先,我创建了表Employees和表employees_audit:
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 版本吗?
I am trying to create a trigger. First of all I created the table employees and the table employees_audit:
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)
)
Then I created the trigger:
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 ;
Both First table and Second is created but when I execute the trigger
I get an error
#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
Is there any error in my code or do I need another MySQL version?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是从 phpMyAdmin 运行的吗?如果是这样,分隔符应设置在 SQL 文本区域下方的字段中,而不是文本区域本身。
没有。与已删除的答案相反,在
DELIMITER $$
之后不需要;
因为这会将分隔符设置为$$;
Are you running this from phpMyAdmin? If so the delimiter should be set in a field below the SQL textarea not in the text area itself.
And no. Contrary to the answer that has been deleted, there is no
;
needed afterDELIMITER $$
because that would set the delimiter to$$;
好吧,我知道你做错了什么。参见下面的执行:
敲响了警钟?看起来很相似?
兄弟,您(很可能)忘记在 create table 指令的末尾添加
;
。下面的代码有效:旧答案
尝试这个missing
;
afterEND
,我猜,编辑:修复格式
Edit1:重新答复。
well, I have got what you are doing wrong. See the execution below:
RANG A BELL? LOOK SIMILAR?
Buddy, you have (most likely) forgotten to put a
;
at the end of create table directive. The below code works:OLD ANSWER
try thismissing
;
afterEND
, I guess,Edit: fixed foramtting
Edit1: re-answered.