EXPDP和IMPDP的应用实例
一、调用EXPDP
使用EXPDP工具时,其转储文件只能被存放在DIRECTORY对象对应的OS目录中,而不能直接指定转储文件所在的OS目录。因此,使用EXPDP工具时,必须首先建立DIRECTORY对象。并且需要为数据库用户授予使用DIRECTORY对象权限。以sys用户登录并创建目录。
1、SQL>create directory dump_dir as 'd:\dump';
目录已创建。
2、执行完上述命令后,还要进入OS,在D盘上动手创建文件夹:DUMP
3、授予grantread,write on directory dump_dir权限给用户
3-1、给scott用户授予权限
SQL>grant read,write on directory dump_dir to scott;
授权成功。
3-2、给hr用户授予权限
SQL>grant read,write on directory dump_dir to hr;
授权成功。
alter userscott identified by rjxy;
alter userhr identified by rjxy;
5、调用EXPDP导出表
5-1、以scott用户登录数据库,导出scott用户的dept,emp 2个表,导出文件名为s_tab.dmp:
SQL>$expdp scott/rjxy directory=dump_dir dumpfile=s_tab.dmp tables=dept,emp;
5-2、以hr用户登录数据库,导出hr用户的regions,jobs2个表,导出文件名为h_tab.dmp:
SQL>$expdp hr/rjxy directory=dump_dir dumpfile=h_tab.dmp tables=regions,jobs;
5-3、以system用户登录数据库,导出scott用户的emp、dept、salgrade、bonus 4个表,导出文件名为tab1.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=tab1.dmptables=scott.emp,scott.dept,scott.salgrade,scott.bonus;
5-4、以system用户登录数据库,导出hr用户的employees、departments、jobs、countries4个表,导出文件名为tab2.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=tab2.dmp tables=hr.employees,hr.departments,hr.jobs,hr.countries;
6、调用EXPDP导出方案
6-1、以scott用户登录数据库,导出scott用户(方案),导出文件名为scott.dmp:
SQL>$expdp scott/tiger directory=dump_dir dumpfile=scott.dmp schemas=scott;
6-2、以hr用户登录数据库,导出hr用户(方案),导出文件名为hr.dmp:
SQL>$expdp hr/rjxy directory=dump_dir dumpfile=hr.dmp schemas=hr;
6-3、以system用户登录数据库,导出system用户(方案),导出文件名为system.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=system.dmp schemas=system;
7、调用EXPDP导出表空间
7-1、以system用户登录数据库,导出表空间system、sysaux,导出文件名为tablespace.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=tablespace.dmptablespaces=system,sysaux;
7-2、以system用户登录数据库,导出表空间example、users,导出文件名为tablespace1.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=tablespace1.dmptablespaces=example,users;
8、调用EXPDP导出数据库
8-1、以system用户登录数据库,导出数据库,导出文件名为db.dmp:
SQL>$expdp system/rjxy directory=dump_dir dumpfile=db.dmp full=y;
8-2、以sys用户登录数据库,导出数据库,导出文件名为db1.dmp:
SQL>$expdp directory=dump_dir dumpfile=db1.dmp full=y;
用户名:sys/rjxyas sysdba
二、调用IMPDP
1、调用IMPDP导入表
1-1、以scott用户登录数据库,导入scott用户的dept,emp 2个表,导入文件名为s_tab.dmp:
SQL>$impdp scott/tiger directory=dump_dir dumpfile=s_tab.dmp tables=dept,emptable_exists_action=replace;
1-2、以hr用户登录数据库,导入hr用户的regions,jobs2个表,导入文件名为h_tab.dmp:
SQL>SQL> $impdp hr/rjxy directory=dump_dir dumpfile=h_tab.dmptables=regions,jobs table_exists_action=replace;
1-3、以system用户登录数据库,将scott用户的emp,dept,salgrade,bonus4个表
导入到HR用户中,导入文件名为tab1.dmp:
SQL>$impdp system/rjxy directory=dump_dir dumpfile=tab1.dmptables=scott.emp,scott.dept,scott.salgrade,scott.bonus
remap_schema=scott:hrtable_exists_action=replace;
1-4、查看HR用户的所有表
SQL>select table_name from dba_tables where owner='HR';
TABLE_NAME
------------------------------
BONUS
SALGRADE
DEPT
EMP
REGIONS
JOBS
LOCATIONS
DEPARTMENTS
EMPLOYEES
JOB_HISTORY
COUNTRIES
已选择11行。
2、调用impdp导入方案
2-1、以scott用户登录数据库,导入scott用户(方案),导入文件名为scott.dmp:
SQL>$impdp scott/rjxy directory=dump_dir dumpfile=scott.dmp schemas=scotttable_exists_action=replace;
2-2、以hr用户登录数据库,导入hr用户(方案),导入文件名为hr.dmp:
SQL>$impdp hr/rjxy directory=dump_dir dumpfile=hr.dmp schemas=hrtable_exists_action=replace;
2-4、以system用户登录数据库,导入system用户(方案),导入文件名为system.dmp:
SQL>$impdp system/rjxy directory=dump_dir dumpfile=system.dmp schemas=systemtable_exists_action=replace;
3、调用impdp导入表空间
3-1、以system用户登录数据库,导入表空间system、sysaux,导入文件名为tablespace.dmp:
SQL>$impdp system/rjxy directory=dump_dir dumpfile=tablespace.dmptablespaces=system,sysaux table_exists_action=replace;
3-2、以system用户登录数据库,导入表空间example、users,导入文件名为tablespace1.dmp:
SQL>$impdp system/rjxy directory=dump_dir dumpfile=tablespace1.dmptablespaces=example,users table_exists_action=replace;
4、调用impdp导入数据库
4-1、以system用户登录数据库,导入数据库,导入文件名为db.dmp:
SQL>$impdp system/rjxy directory=dump_dir dumpfile=db.dmp full=y;
4-2、以sys用户登录数据库,导入数据库,导入文件名为db1.dmp:
SQL>$impdp directory=dump_dir dumpfile=db1.dmp full=y;
用户名:sys/rjxyas sysdba