1. 程式人生 > >使用empdp和impdp匯出和匯入資料庫的表

使用empdp和impdp匯出和匯入資料庫的表

資料泵技術比原來匯入/匯出(imp,exp)技術快15-45倍。速度的提高源於使用了並行技術來讀寫匯出轉儲檔案。此命令只可用在服務端,客戶端無法使用。

1.開啟SQL plus

首先需要輸入使用者名稱和密碼進行登入;

建立一個directory物件:create directory dpdata1 as 'd:\test\dump';

然後檢視當前例項下有哪些匯出目錄可使用:select * from dba_directories;

最後登入一個管理員的賬戶來給目前的賬戶賦予讀寫的許可權:grant read,write on directory dpdata1 to scott;

若想賦予全部的許可權即管理員許可權:grant dba to scott;

若重新建立一個新使用者:create user username identified by password;

給新使用者賦予登入的許可權:grant connect,resource to username;

2.啟用和禁用主鍵等

 

Type Code Type Description Acts On Level
C Check on a table Column
O Read Only on a view Object
P Primary Key Object
R Referential AKA Foreign Key Column
U Unique Key Column
V Check Option on a view Object
--刪除所有主鍵約束 的Sql程式碼 
select 'alter table '||table_name||' drop constraint '||constraint_name||';' from user_constraints where constraint_type='R'
--alter table EMP drop constraint EMP_PK ;
--禁用所有主鍵約束的Sql程式碼
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='R'
--alter table EMP disable constraint EMP_PK ;
--啟用所有主鍵約束的Sql程式碼
select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='R'   
--alter table EMP enable constraint EMP_PK ;

禁用掉主鍵等後會使匯入匯出的速度提高。

3.匯出資料

按表匯出(可單個或多個):

expdp scott/[email protected] directory=dpdata1 dumpfile=expdp.dmp tables=BONUS,emp,dept

結尾不要加分號;

按使用者匯出:expdp scott/[email protected] directory=dpdata1 dumpfile=expdp.dmp schemas=scott

匯出所有所有表:expdp scott/[email protected] directory=dpdata1 dumpfile=expdp.dmp full=y

4.匯入資料

impdp yyb/[email protected] directory=dpdata1 dumpfile=expdp.dmp remap_schema=scott:yyb exclude=user(使用者已存在,最後不要加分號)

impdp yyb/[email protected] directory=dpdata1 dumpfile=expdp.dmp remap_schema=scott:yyb//使用者不存在;

從scott使用者匯入到yyb使用者。

5.拓展:

查詢Oracle表是否有觸發器:

select * from user_triggers where table_owner = 'xxx' and table_name = upper('table_name');

匯出資料
1)按使用者導
expdp scott/[email protected] schemas=scott dumpfile=expdp.dmp DIRECTORY=dpdata1;
2)並行程序parallel
expdp scott/[email protected] directory=dpdata1 dumpfile=scott3.dmp parallel=40 job_name=scott3
3)按表名導
expdp scott/[email protected] TABLES=emp,dept dumpfile=expdp.dmp DIRECTORY=dpdata1;
4)按查詢條件導
expdp scott/[email protected] directory=dpdata1 dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
5)按表空間導
expdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=temp,example;
6)導整個資料庫

expdp system/manager DIRECTORY=dpdata1 DUMPFILE=full.dmp FULL=y;

還原資料
1)導到指定使用者下
impdp scott/tiger DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=scott;
2)改變表的owner
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
3)匯入表空間
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=example;
4)匯入資料庫
impdb system/manager DIRECTORY=dump_dir DUMPFILE=full.dmp FULL=y;
5)追加資料

impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION

6刪除資料

truncate table test;
truncate與delete的異同:
      TRUNCATE TABLE 在功能上與不帶 WHERE 子句的 DELETE 語句相同:二者均刪除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系統和事務日誌資源少。
  DELETE 語句每次刪除一行,並在事務日誌中為所刪除的每行記錄一項。TRUNCATE TABLE 通過釋放儲存表資料所用的資料頁來刪除資料,並且只在事務日誌中記錄頁的釋放。
  TRUNCATE TABLE 刪除表中的所有行,但表結構及其列、約束、索引等保持不變。新行標識所用的計數值重置為該列的種子。如果想保留標識計數值,請改用 DELETE。