1対多の関係を持つ2つのテーブルがあり、次の挿入が必要です。
A - source table
nr Name
1 a
1 b
1 c
2 d
2 e
546 abc
546 asd
546 qwe
B - results table
FK_ID Name
...
6 a
6 b
6 c
7 d
7 e
8 abc
8 asd
8 qwe
C - table with unique IDs
ID
...
5
6 (new)
7 (new)
8 (new)
ソーステーブルから行を取得して結果テーブルに挿入し、グループごとに、一意のIDでテーブルに行を挿入し、結果テーブルに挿入された行を更新して、BとCの関係を確立する必要があります(IDはCでの自動インクリメント)。
BEFORE / AFTER INSERTトリガーを作成する必要がありますか、それともより高速な方法がありますか?(> 100k行)
編集:
Bから外部キーを削除したので、Bに何でも挿入できますが、ステップ2は今ではやりすぎです(3k行/ 10分)。
-- step 1
insert into B(..., helperColumn)
select ..., 1 from A;
-- step 2
myloop: WHILE true DO
set @updateID = (select ID from B where helperColumn = 1 limit 1);
if @updateID is null then
LEAVE myloop;
end if;
insert into C(...)
values(...);
set @id = LAST_INSERT_ID();
update B
set ID = @id, helperColumn = 0
where ID = @updateID
and helperColumn = 1;
END WHILE;