Oracle 表的导入与导出
周末任务:
将一个表从一个库导到另一个库
大致思路:用expdp 将表从一个实例导出 ,再用impdp将导出的 .dmp文件导入到另一个实例
1.在实例 orcl 中准备一个用于导出的表:
进入实例为orcl的数据库: export ORACLE_SID=orcl
建用户:SQL> create user orcl_user identified by 123 default tablespace table_test;
赋予dba权限:SQL> grant dba to orcl_user;
建一个directory目录:
SQL> create directory dir as '/tmp/';
把这个目录的读写权限给予用户:
SQL> grant read,write on directory dir to orcl_user;
接下来可以登录orcl_user用户建表:
sqlplus orcl_user/123
SQL> create table orcl_table as select * from dba_users;
导出:
oracle@admin ~]$ expdp orcl_user/123 dumpfile=test.dmp logfile=test.log directory=dir tables=orcl_table
Export: Release 11.2.0.4.0 - Production on Wed Dec 18 07:55:51 2019
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, OLAP, Data Mining and Real Application Testing options
Starting "ORCL_USER"."SYS_EXPORT_TABLE_01": orcl_user/******** dumpfile=test.dmp logfile=test.log directory=dir tables=orcl_table
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
. . exported "ORCL_USER"."ORCL_TABLE" 14.46 KB 33 rows
Master table "ORCL_USER"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for ORCL_USER.SYS_EXPORT_TABLE_01 is:
/tmp/test.dmp
Job "ORCL_USER"."SYS_EXPORT_TABLE_01" successfully completed at Wed Dec 18 07:55:56 2019 elapsed 0 00:00:04
到此,导出表成功
2.在实例名为oorl的数据库中做导入的相关准备
建用户:
SQL> create user oorl_user identified by 123 default tablespace test;
赋权限(dba权限和对directory的读写权限)
SQL> grant dba to oorl_user;
SQL> grant read,write on directory dir to oorl_user;
下面开始做impdp导入:
1).在导入前因将要导入的表中有统计信息,会报ORA-39154的错,故在将imp_full_database的权限授予要导入表的用户:
SQL> grant imp_full_database to oorl_user;
2) impdp命令:
oracle@admin ~]$ impdp oorl_user/123 directory=dir dumpfile=test.dmp logfile=imp.log p.log remap_tablespace=table_test:test remap_schema=orcl_user:oorl_user
Import: Release 11.2.0.4.0 - Production on Wed Dec 18 08:10:40 2019
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, OLAP, Data Mining and Real Application Testing options
Master table "OORL_USER"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "OORL_USER"."SYS_IMPORT_FULL_01": oorl_user/******** directory=dir dumpfile=test.dmp logfile=imp.log remap_tablespace=table_test:test remap_schema=orcl_user:oorl_user
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "OORL_USER"."ORCL_TABLE" 14.46 KB 33 rows
Job "OORL_USER"."SYS_IMPORT_FULL_01" successfully completed at Wed Dec 18 08:10:42 2019 elapsed 0 00:00:01
到此导入成功
3.验证是否将orcl中的表导入了oorl中:
在orcl中
SQL> select count(*) from orcl_user.orcl_table;
COUNT(*)
----------
33
在oorl中:
SQL> select count(*) from oorl_user.orcl_table;
COUNT(*)
----------
33
验证成功
3.实验中出现的错误
当出现ORA—39154错时,要给用户赋予imp_full_database 的权限(有统计信息时)
grant imp_full_database to test;
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "TEST"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "TEST"."SYS_IMPORT_FULL_01": test/******** directory=dir dumpfile=t1.dmp logfile=test.log remap_tablespace=table_test:test remap_schema=user2:test
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "TEST"."T1" 8.457 MB 87170 rows
Job "TEST"."SYS_IMPORT_FULL_01" successfully completed at Wed Dec 18 07:05:56 2019 elapsed 0 00:00:01
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义