任务:将数据库A导入到数据库B
准备:
A库相关信息查看
1、用户查看
select * from all_users order by username
此命令不受登录用户限制,可查看系统所有用户
2、查看表记录数,了解大致数据量
select table_name,num_rows from user_tables
若num_rows为空,需手动执行分析命令(analyze table my_table compute statistics;),因为user_tables不是实时更新的
3、表空间查看
当前登录用户各表的表空间
select table_name,tablespace_name from user_tables;
系统当前所有表空间使用情况【需管理员权限】,包含物理文件位置,是否自增长,字节数大小
select tablespace_name,file_name,autoextensible,bytes/1048576 mb from dba_data_files order by relative_fno
4、查看表是否有外键约束
SELECT a.table_name,a.column_name,a.constraint_name,b.constraint_type,b.status
FROM user_cons_columns a,user_constraints b
WHERE a.constraint_name=b.constraint_name and b.constraint_type='R'
ORDER BY a.table_name
5、表及表字段comment注释查看
select a.table_name,a.comments,b.column_name,b.comments col_comment from user_tab_comments a
inner join user_col_comments b on a.table_name=b.table_name and (a.comments is not null or b.comments is not null)
and a.table_name not like '%$0'
order by a.table_name
B库导入前准备:
1、创建用户并授权
create user testuser identified by testpassword;
grant connect,resource to testuser;
grant create view to testuser;
grant unlimited tablespace to testuser;
2、创建表空间并使其自增长
create tablespace mytablespace datafile 'D:\ORADATA\ORCL\mytablespace.DBF' size 8m extent management local autoallocate;
alter database datafile 'D:\ORADATA\ORCL\mytablespace.DBF' autoextend on;
若发现创建的表空间有问题,可删除再创建。drop时仅是逻辑删除,需手动删除对应磁盘文件,否则重新创建时需加REUSE
DROP TABLESPACE mytablespace INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;
create tablespace mytablespace datafile 'D:\ORADATA\ORCL\mytablespace.DBF' size 8m REUSE extent management local autoallocate;
alter database datafile 'D:\ORADATA\ORCL\mytablespace.DBF' autoextend on;
A库导出
A库按某用户导出,导出该用户所有的表,视图,索引等对象
exp testuser/[email protected]/orcl BUFFER=64000 file=E:\bak\testuser.dmp log=E:\bak\testuser.log OWNER=testuser
用户需安装有oracle客户端工具,在CMD命令行中执行上述命令
B库导入
A库是按某用户导出的,且导出的是dmp文件,所以B库需对应建好用户和表空间
imp testuser/[email protected]/orcl feedback=10000 BUFFER=65536 file=E:\bak\testuser.dmp log=E:\bak\implog\testuser.log fromuser=testuser touser=testuser
同样的,上述命令也是在CMD命令行中执行