在使用exp/imp进行数据迁移时,本希望把数据导入另一个表空间,但导入之后却发现数据表还是放在了原来的表空间中。
产生这种后果的原因很简单: 导入操作所使用的那个用户具有DBA权限(在数据导入时会导入到原表空间)!
下面的测试可以模拟上述场景:
1.创建用户及授权,指定默认表空间为 USERS :
TESTDB > create user eric identified by wyh
default tablespace USERS
temporary tablespace TEMP;
TESTDB > grant connect,resource to eric;
TESTDB > grant dba to eric;
2.创建测试用的三张表:
TESTDB > create table test1 as select * from dba_users;
Table created.
TESTDB > create table test2 as select * from dba_tables;
Table created.
TESTDB > create table test3 as select * from dba_tablespaces;
Table created.
TESTDB > select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TEST1 USERS
TEST2 USERS
TEST3 USERS
3.再创建另一个用户,指定默认表空间为 NEWTBS,也授予DBA权限:
TESTDB > create user wangyh identified by wyh
default tablespace NEWTBS
temporary tablespace TEMP
TESTDB > grant connect,resource to wangyh;
TESTDB > grant dba to wangyh;
4.导出eric用户的数据:
$ exp eric/wyh file=test.dmp
Export: Release 10.2.0.1.0 - Production on Tue Dec 18 11:08:17 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in WE8ISO8859P1 character set and UTF8 NCHAR character set
About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user ERIC
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user ERIC
About to export ERIC's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export ERIC's tables via Conventional Path ...
. . exporting table TEST1 17 rows exported
. . exporting table TEST2 1058 rows exported
. . exporting table TEST3 11 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.
5.导入至wangyh用户:
$ imp wangyh/wyh file=test.dmp fromuser=eric touser=wangyh grants=n
Import: Release 10.2.0.1.0 - Production on Tue Dec 18 11:10:09 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.02.01 via conventional path
Warning: the objects were exported by ERIC, not by you
import done in WE8ISO8859P1 character set and UTF8 NCHAR character set
. . importing table "TEST1" 17 rows imported
. . importing table "TEST2" 1058 rows imported
. . importing table "TEST3" 11 rows imported
Import terminated successfully without warnings.
6.在wangyh用户下,查看刚才导入的表所属的表空间:
$ sqlplus wangyh/wyh
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Dec 18 11:10:51 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
TESTDB > select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TEST1 USERS
TEST2 USERS
TEST3 USERS
可以看到,虽然我们指定了不同的默认表空间,但是导入的表依然放在原表空间"USERS"中。
解决方法也不复杂:
1.回收wangyh用户的表空间权限:
TESTDB > revoke unlimited tablespace from wangyh;
TESTDB > alter user wangyh quota 0 on users;
TESTDB > alter user wangyh quota unlimited on NEWTBS;
2.删除已经导入的对象,重新imp
imp wangyh/wyh file=test.dmp fromuser=eric touser=wangyh grants=n
Import: Release 10.2.0.1.0 - Production on Tue Dec 18 11:17:06 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.02.01 via conventional path
Warning: the objects were exported by ERIC, not by you
import done in WE8ISO8859P1 character set and UTF8 NCHAR character set
. . importing table "TEST1" 17 rows imported
. . importing table "TEST2" 1058 rows imported
. . importing table "TEST3" 11 rows imported
Import terminated successfully without warnings.
3.再次查看:
$ sqlplus wangyh/wyh
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Dec 18 11:17:58 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
TESTDB > select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
------------- ------------------------------
TEST1 NEWTBS
TEST2 NEWTBS
TEST3 NEWTBS
现在可以看到,导入的表,放在了当前用户的默认表空间NEWTBS中!