需要把一個Schema下的對象和資料轉移到另外一個Schema下,新Schema名:GAOL,舊Schema名:scott
1.首先建立需要存放導出檔案的目錄(我的oracle安裝在window,是以建立了一個新的檔案夾D:\dmp)
2.以sys身份登陸Oracle資料庫,執行建立目錄語句并授權
create directory 目錄名 as 建立的路徑
create directory scott_dmp as 'd:/dmp'
通過sql檢視目錄映射關系是否建立成功
select * from dba_directories
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNyZuBnLwAjN3QjNwYTMzATOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
執行授權語句,讓GAOL和SCOTT有對該目錄的read,write權限
grant read,write on directory scott_dmp to GAOL
grant read,write on directory scott_dmp to scott
檢視是否授權成功
select privilege, directory_name, DIRECTORY_PATH FROM user_tab_privs t, all_directories WHERE t.table_name(+) = d.directory_name ORDER BY 2, 1;
3.在指令行執行導出語句(window打開CMD)
expdp scott/[email protected] schemas=scott directory=scott_dmp dumpfile =expdp_test1.dmp logfile=expdp_test1.log
scott/[email protected]:連接配接oracle的使用者/密碼@連接配接串
schemas=scott:需要導出的schema
directory:oracle建立的目錄
dumpfile/logfile:導出的dmp檔案和日志檔案的檔案名
這裡需要注意一點是語句最後不需要加分号“;”,加了分号會預設為檔案名,即如果最後是…logfile=expdp_test1.log;,那我們生成的檔案名是有分号的
接下來執行語句
可以看到D:\dmp目錄下生成了對應的檔案,接着我們執行導入語句
IMPDP GAOL/[email protected] DIRECTORY=scott_dmp schemas=scott dumpfile=EXPDP_TEST1.DMP logfile=expdp_test1.log REMAP_SCHEMA=scott:GAOL
REMAP_SCHEMA:需要指定我們schema的映射關系,即scott對應加載到GAOL
看到已經導入成功,我們去資料庫檢視,可以看到是否正确
這是個小測試,expdp/impdp的導出導入模式還有很多,可以參考以下部落格
https://blog.csdn.net/zhaogang1993/article/details/84258838
https://blog.csdn.net/zhaogang1993/article/details/86466111
https://blog.csdn.net/zhongguomao/article/details/78933333