Oracle匯入 匯出資料

2021-06-27 17:47:18 字數 4673 閱讀 5624

注:摘自網際網路

oracle 提供兩個工具imp.exe 和exp.exe分別用於匯入和匯出資料。這兩個工具位於oracle_home/bin目錄下。 

匯出資料exp

1 將資料庫atstestdb完全匯出,使用者名稱system 密碼123456 匯出到c:/export.dmp中 

exp system/123456@atstestdb file=c:/export.dmp full=y 

其中atstestdb為資料庫名稱,system為該資料庫裡的賬戶,123456為其密碼。 

2 將資料庫中system使用者與sys使用者的所有相關資源匯出(表,儲存過程,方法,檢視等等) 

exp system/123456@atstestdb file= c:/export.dmp owner=(system,sys) 

3 將資料庫中的表sys.table1、owbsys.table2匯出 

exp system/123456@atstestdb file= c:/export.dmp tables=( sys.table1, owbsys.table2) 

注意,需要加上表的schema名稱,如果沒有加的話預設是導當前connected使用者的表,當然你連線上去的賬戶要對相應的表有許可權。 

4 將資料庫中的表table1中的字段title以"gangge"打頭的資料匯出 

exp system/123456@atstestdb file= c:/export.dmp tables=(table1) query=/" where title like 'gangge%'/" 

斜槓 「/」 後面跟冒號是為了轉義字元冒號」 「 「用的,因為後面是一條條件查詢語句。query引數只能指定乙個,如果query要為多張表,所以同樣,tables裡面也只能有一張表,或者多張表,然後query裡面的條件在這些表上面都可以執行。否則只好多寫幾條exp語句了。 

匯出後,或許發現資料比較大,我們可以用一些壓縮工具對資料進行二次壓縮,例如用winzip, winrar, 7zip等第三方工具。同樣,exp支援乙個引數使用者直接對資料進行壓縮:compress = y, 這個引數直接加到命令的後面即可實現匯出的同時壓縮資料。

1 將資料庫test完全匯出,使用者名稱system 密碼manager 匯出到d:/daochu.dmp中 

exp system/manager@test file=d:/daochu.dmp full=y 

2 將資料庫中system使用者與sys使用者的表匯出 

exp system/manager@test file=d:/daochu.dmp owner=(system,sys) 

3 將資料庫中的表inner_notify、notify_staff_relat匯出 

exp aichannel/aichannel@testdb2 file= d:/datanewsmgnt.dmp tables=(inner_notify,notify_staff_relat)

4 將資料庫中的表table1中的字段filed1以"00"打頭的資料匯出 

exp system/manager@test file=d:/daochu.dmp tables=(table1) query=" where filed1 like '00%'"

上面是常用的匯出,對於壓縮,既用winzip把dmp檔案可以很好的壓縮。 

也可以在上面命令後面 加上 compress=y 來實現。 

匯入資料imp

我們知道怎麼提取資料,那麼還原的時候,就需要使用imp命令把匯出的資料載入進去。 

1 向atstestdb裡面載入c:/export.dmp資料 

imp system/123456@atstestdb file=c:/export.dmp 

好了,導資料得時候,有可能報錯了。為什麼?有兩種主要的原因: 

a. 匯入的物件(表,檢視,方法等)原本不屬於當前連線的使用者的 

b. 匯入的物件在該資料庫的指定使用者下已經存在 

c. 匯入的物件的原本使用者不再這個資料庫裡 

所有物件全部匯入到指定的賬戶下: 

imp system/123456@atstestdb file=c:/export.dmp fromuser=sys touser=system 

其中fromuser=sys為.dmp檔案裡的物件的原先的owner, touser=system 為作為匯入的物件的新的owner. 

忽略/插入資料 

imp system/123456@atstestdb file=c:/export.dmp ignore=y 

其中ignore=y告訴imp.exe把資料直接插入到相應物件(並且如果匯入的物件裡面有其他的物件,如約束,索引等,會在資料插入後被建立)。 

2 載入其中的指定表table1,table2 

imp system/123456@atstestdb file=c:/export.dmp tables=(table1,table2)

3 忽略載入約束 

有時候導資料進來的時候,我們不需要把它的約束,比如一些外來鍵約束等都導進來,可以加上引數constraints=n 

imp system/123456@atstestdb file=c:/export.dmp tables=(table1,table2) constraints=n 

4 不載入索引(比如唯一性的索引) 

imp system/123456@atstestdb file=c:/export.dmp tables=(table1,table2) indexs=n 

5 只載入結構,不載入資料 

如果只要表的結構等定義(約束,觸發器),那麼不要裡面的資料,可以加上引數rows=n 

imp system/123456@atstestdb file=c:/export.dmp tables=(table1,table2) rows=n 

對於上述操作登陸操作的物件system是管理員,如果不是管理員,而是普通使用者,那麼這個使用者必須有建立刪除物件的權利,物件可能包括 表,檢視,方法,儲存過程等等常見的物件。為什麼「可能」包括?這個要看匯入匯出的時候是否涉及相關型別的物件而定。

1 將d:/daochu.dmp 中的資料匯入 test資料庫中。 

imp system/manager@test  file=d:/daochu.dmp 

imp aichannel/aichannel@test  full=y  file=d:/datanewsmgnt.dmp ignore=y 

上面可能有點問題,因為有的表已經存在,然後它就報錯,對該錶就不進行匯入。 

在後面加上 ignore=y 就可以了。 

2 將d:daochu.dmp中的表table1 匯入 

imp system/manager@test  file=d:/daochu.dmp  tables=(table1) 

基本上上面的匯入匯出夠用了。不少情況要先是將表徹底刪除,然後匯入。 

注意: 

操作者要有足夠的許可權,許可權不夠它會提示。 

資料庫時可以連上的。可以用tnsping test 來獲得資料庫test能否連上。 

oracle 不允許直接改變表的擁有者, 利用export/import可以達到這一目的. 

先建立import9.par, 

然後,使用時命令如下:imp parfile=/filepath/import9.par 

例 import9.par 內容如下: 

fromuser=tgpms       

touser=tgpms2     (注:把錶的擁有者由fromuser改為touser,fromuser和touser的使用者可以不同)          

rows=y 

indexes=y 

grants=y 

constraints=y 

buffer=409600 

file==/backup/ctgpc_20030623.dmp 

log==/backup/import_20030623.log

在匯入匯出命令中加上feedback=1000可以讓過程顯示乙個不斷增多的「...」,以改變以往的閃爍的游標

new:

exp/imp已經很好用了,但是唯一的確定是速度太慢,如果1張表的資料有個百千萬的,常常匯入匯出就長時間停在這個表這,但是從oracle 10g開始提供了稱為資料幫浦新的工具expdp/impdp,它為oracle資料提供高速並行及大資料的遷移。

imp/exp可以在客戶端呼叫,但是expdp/impdp只能在服務端,因為在使用expdp/impdp以前需要在資料庫中建立乙個 directory

create directory dump_test as '/u01/oracle10g';

grant read, write on directory dump_test to piner

然後就可以開始匯入匯出

expdp piner/piner directory=dump_test dumpfile=user.dmp  匯出使用者的資料

expdp piner/piner directory=dump_test dumpfile=table.dmp tables=test1,test2 匯出表資料

impdp piner/piner directory=dump_test dumpfile=user.dmp 匯入該使用者資料

impdp piner/piner directory=dump_test dumpfile=table.dmp  匯出表資料

oracle資料匯入匯出

語法 imp userid password 資料庫全域性名 file dmp檔案的目錄 其它引數 獲取幫助,敲入 imp help y import常用的引數 1 file 指定匯入檔名 fromuser 允許匯入指定的使用者擁有的表 full full y時,匯入dmp檔案中所有的事物 igno...

Oracle資料匯入匯出

資料庫的維護過程,難免遇到一些表的備份和恢復工作。為了方便起見,我將這些重複的工作整理成了執行在windows上的批處理,和執行在aix伺服器上的kshell指令碼。指令碼的作業內容就是,指定具體的表集合,分檔案逐表備份和恢復。如果是經常性的指定的固定表的話,可以修改以陣列方式的指令碼。如果是經常改...

oracle 資料匯出匯入

資料庫的匯出匯入 匯出表步驟 1 匯出表結構與資料 2 匯出序列 3 匯出觸發器 4 匯出檢視 匯出表工具 plsql developer 設定環境變數 變數名 nls lang 變數值 american america.zhs16gbk 這裡主要是解決資料庫的字符集問題,根據資料庫的字符集設定,保...