[雜七雜八的小問題]impdp外鍵沒有導入的問題


0. summary

1. 問題背景和初步分析
2. 問題模擬
.   2.1 環境準備
.   2.2 模擬導入父表的數據
.   2.3 模擬導入子表的數據
.   2.4 模擬導入父表和子表的數據
3. 結論

1. 問題背景和初步分析

應用一套系統需要遷移,遷移的內容包含指定的用戶下的所有對象,其中部分用戶下的表只需要導指定的數據。想著方便點,就先導出metadata, 然后將需要數據的表單獨用parfile導出,導入的時候先導入metadata保證全部對象存在,再導入需要數據的表,使用replace的選項。沒有使用metadata+data_only的原因是導入時需要關閉外鍵,再打開,驗證很耗時,雖然不驗證一般也不會有問題。

測試過程中沒有報錯,但是應用在檢查過程中,發現有兩個外鍵沒有導入。使用了應用提供的語句查看了下確實如此。

select * from dba_constraints a where a.constraint_name in ('FK_ALARM_LH_REFERENCE_ALARM_TY','FK_ALARM_LO_REFERENCE_ALARM_TY');

從約束的last_change時間來看,不會是新建的。首先我嘗試了下重新導出個metadata,然后導入自己的測試庫,發現約束是有的。然后再嘗試導入需要數據的表時,在沒有導完時,發現這兩個外鍵約束沒有了,意識到可能是導入引起的問題。我們知道,在使用impdp完成數據庫導入時,若表已經存在,有四種的處理方式:

  • skip : 默認操作
  • replace : 先drop表,然后創建表,最后插入數據
  • append : 在原來數據的基礎上增加數據
  • truncate : 先truncate, 然后再插入數據

我使用的是replace操作,導入的時候會刪除原表,為什么不使用truncate, 使用truncate和使用結構+數據的方式實質是一樣的,同樣需要關閉外鍵。仔細檢查需要導出的表清單以及該約束所屬的對象,發現一個問題,應用需要導入父表的數據,而子表的數據不需要導入,那么是不是drop動作引起的,我嘗試了下手工drop父表,是無法drop的。而replace可以強制刪除掉。

2. 問題模擬

2.1 環境準備

#### 創建子表 ####

create table fk_t as select * from user_objects;
delete from fk_t where object_id is null;
commit;

#### 創建父表 ####

create table pk_t as select * from user_objects;
delete from pk_t where object_id is null;
commit;

#### 創建父表的主鍵 ####

alter table PK_t add constraint pk_pktable primary key (OBJECT_ID);

#### 創建子表的外鍵 ####

alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);

#### 表數據 ####

PANGZI@panda>select count(*) from pk_t;

  COUNT(*)
----------
         3

PANGZI@panda>select count(*) from fk_t;

  COUNT(*)
----------
         2

2.2 模擬導入父表的數據

#### 導出父表和子表的數據 ####

expdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=exp_test.log tables=pangzi.pk_t,pangzi.fk_t;

#### 刪除父表和子表數據,相當于表結構已經存在沒有數據的情況 ####

PANGZI@panda>truncate table fk_t;

Table truncated.

PANGZI@panda>truncate table pk_t;
truncate table pk_t
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys


PANGZI@panda>alter table fk_t disable constraint fk_fktable;

Table altered.

PANGZI@panda>truncate table pk_t;

Table truncated.

PANGZI@panda>alter table fk_t enable constraint fk_fktable;

Table altered.

PANGZI@panda>col owner for a30
PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

OWNER                          CONSTRAINT_NAME                TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PANGZI                         FK_FKTABLE                     FK_T                           ENABLED

#### 使用replace導入 ####

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=replace

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 14:55:39 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=replace 
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Job "SYS"."SYS_IMPORT_TABLE_01" successfully completed at Tue Mar 7 14:55:41 2017 elapsed 0 00:00:02

成功導入,檢查下約束,發現確實沒有了。

PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

no rows selected

這是正常的,因為正常情況下,drop父表會報錯,而replace會強制刪除父表,父表沒有了,那么約束也就沒用了。

#### 使用truncate導入 ####

drop table fk_t purge;
drop table pk_t purge;
create table fk_t as select * from user_objects where 1=2;
create table pk_t as select * from user_objects where 1=2;
alter table PK_t add constraint pk_pktable primary key (OBJECT_ID);
alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=truncate

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:03:27 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=truncate 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."PK_T" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Job "SYS"."SYS_IMPORT_TABLE_01" successfully completed at Tue Mar 7 15:03:30 2017 elapsed 0 00:00:03

發現僅導入父表時,truncate是可以的,檢查下表數據和約束狀態正常。

PANGZI@panda>select count(*) from pk_t;

  COUNT(*)
----------
         3

PANGZI@panda>select count(*) from fk_t;

  COUNT(*)
----------
         0

PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

OWNER                          CONSTRAINT_NAME                TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PANGZI                         FK_FKTABLE                     FK_T                           ENABLED

#### 使用append導入 ####

drop table fk_t purge;
drop table pk_t purge;
create table fk_t as select * from user_objects where 1=2;
create table pk_t as select * from user_objects where 1=2;
alter table PK_t add constraint pk_pktable primary key (OBJECT_ID);
alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=truncate

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:03:27 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.pk_t table_exists_action=truncate 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."PK_T" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Job "SYS"."SYS_IMPORT_TABLE_01" successfully completed at Tue Mar 7 15:03:30 2017 elapsed 0 00:00:03

發現僅導入父表的情況下,檢查下表數據和約束狀態,append同樣不存在問題。

PANGZI@panda>select count(*) from pk_t;

  COUNT(*)
----------
         3

PANGZI@panda>select count(*) from fk_t;

  COUNT(*)
----------
         0

PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

OWNER                          CONSTRAINT_NAME                TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PANGZI                         FK_FKTABLE                     FK_T                           ENABLED

2.3 模擬導入子表的數據

#### 使用replace導入 ####

drop table fk_t purge;
drop table pk_t purge;
create table fk_t as select * from user_objects where 1=2;
create table pk_t as select * from user_objects where 1=2;
alter table PK_t add constraint pk_pktable primary key (OBJECT_ID);
alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=replace

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:10:22 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=replace 
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "PANGZI"."FK_T"                             10.45 KB       2 rows
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
ORA-39083: Object type REF_CONSTRAINT failed to create with error:
ORA-02298: cannot validate (PANGZI.FK_FKTABLE) - parent keys not found
Failing sql is:
ALTER TABLE "PANGZI"."FK_T" ADD CONSTRAINT "FK_FKTABLE" FOREIGN KEY ("OBJECT_ID") REFERENCES "PANGZI"."PK_T" ("OBJECT_ID") ENABLE
Job "SYS"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Mar 7 15:10:24 2017 elapsed 0 00:00:01

發現數據雖然導入了,但是外鍵約束失敗了。

PANGZI@panda>select count(*) from pk_t;

  COUNT(*)
----------
         0

PANGZI@panda>select count(*) from fk_t;

  COUNT(*)
----------
         2

PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

no rows selected

PANGZI@panda>alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);
alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID)
                                *
ERROR at line 1:
ORA-02298: cannot validate (PANGZI.FK_FKTABLE) - parent keys not found

這種情況很好理解,因為父表沒數據,當然無法建立外鍵。

#### 使用truncate導入 ####

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=truncate

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:12:32 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=truncate 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."FK_T" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "PANGZI"."FK_T" failed to load/unload and is being skipped due to error:
ORA-02291: integrity constraint (PANGZI.FK_FKTABLE) violated - parent key not found
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SYS"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Mar 7 15:12:33 2017 elapsed 0 00:00:01

無法執行,同樣是因為父表無數據,當然,這里不存在刪表,所以約束還在。

#### 使用append導入 ####

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=append

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:37:47 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t table_exists_action=append 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."FK_T" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "PANGZI"."FK_T" failed to load/unload and is being skipped due to error:
ORA-02291: integrity constraint (PANGZI.FK_FKTABLE) violated - parent key not found
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SYS"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Mar 7 15:37:49 2017 elapsed 0 00:00:01

append也是一個道理。

2.4 模擬導入父表和子表的數據

#### 使用replace導入 ####

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=replace

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:39:24 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=replace 
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "PANGZI"."FK_T"                             10.45 KB       2 rows
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SYS"."SYS_IMPORT_TABLE_01" successfully completed at Tue Mar 7 15:39:26 2017 elapsed 0 00:00:01

這種情況下,replace沒有任何問題,數據和約束都正常,這也是常用的導入方式。

PANGZI@panda>select count(*) from pk_t;

  COUNT(*)
----------
         3

PANGZI@panda>select count(*) from fk_t;

  COUNT(*)
----------
         2

PANGZI@panda>select owner, constraint_name, table_name, status
  2    from dba_constraints a
  3   where a.constraint_name in upper('fk_fktable');

OWNER                          CONSTRAINT_NAME                TABLE_NAME                     STATUS
------------------------------ ------------------------------ ------------------------------ --------
PANGZI                         FK_FKTABLE                     FK_T                           ENABLED

#### 使用truncate導入 ####

drop table fk_t purge;
drop table pk_t purge;
create table fk_t as select * from user_objects where 1=2;
create table pk_t as select * from user_objects where 1=2;
alter table PK_t add constraint pk_pktable primary key (OBJECT_ID);
alter table FK_t add constraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=truncate

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 15:41:45 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=truncate 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."FK_T" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Table "PANGZI"."PK_T" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "PANGZI"."FK_T" failed to load/unload and is being skipped due to error:
ORA-02291: integrity constraint (PANGZI.FK_FKTABLE) violated - parent key not found
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SYS"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Mar 7 15:41:46 2017 elapsed 0 00:00:01

報錯很明顯,父表的數據導入了,但是子表因為外鍵的關系,數據無法導入,這也是通常情況下為什么不使用結構+數據導入方式的原因,需要關閉外鍵約束導入,導入之后再啟用外鍵,存在是否校驗的問題。比如:

select 'ALTER TABLE '||owner||'.'||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||';' 
from dba_constraints WHERE CONSTRAINT_TYPE='R' and owner = upper('crm');

select 'ALTER TABLE '||owner||'.'||TABLE_NAME||' ENABLE NOVALIDATE CONSTRAINT '||constraint_name||';' 
from dba_constraints WHERE CONSTRAINT_TYPE='R' and owner = upper('crm');

#### 使用append導入 ####

[oracle@stb11g pump]$ impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=append

Import: Release 11.2.0.4.0 - Production on Tue Mar 7 16:33:51 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" DIRECTORY=DATA_PUMP_DIR dumpfile=test.dmp logfile=imp_test.log tables=pangzi.fk_t,pangzi.pk_t table_exists_action=append 
Processing object type TABLE_EXPORT/TABLE/TABLE
Table "PANGZI"."FK_T" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Table "PANGZI"."PK_T" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "PANGZI"."FK_T" failed to load/unload and is being skipped due to error:
ORA-02291: integrity constraint (PANGZI.FK_FKTABLE) violated - parent key not found
. . imported "PANGZI"."PK_T"                             10.52 KB       3 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SYS"."SYS_IMPORT_TABLE_01" completed with 1 error(s) at Tue Mar 7 16:33:53 2017 elapsed 0 00:00:01

append也是一樣的

3. 結論

簡單點,在表結構存在的前提下,如果僅導入其中一張表的數據,table_exists_action無論使用什么選項,都可能存在問題(append雖然對導入父表沒有問題,但是整體導入一般不會是僅存在父表的導入,針對其他父子表都需要導入的情況需要關閉外鍵)。

#### 檢查是否存在導入父表數據而不導入數據的子表的腳本 ####

select dc.owner,
       dc.constraint_name,
       dc.constraint_type,
       dc.table_name,
       dc.status
  from dba_constraints dc,
       (select b.owner, b.constraint_name
          from v_exptab_check a, dba_constraints b
         where a.owner = b.owner
           and a.table_name = b.table_name
           and b.constraint_type = 'P') x,
       v_exptab_check v
 where dc.r_owner = x.owner
   and dc.r_constraint_name = x.constraint_name
   and dc.constraint_type = 'R'
   and dc.owner = v.owner(+)
   and dc.table_name = v.table_name(+)
   and v.owner is null
   and v.table_name is null;

v_exptab_check是要導出數據的表清單,我這里表是全部都要導入結構的,如果有表的表結構也不需要導入,需要再準備個表結構清單表。對于這種問題,圖省事可以在導入完成后再把這些約束加上。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,345評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,494評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,283評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,953評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,714評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,186評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,255評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,410評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,940評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,776評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,976評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,518評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,210評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,642評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,878評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,654評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,958評論 2 373

推薦閱讀更多精彩內容