【问题标题】:trigger with multiple action and loop触发多个动作和循环
【发布时间】:2015-07-22 23:26:32
【问题描述】:

我的数据库中有 3 个表。

- train_information
- 车轴
- 转向架

当我填写表格时,信息会填写在train_information 表中,然后看起来像这样:

现在,您看到 number_of_axles 是 4。

axle 和 bogie 表如下所示。

车轴:

转向架:

如您所见,触发器插入了 3,它应该只用于轴表。但在转向架表中,我想添加 4。
这里举个例子:

这一切都需要在 1 个触发器中。因为mysql版本不支持同一个动作的多个触发器(插入后)。

轴触发器已经工作。看起来是这样的:

【问题讨论】:

  • 还有什么问题?
  • 您的问题/问题到底是什么?
  • 什么是转向架表中的axle_nr 它的值是如何计算的?
  • 在 train_information 表中,您会看到 4 个车轴。我希望将它们放置在轴nr 行的转向架表中。就像现在一样,但还有 1 个。所以转向架表中的axis_nr就是列车信息表的轴数。
  • 因此您可以在 train_information 中创建一个 AFTER INSERT 触发器,然后在 Bogie 表中创建另一个 BEFORE INSERT 以检查是否存在。好吧,如果那是你的问题,因为我真的不明白。

标签: php mysql sql oracle triggers


【解决方案1】:

您只需要在现有触发器中添加另一个循环即可

delimiter //
create trigger train_information_ins after insert on train_information
for each row 
begin
 declare x int ;
 declare y 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 ;

    set y=1;
    while y <= new.number_of_axies do
      insert into bogie (train_id,axle_nr)
      values
      (new.train_id,y); 
       set y=y+1;
     end while ; 
  end if ;
end;//

delimiter ;

【讨论】:

  • 非常感谢! (再次哈哈)
猜你喜欢
  • 2011-05-13
  • 2023-01-07
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
  • 2017-04-12
  • 1970-01-01
相关资源
最近更新 更多