我只想在插入触发器后创建一个以在历史表中插入新行。为什么运行查询时出现错误?
订单
create table orders
(
id int auto_increment
primary key,
id_user int not null,
picture_name varchar(100) not null,
time date not null,
constraint FK_USER
foreign key (id_user) references stef.users (id)
)
;
create index FK_USER_idx
on orders (id_user)
;
历史
create table history
(
id int auto_increment
primary key,
id_order int not null,
id_action int not null,
time date not null,
constraint FK_ORDER
foreign key (id_order) references stef.orders (id),
constraint FK_ACTION
foreign key (id_action) references stef.actions (id)
)
;
create index FK_ORDER_idx
on history (id_order)
;
create index FK_ACTION_idx
on history (id_action)
;
我的触发器
CREATE TRIGGER orders_AFTER_INSERT
AFTER INSERT ON stef.orders
FOR EACH ROW
BEGIN
INSERT INTO history('id_order', 'id_action', 'time')
VALUES (NEW.id, 1, NOW());
END;
我只想在插入触发器后创建一个以在历史表中插入新行。为什么运行查询时出现错误?
最佳答案
尝试这个
DELIMITER $$
CREATE TRIGGER orders_AFTER_INSERT
AFTER INSERT ON stef.orders
FOR EACH ROW
BEGIN
INSERT INTO history(`id_order`, `id_action`, `time`)
VALUES (NEW.id, 1, NOW());
END$$
DELIMITER ;
您需要临时重写定界符,以便MySQL可以区分触发器主体(或过程或函数)内的语句结尾与主体结尾之间的区别。
编辑:单引号(
'
)仅用于表示字符串值,因为字段名使用`(或在某些配置中为"
)关于mysql - 无法创建触发器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44681674/