exp/imp 多用户导入导出

时间:2022-01-31 23:40:02

创建用户

创建三个用户test1,test2,test3及表table1,table2,table3

SQL> create user test1 identified by test1 default tablespace users;

User created.

SQL> grant connect,resource to test1;

Grant succeeded.

SQL> create table test1.table1 as select * from dba_objects;

Table created.

SQL> select count(1) from test1.table1;

  COUNT(1)
----------
87459
create user test2 identified by test2 default tablespace users;
grant connect,resource to test2;
create table test2.table2 as select * from dba_objects; create user test3 identified by test3 default tablespace users;
grant connect,resource to test3;
create table test3.table3 as select * from dba_objects;

导出数据

$ exp system/oracle file=test.dmp owner=test1,test2,test3

...

About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user TEST1
. exporting foreign function library names for user TEST2
. exporting foreign function library names for user TEST3

...

Export terminated successfully without warnings.

清理数据

drop table test1.table1 purge;
drop table test2.table2 purge;
drop table test3.table3 purge;

导入数据

1.乱序导入

$ imp system/oracle fromuser=test1,test2,test3 touser=test3,test1,test2 file=test.dmp
...
. importing TEST1's objects into TEST3
. importing TEST2's objects into TEST1
. importing TEST3's objects into TEST2
...
Import terminated successfully without warnings.
$ sqlplus / as sysdba
SQL> select count(1) from test1.table2; COUNT(1)
----------
87461 SQL> select count(1) from test2.table3; COUNT(1)
----------
87461 SQL> select count(1) from test3.table1; COUNT(1)
----------
87461

总结:导入的时候不会主动识别用户名进行匹配,而是根据fromuser和touser的排列按顺序匹配进行导入。

2.fromuser<touser

清理数据:
drop table test1.table2 purge;
drop table test2.table3 purge;
drop table test3.table1 purge;
导入:
$ imp system/oracle fromuser=test1,test2 touser=test1,test2,test3 file=test.dmp
...
. importing TEST1's objects into TEST1
. importing TEST2's objects into TEST2
...
Import terminated successfully without warnings.
验证:
SQL> select owner,table_name from dba_tables where owner in ('TEST1','TEST2','TEST3'); OWNER TABLE_NAME
---------- ------------------------------
TEST2 TABLE2
TEST1 TABLE1

总结:当fromuser<touser时,仍然按顺序匹配将fromuser下的对象导入到touser。多出的touser不作处理,不导入任何数据。

3.fromuser>touser

清理数据:
drop table test1.table1 purge;
drop table test2.table2 purge; 导入:
$ imp system/oracle fromuser=test1,test2,test3 touser=test1,test2 file=test.dmp
...
. importing TEST1's objects into TEST1
. importing TEST2's objects into TEST2
. importing TEST3's objects into TEST3
...
Import terminated successfully without warnings. 验证:
SQL> select owner,table_name from dba_tables where owner in ('TEST1','TEST2','TEST3'); OWNER TABLE_NAME
---------- ------------------------------
TEST1 TABLE1
TEST2 TABLE2
TEST3 TABLE3

总结:fromuser>touser时,多出的用户会在数据库中找到对应相同账户并导入数据。(相同账户在目标数据库中存在的情况下)

当账户在目标库中不存在时,会报错,并忽略掉对该用户的导入:

SQL> drop user test3 cascade;

User dropped.

$ imp system/oracle fromuser=test1,test2,test3  touser=test1,test2 file=test.dmp

...
. importing TEST1's objects into TEST1
. importing TEST2's objects into TEST2
. importing TEST3's objects into TEST3
IMP-00003: ORACLE error 1435 encountered
ORA-01435: user does not exist
....
Import terminated successfully with warnings.

SQL> select owner,table_name from dba_tables where owner in ('TEST1','TEST2','TEST3');


OWNER TABLE_NAME
---------- ------------------------------
TEST1 TABLE1
TEST2 TABLE2