一直以来,我都认为只要指定用户的默认表空间,向该用户导入数据时,会自动进入到默认表空间。后来发现从System导出的dmp文件在导入时,即使指定新用户的默认表空间,还是要往System表空间中导数据。
上网搜了一下,还是有解决方法的,常见的方法如下:
方法一
create user myhuang identified by myhuang default tablespace myhuang;
grant resource,connect to myhuang;
grant dba to myhuang;//赋DBA权限
revoke unlimited tablespace from myhuang;//撤销此权限
alter user myhuang quota 0 on system;//将用户在System表空间的配额置为0
alter user myhuang quota unlimited on myhuang;//设置在用户在myhuang表空间配额不受限。
经过上述设置后,就可以用imp导入数据,数据将会进入指定的myhuang表空间:
imp system/123456@vdb fromuser=lnxh tous
er=myhuang file=G:\myhuang\lnxh.dmp ignore=y grants=n
顺便说两个小问题:
(1)IMP-00003: 遇到 ORACLE 错误 1658
ORA-01658: 无法为表空间 MYHUANG 中的段创建 INITIAL 区
通常这个问题可以通过Resize增加表空间数据文件大小来解决。
(2)删除表空间
drop tablespace myhuang including contents and datafiles;
在10g中实验,drop表空间之后,仍然需要手动去删除数据文件。
方法二
该方法较好
Create tablespace {tbs_name} datafile ‘{file_path}’ size 500M autoextend on next 10M;
Create user {u_name} identified by {u_pwd} default tablespace {tbs_name} quota unlimited on {tbs_name};
Grant connect,imp_full_database to {u_name};
Imp {u_name}/{u_pwd}@{local_svrname} fromuser={from_user} touser={u_name} file={dmp_file_path} ignore=y tablespaces={tbs_name};
此方法不需要授予新用户DBA权限。
此方法的存在的问题是:可能导致包含BLOB、CLOB字段的表导入失败,这种情况下可以先用sql脚本将表结构建立起来,再导入相应的数据。
如
create user atf_ygj identified by mas123 default tablespace ygj_atf_data temporary tablespace ygj_atf_temp quota unlimited on ygj_atf_data ;
用户已创建。
grant connect,imp_full_database to atf_ygj;
授权成功。
imp atf_ygj/mas123@orcl_192.168.6.19 fromuser=atf_back touser=atf_ygj file=d:/atf_back20090927.dmp ignore=y tablespaces=ygj_atf_data;
方法三 改变用户数据所在的表空间
以后以该用户登录,创建的任何数据库对象都属于test_temp 和test_data表空间,这就不用在每创建一个对象给其指定表空间了。
============= 移动数据到表空间 =================
查询需要移动的表所在的表空间
select tt.table_name,tt.tablespace_name from user_all_tables tt where tt.tablespace_name like '%YGJ%'
移动表到指定表空间
alter table employees move tablespace ygj_data;
查询要移动的索引所在的表空间
select ii.index_name,ii.table_name,ii.tablespace_name,ii.temporary from user_indexes ii where index_name like '%EMP_PK%'
移动(重建)索引到指定表空间
alter index EMP_PK rebuild tablespace ygj_data;