对一个业务用户数据使用数据泵迁移至另一个数据库,检查日志发现导入报错;
[oracle@ora19cr3-n1 dmp]$ cat mxsystem514_202005142.log|grep ORA
ORA-02291: integrity constraint (MXSYSWSGW.JOB_EXEC_PARAMS_FK) violated - parent key not found
ORA-31693: Table data object "MXSYSWSGW"."WORKPLANRECEIVESCOPE" failed to load/unload and is being skipped due to error:
······
由于有些表之间是存在主外键关联的,expdp导出的时候选择了data_only仅导出数据,impdp导入的时候会因未插入主键记录而插入外键记录,出现ORA-02291的错误,
对于这种情况可以选择先禁止主外键关联,导入后再恢复关联。 select owner,CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,R_OWNER,R_CONSTRAINT_NAME,STATUS,INVALID,VIEW_RELATED from
dba_constraints WHERE CONSTRAINT_TYPE='R' and owner='Mxx' AND TABLE_NAME IN('XXX'); OWNER CONSTRAINT_NAME C TABLE_NAME R_OWNER R_CONSTRAINT_NAME STATUS ---------- ------------------------------ - ------------------------------ ---------- -------------------- -------- Mxx Sxx_FK R BxxXT MXxxGW SYS_C0013343 ENABLED
禁用外键约束
select 'ALTER TABLE MXSYSWSGW.'||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||';'
from dba_constraints WHERE CONSTRAINT_TYPE='R' and owner='Mxx' AND TABLE_NAME IN('xxx');
启用外键约束【仅对新加入的数据强制执行约束,而不管表中的现有数据】 select 'ALTER TABLE MXSYSWSGW.'||TABLE_NAME||' ENABLE NOVALIDATE CONSTRAINT '||constraint_name||';'
from dba_constraints WHERE CONSTRAINT_TYPE='R' and owner='Mxx' AND TABLE_NAME IN('xxx');
再次导入
impdp xx/xx@mlxg directory=local_dmp dumpfile=xx%U_202005142.dmp logfile=xx_202005142.log parallel=4 cluster=no remap_schema="c##mlxg":QQSYSTEM content=data_only TABLE_EXISTS_ACTION=append TABLES="C##MLXG".XX