我不小心删除了触发器(需要重置数据库中的某些内容)。但是我不知道如何再次触发我的触发器..
我有 2 个表。
1:
火车信息:
2:
车轴:
现在,当火车被添加并插入到数据库表中时:train_information。我想要一个触发器来更新轴表。
触发器工作后,我希望轴表看起来像:
但是。如果 train_information 有 4 个轴。我只想把 3 放在轴表中。所以我希望循环/触发器少插入 1 个。 所以如果train_information表有20个车轴。我希望轴表显示 19 等。
最佳答案
您可以将以下触发器作为
delimiter //
create trigger train_information_ins after insert on train_information
for each row
begin
declare x int ;
if(new.number_of_axies > 0 ) then
set x = 1 ;
while x < new.number_of_axies do
insert into axle (train_id,axle)
values
(new.train_id,x);
set x=x+1;
end while ;
end if ;
end;//
delimiter ;
关于mysql - 插入后在触发器中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164606/