在迁移容器的过程中,如果有两个表;
TABLE_MAPPING (old_value, new_value)
TABLE_USING (value, data...)
TABLE_USING
正在引用(FK)不相关表中的容器。TABLE_MAPPING
是临时用于迁移的,目标是将内容从已弃用的容器移到新的容器。
这里的问题是,有时容器不仅被替换,而且被拆分为多个新容器,例如TABLE_MAPPING
可能包含:
OLD_VALUE | 新价值 |
---|---|
1个 | 10 |
1个 | 11 |
2 | 20 |
该查询将导致将值“ 1”的一行“更新”为值“ 10”和“ 11”的两行。
有没有简单的SQL方法可以做到这一点?还是应该使用PL / SQL?
编辑:根据要求,这是使用TABLE_MAPPING
上述方法之前/之后的示例
之前:
值 | IRRELEVANT_COLUMNS ... |
---|---|
1个 | ... |
2 | ... |
后:
值 | IRRELEVANT_COLUMNS ... |
---|---|
10 | ... |
12 | ... |
20 | ... |
您需要两个步骤。在下面,我首先插入所有新行,然后删除所有旧行。
-- insert rows with new values
insert into table_using (value, data ...)
select m.new_value, u.data ...
from table_using u
join table_mapping m on m.old_value = u.value;
-- delete rows with old values
delete from table_using where value in (select old_value from table_mapping);
-- commit the transaction
commit;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句