我无意中删除了我的触发器(需要重置数据库中的某些内容)。但我不知道怎么再扣动扳机。。
我有两张桌子。
一:
列车信息:
二:
轴:
现在,当火车被添加并插入数据库表:火车信息。我想要一个触发器来更新轴表。
在扳机启动后,我希望车轴表看起来像:
然而。如果列车信息有4个轴。我只想把3放在车轴表里。所以我希望循环/触发器少插入1个。
所以如果列车信息表有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/

10-12 00:26
查看更多