Oracle-impdp 数据泵导入数据 表空间和用户(schema)重映射 部份表数据替换
1. 步骤
1. 将数据泵导出(expdp)的.dmp转储文件存放到指定位置。
-
转储文件:
- 主文件:
E:\dmp\expdp_2501.dmp, - 进行替换的表数据:
E:\dmp\expdp_2502.dmp;
- 主文件:
-
存放位置:
E:\dmp;
2. 在oracle中创建路径对象映射到转储文件存放位置。
-
路径对象:
mydir; -
映射路径:
E:\dmp;
3. 授权目标用户对路径对象的读写权限。
-
目标用户:
user1; -
路径对象:
mydir; -
权限:
-
读:
read, -
写:
write;
-
4. impdp 数据泵导入
2. 导入
- sqlplus使用dba登入要导入数据的数据库
C:\Users\Administrator> sqlplus / as sysdba - 数据库中为转储文件存放位置(E:\dmp)创建路径(mydir)
SQL> create directory mydir as 'E:\dmp'; 目录已创建。
2. 从创建用户开始
-
创建用户user1
SQL> create user user1 identified by pwd1 default tablespace users quota unlimited on users; 用户已创建。创建的用户名不建议首位为数字,密码不建议带‘@’字符参数说明:
create user user1:创建用户‘USER1’;identified by pwd1:设置密码为pwd1;default tablespace users:使用‘USERS’作为默认表空间;quota unlimited on users:授予用户在表空间'USERS'无限配额. -
授权,授予创建会话、资源访问、读取和写入mydir目录路径的权限
SQL> grant create session,resource to user1; 授权成功。 SQL> grant read,write on directory mydir to user1; 授权成功。参数说明
grant:授权关键词to user1:授权给user1可选用的部分权限:
create session:创建会话权限resource:资源权限read:读权限write:写权限directory:目录insert,delete,update,select on schema_name.table_name:增,删,改,查特定表的权限(可从增删改查中选择需要的权限使用)dba:DBA权限(一般不要用) -
退出SQL
SQL> exit
3.数据泵导入数据
-
数据泵导入数据
C:\Users\Administrator> impdp user1/pwd1 directory=mydir dumpfile=expdp_2501.dmp logfile=expdp_2501.log remap_tablespace=old_spacename:now_spacename(默认为USERS) remap_schema=olduser:user1 Import: Release 19.0.0.0.0 - Production on 星期三 8月 19 11:10:03 2025 Version 19.3.0.0.0 Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved. 连接到: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production 已成功加载/卸载了主表 "USER1"."SYS_IMPORT_FULL_01" 已在 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中完成导入 已在 AL32UTF8 字符集和 AL16UTF16 NCHAR 字符集中完成导出 警告: 字符集转换过程中可能出现数据丢失情况 启动 "USER1"."SYS_IMPORT_FULL_01": USER1/******** directory=mydir dumpfile=2501.dmp logfile=2501.log remap_schema=olduser:user1 remap_tablespace=old_spacename:USERS 处理对象类型 TABLE_EXPORT/TABLE/TABLE 处理对象类型 TABLE_EXPORT/TABLE/TABLE_DATA . . 导入了 "USER1"."T_0" 6.589 GB 31082984 行 . . 导入了 "USER1"."T_1" 1017. MB 5093060 行 . . 导入了 "USER1"."T_2" 999.0 MB 2973015 行 . . 导入了 "USER1"."T_3" 268.1 MB 2758846 行 . . 导入了 "USER1"."T_4" 158.7 MB 812006 行 . . 导入了 "USER1"."T_5" 8.980 MB 32812 行 . . 导入了 "USER1"."T_6" 7.279 MB 74622 行 . . 导入了 "USER1"."T_7" 5.140 MB 35717 行 . . 导入了 "USER1"."T_8" 2.357 MB 19503 行 . . 导入了 "USER1"."T_9" 545.7 KB 5563 行 处理对象类型 TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS 作业 "USER1"."SYS_IMPORT_FULL_01" 已于 星期三 8月 19 11:12:11 2025 elapsed 0 00:02:08 成功完成remap_tablespace:表空间重映射
remap_schema:模式重映射(一般一个用户对应一个模式) -
部分表数据更新导出的转储文件(2502.dmp),替换原有该部分表数据(table_exists_action)
使用table_exists_action替换
C:\Users\Administrator> impdp user1/pwd1 directory=mydir dumpfile=expdp_2502.dmp logfile=expdp_2502.log remap_tablespace=old_spacename:now_spacename(默认为USERS) remap_schema=olduser:user1 table_exists_action=replace Import: Release 19.0.0.0.0 - Production on 星期三 8月 20 11:31:48 2025 Version 19.3.0.0.0 Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved. 连接到: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production 已成功加载/卸载了主表 "USER1"."SYS_IMPORT_FULL_01" 已在 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中完成导入 已在 AL32UTF8 字符集和 AL16UTF16 NCHAR 字符集中完成导出 警告: 字符集转换过程中可能出现数据丢失情况 启动 "USER1"."SYS_IMPORT_FULL_01": user1/******** directory=mydir dumpfile=2502.dmp logfile=2502.log remap_schema=IIH:user1 remap_tablespace=IIH_DATA:USERS table_exists_action=replace 处理对象类型 TABLE_EXPORT/TABLE/TABLE 处理对象类型 TABLE_EXPORT/TABLE/TABLE_DATA . . 导入了 "USER1"."T_0" 2.866 GB 12102270 行 作业 "USER1"."SYS_IMPORT_FULL_01" 已于 星期三 8月 20 11:32:37 2025 elapsed 0 00:00:49 成功完成table_exists_action选项及可选参数说明:
table_exists_action:如果表存在的动作
skip:默认,是如果已存在表,则跳过并处理下一个对象;
append:追加,是为表增加数据;
truncate:清空?是截断表,然后为其增加新数据,不改变表结构;
replace:是删除已存在表,重新建表并追加数据
3.附:熟悉一下目录创建、读写授权、读写授权撤销、删除目录和删除用户(包括无法删除的解决办法)的操作
```shell
SQL> create directory mydir_log as 'E:\dmp_log';
目录已创建。
SQL> grant read,write on directory mydir_log to user1;
授权成功。
SQL> revoke read,write on directory mydir_log from user1;
撤销成功。
SQL> drop directory mydir_log;
目录已删除。
SQL> drop user user1;
drop user user1
*
第 1 行出现错误:
ORA-01940: 无法删除当前连接的用户
SQL> select username,sid,serial#,paddr from v$session where username='user1';
USERNAME
--------------------------------------------------------------------------------
SID SERIAL# PADDR
---------- ---------- ----------------
user1
745 20199 00007FF75B42C388
SQL> select PROGRAM from v$process where addr='00007FF75B42C388';
PROGRAM
----------------------------------------------------------------
ORACLE.EXE (SHAD)
SQL> alter system kill session '745,20199';
系统已更改。
SQL> drop user user1;
drop user user1
*
第 1 行出现错误:
ORA-01922: 必须指定 CASCADE 以删除 'user1'
SQL> drop user user1 CASCADE;
用户已删除。
SQL>exit
```
本文来自博客园,作者:木氷,转载请注明原文链接:https://www.cnblogs.com/kihyou/p/19047172

浙公网安备 33010602011771号