我们在日常维护数据库过程中,经常会通过exp、expdp,imp、impdp等导入导出数据。但通过这种方式进行的数据迁移,如果使用到序列,由于序列缓存(cache)的问题,如果使用序列做主键,经常会发生保存不了(违反唯一约束的问题),处理起来也比较麻烦。
方法1:
针对此类问题的处理,我总结了一下。最好的方法是在导入前,将序列的信息提取出来,生产sql脚本。导入完成后,删除导入的序列,通过sql脚本重建序列即可。
sql脚本如下:
--导出用户下执行,执行结果存成sql文件
select 'create sequence '||sequence_name||
' minvalue '||min_value||
' maxvalue '||max_value||
' start with '||last_number||
' increment by '||increment_by||
(case when cycle_flag='N' then '' else ' cycle ' end) ||
(case when order_flag='N' then '' else ' order ' end) ||
(case when cache_size=0 then ' nocache' else ' cache '||cache_size
end) ||';'
from USER_sequences;