匯出:
exp ssht/taxware@sshtfile=d:\ssht.dmp
exp 使用者名稱/密碼@服務名
匯入:
imp ssht/taxware@mysshtfile=d:\ssht.dmp fromuser=ssht touser=ssht
imp 使用者名稱/密碼@服務名
注意:在匯入時,首先要保證使用者有許可權,沒有的話,用sys進入后角色授權:dba,exp_full_databae,imp_full_database
以上是導全部資料,如果只想匯入其中一部分表:
匯出:exp ctais2/oracle@ctshntables=(qx_gnmk,qx_gnmk_tree,qx_gnmb,qx_gnmb_gnmk,qx_gw_gnmb,qx_user,qx_user_gw,qx_gw)file=d:\qx.dmp grants=y
匯入還同上:imp ssht/taxware@mysshtfile=d:\ssht.dmp fromuser=ssht touser=ssht
---------------------------------------------------------
【用 exp 數 據匯出】:
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 將資料庫中的表table1 、table2匯出
exp system/manager@TEST file=d:\daochu.dmptables=(table1,table2)
4 將資料庫中的表table1中的欄位filed1以”00″打頭的資料匯出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\” where filed1like'00%'\”
上面是常用的匯出,對於壓縮我不太在意,用winzip把dmp檔案可以很好的壓縮。不過在上面命令後面 加上compress=y 就可以了。
【用 imp 數 據匯入】:
1 將D:\daochu.dmp 中的資料匯入 TEST資料庫中。
imp system/manager@TEST file=d:\daochu.dmp
上面可能有點問題,因為有的表已經存在,然後它就報錯,對該表就不進行匯入。
在後面加上 ignore=y 就可以了。
2 將d:\daochu.dmp中的表table1 匯入
imp system/manager@TEST file=d:\daochu.dmp tables=(table1)
基本上上面的匯入匯出夠用了。不少情況我是將表徹底刪除,然後匯入。
功能:Oracle資料匯入匯出imp/exp就相當與oracle資料還原與備份。
大多情況都可以用Oracle資料匯入匯出完成資料的備份和還原(不會造成資料的丟失)。
Oracle有個好處,雖然你的電腦不是伺服器,但是你裝了oracle客戶端,並建立了連線
(通過net8 assistant中本地–>服務命名 新增正確的服務命名
其實你可以想成是客戶端與伺服器端修了條路,然後資料就可以被拉過來了)
這樣你可以把資料匯出到本地,雖然可能伺服器離你很遠。
你同樣可以把dmp檔案從本地匯入到遠處的資料庫伺服器中。
利用這個功能你可以構建倆個相同的資料庫,一個用來測試,一個用來正式使用。
執行環境:可以在SQLPLUS.EXE或者DOS(命令列)中執行,
DOS中可以執行時由於 在oracle 8i 中 安裝目錄\ora81\BIN被設定為全域性路徑,
該目錄下有EXP.EXE與IMP.EXE檔案被用來執行匯入匯出。
oracle用java編寫,我想SQLPLUS.EXE、EXP.EXE、IMP.EXE這倆個檔案是被包裝後的類檔案。
SQLPLUS.EXE呼叫EXP.EXE、IMP.EXE他們所包裹的類,完成匯入匯出功能。
注意:
你要有足夠的許可權,許可權不夠它會提示你。
資料庫時可以連上的。可以用tnsping TEST 來獲得資料庫TEST能否連上。
------------------------------------------------------------------------------------------------------------------------------
詳細參考如下:
一.匯出工具 exp
1. 它是作業系統下一個可執行的檔案存放目錄/ORACLE_HOME/bin
exp匯出工具將資料庫中資料備份壓縮成一個二進位制系統檔案.可以在不同OS間遷移
它有三種模式:
a. 使用者模式:匯出使用者所有物件以及物件中的資料;
b. 表模式: 匯出使用者所有表或者指定的表;
c. 整個資料庫: 匯出資料庫中所有物件。
2. 匯出工具exp互動式命令列方式的使用的例子
$exp test/test123@appdb
Enter array fetch buffer size: 4096 > 回車
Export file: expdat.dmp > m.dmp 生成匯出的檔名
(1)E(ntire database), (2)U(sers), or (3)T(ables): (2)U> 3
Export table data (yes/no): yes > 回車
Compress extents (yes/no): yes > 回車
Export done in ZHS16GBK character set and ZHS16GBK NCHAR characterset
About to export specified tables via Conventional Path ...
Table(T) or Partition(T:P) to be exported: (RETURN to quit)> cmamenu 要匯出的表名
. . exportingtable CMAMENU 4336 rows exported
Table(T) or Partition(T:P) to be exported: (RETURN to quit)>要匯出的表名n
Table(T) or Partition(T:P) to be exported: (RETURN to quit)> 回車
Export terminated successfully without warnings.
3. 匯出工具exp非互動式命令列方式的例子
$exp scott/tiger tables=(emp,dept)file=/directory/scott.dmp grants=y
說明:把scott使用者裡兩個表emp,dept匯出到檔案/directory/scott.dmp
$exp scott/tiger tables=emp query=\"wherejob=\'salesman\' and sal\<1600\"file=/directory/scott2.dmp
說明:在exp裡面加上匯出emp的查詢條件job='salesman' andsal<1600
(但我個人很少這樣用,還是把滿足條件的記錄生成臨時表後,再exp會方便一些)
$exp parfile=username.parfile=/directory1/username_1.dmp,/directory1/username_2.dmpfilesize=2000M log=/directory2/username_exp.log
引數檔案username.par內容
userid=username/userpassword
buffer=8192000
compress=n
grants=y
說明:username.par為匯出工具exp用的引數檔案,裡面具體引數可以根據需要去修改
filesize指定生成的二進位制備份檔案的最大位元組數
(可用來解決某些OS下2G物理檔案的限制及加快壓縮速度和方便刻歷史資料光碟等)
二.匯入工具 imp
1. 它是作業系統下一個可執行的檔案 存放目錄/ORACLE_HOME/bin
imp匯入工具將EXP形成的二進位制系統檔案匯入到資料庫中.
它有三種模式:
a. 使用者模式:匯出使用者所有物件以及物件中的資料;
b. 表模式: 匯出使用者所有表或者指定的表;
c. 整個資料庫: 匯出資料庫中所有物件。
只有擁有IMP_FULL_DATABASE和DBA許可權的使用者才能做整個資料庫匯入
imp步驟:
(1) create table (2) insertdata (3) create index (4)create triggers,constraints
2.匯入工具imp互動式命令列方式的例子
$ imp
Import: Release 8.1.6.0.0 - Production on 星期五 12月 7 17:01:082001
(c) Copyright 1999 OracleCorporation. All rightsreserved.
使用者名稱: test
口令:****
連線到: Oracle8i Enterprise Edition Release 8.1.6.0.0 - 64bitProduction
With the Partitioning option
JServer Release 8.1.6.0.0 - Production
匯入檔案: expdat.dmp> /tmp/m.dmp
輸入插入緩衝區大小(最小為 8192 ) 30720>
經由常規路徑匯出由EXPORT:V08.01.06建立的檔案
警告: 此物件由 TEST 匯出, 而不是當前使用者
已經完成ZHS16GBK字符集和ZHS16GBK NCHAR 字符集中的匯入
只列出匯入檔案的內容(yes/no):no>
由於物件已存在, 忽略建立錯誤(yes/no):no> yes
匯入許可權(yes/no):yes>
匯入表資料(yes/no):yes>
匯入整個匯出檔案(yes/no):no> yes
. 正在將TEST的物件匯入到 SCOTT
. .正在匯入表 "CMAMENU" 4336行被匯入
成功終止匯入,但出現警告。
3.匯入工具imp非互動式命令列方式的例子
$ imp system/manager fromuser=jones tables=(accts)
$ imp system/manager fromuser=scott tables=(emp,dept)
$ imp system/manager fromuser=scott touser=joe tables=emp
$ imp scott/tiger file = expdat.dmp full=y
$ imp scott/tiger file = /mnt1/t1.dmp show=n buffer=2048000ignore=n commit=y grants=y full=ylog=/oracle_backup/log/imp_scott.log
$ imp system/manager parfile=params.dat
params.dat 內容
file=dba.dmp show=n ignore=n grants=y fromuser=scotttables=(dept,emp)
4.匯入工具imp可能出現的問題
(1) 資料庫物件已經存在
一般情況, 匯入資料前應該徹底刪除目標資料下的表, 序列,函式/過程,觸發器等;
資料庫物件已經存在, 按預設的imp引數, 則會匯入失敗
如果用了引數ignore=y, 會把exp檔案內的資料內容匯入
如果表有唯一關鍵字的約束條件, 不合條件將不被匯入
如果表沒有唯一關鍵字的約束條件, 將引起記錄重複
(2) 資料庫物件有主外來鍵約束
不符合主外來鍵約束時, 資料會匯入失敗
解決辦法: 先匯入主表, 再匯入依存表
disable目標匯入物件的主外來鍵約束, 匯入資料後, 再enable它們
(3) 許可權不夠
如果要把A使用者的資料匯入B使用者下, A使用者需要有imp_full_database許可權
(4) 匯入大表( 大於80M ) 時,儲存分配失敗
預設的EXP時, compress = Y, 也就是把所有的資料壓縮在一個數據塊上.
匯入時, 如果不存在連續一個大資料塊, 則會匯入失敗.
匯出80M以上的大表時, 記得compress= N, 則不會引起這種錯誤.
(5) imp和exp使用的字符集不同
如果字符集不同, 匯入會失敗, 可以改變unix環境變數或者NT登錄檔裡NLS_LANG相關資訊.
匯入完成後再改回來.
(6) imp和exp版本不能往上相容
imp可以成功匯入低版本exp生成的檔案, 不能匯入高版本exp生成的檔案
根據情況我們可以用
$ imp username/password@connect_string
說明: connect_string 是在/ORACLE_HOME/network/admin/tnsnames.ora
定義的本地或者遠端資料庫的名稱
注意事項:
UNIX:/etc/hosts 要定義本地或者遠端資料庫伺服器的主機名
win98: windows\hosts 和IP地址的對應關係