触发多个动作和循环
trigger with multiple action and loop
我的数据库中有 3 个 table。
- train_information
- 车轴
- 转向架
当我填写表格时,信息会填入 train_information
table,然后看起来像这样:
现在,您看到 number_of_axles
是 4。
tables axle
和 bogie
看起来像这样。
轮轴:
转向架:
如您所见,触发器插入 3 只用于轴 table。但是在转向架table,我想加4.
举个例子:
这一切也需要在 1 个触发器中。因为 mysql 版本不支持具有相同操作的多个触发器(插入后)。
轴触发器已经在工作。这是它的样子:
您只需在现有触发器中添加另一个循环即可,如
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 ;
我的数据库中有 3 个 table。
- train_information
- 车轴
- 转向架
当我填写表格时,信息会填入 train_information
table,然后看起来像这样:
现在,您看到 number_of_axles
是 4。
tables axle
和 bogie
看起来像这样。
轮轴:
转向架:
如您所见,触发器插入 3 只用于轴 table。但是在转向架table,我想加4.
举个例子:
这一切也需要在 1 个触发器中。因为 mysql 版本不支持具有相同操作的多个触发器(插入后)。
轴触发器已经在工作。这是它的样子:
您只需在现有触发器中添加另一个循环即可,如
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 ;