oracle 的匯入與匯出

2021-09-24 10:46:25 字數 4677 閱讀 8013

在專案實施過程中,總會少不了與資料庫打交道,這是個人總結:

刪除表空間

drop tablespace pt6 including contents;

drop tablespace mpm including contents;

drop tablespace sf including contents;

drop tablespace bt including contents;

刪除使用者

drop user pt6 cascade;

drop user mpm cascade;

drop user sf cascade;

drop user bt cascade;

建立表空間

當首次匯入時建立表空間 (執行指令碼時會提示輸入 &1 輸入資料檔案希望存放的實體地址 例如 d:\oracledata)

create tablespace pt6 datafile '&1\pt6.dbf' size 100m autoextend on next 10m;

create tablespace mpm datafile '&1\mpm.dbf' size 100m autoextend on next 10m;

建立使用者

create user pt6 identified by cape default

tablespace pt6;

create user mpm identified by cape

default tablespace mpm;

授權

grant dba to pt6;

grant dba to mpm;

grant all on dual to pt6 with grant option;

grant all on dual to mpm with grant option;

資料庫的備份:

方法一:

資料幫浦方式

匯出:expdp pt6/cape@localhost:1521/bt dumpfile=orcl_2014-08-18.expdp logfile=orcl_2014-08-18.log schemas=(pt6,mpm,sf,bt)

// 匯入:impdp pt6/cape@localhost:1521/bt dumpfile=newbt_2015-03-16.expdp logfile =newbt_2015-03-16.log schemas=(pt6,mpm,sf,bt)

方法二:

匯出:exp pt6/cape@localhost:1521/test file=c:\test_2014-04-02.dmp log=c:\test_2014-04-02.log owner=(pt6,mpm)

.log

.log

說明:如果高版本的資料庫導向低版本的資料庫腳步,那麼在匯出的時候加上低版本號 version=11.1.0.7.0 版本號即可。匯入不需要加入。10.1.0.2.0

例如:expdp pt6/cape@localhost:1521/test dumpfile=test_2014-04-02.expdp logfile=exp_test_2014-04-02.log schemas=(pt6,mpm) version=11.1.0.7.0

expdp pt6/cape@localhost:1521/test dumpfile=test_2014-04-02.expdp logfile=exp_test_2014-04-02.log schemas=(pt6,mpm) version=10.1.0.2.0

impdp pt6/cape@localhost:1521/test dumpfile=test_2014-04-02.expdp logfile=imp_test_2014-04-02.log schemas=(pt6,mpm)

同意詞建立:

接著再說表的恢復與表資料恢復(這是別人總結的東西,感覺很好用,先複製過來,學習了,哈哈)

對誤刪的表,只要沒有使用 purge 永久刪除選項,那麼基本上是能從 flashback table 區恢復回來的。

資料表和其中的資料都是可以恢復回來的,記得 flashback table 是從 oralce 10g 提供的,一般步驟有:

a.從 flashback table 裡查詢被刪除的資料表

select

*from recyclebin order

by droptime desc

b.執行表的恢復

flashback table

'需要恢復的表名

'to before drop

刪除表中資料有三種方法:

·delete(刪除一條記錄)

·drop或truncate刪除**中資料

1.delete誤刪除的解決方法

原理:利用oracle提供的閃回方法,如果在刪除資料後還沒做大量的操作(只要保證被刪除資料的塊沒被覆寫),就可以利用閃回方式直接找回刪除的資料

具體步驟為:

*確定刪除資料的時間(在刪除資料之前的時間就行,不過最好是刪除資料的時間點)

*用以下語句找出刪除的資料:select * from 表名 as of timestamp to_timestamp('刪除時間點','yyyy-mm-dd hh24:mi:ss')

*把刪除的資料重新插入原表:

insert into 表名 (select * from 表名 as of timestamp to_timestamp('刪除時間點','yyyy-mm-dd hh24:mi:ss'));注意要保證主鍵不重複。

如果表結構沒有發生改變,還可以直接使用閃回整個表的方式來恢復資料。

具體步驟為:

表閃回要求使用者必須要有flash any table許可權

·alter table 表名 enable row movement

·flashback table 表名 to timestamp to_timestamp(刪除時間點','yyyy-mm-dd hh24:mi:ss')

2.drop誤刪除的解決方法

原理:由於oracle在刪除表時,沒有直接清空表所佔的塊,oracle把這些已刪除的表的資訊放到了乙個虛擬容器「**站」中,而只是對該錶的資料塊做了可以被覆寫的標誌,所以在塊未被重新使用前還可以恢復。

具體步驟:

*查詢這個「**站」或者查詢user_table檢視來查詢已被刪除的表:

· select table_name,dropped from user_tables

· select object_name,original_name,type,droptime from user_recyclebin

在以上資訊中,表名都是被重新命名過的,欄位table_name或者object_name就是刪除後在**站中的存放表名

*如果還能記住表名,則可以用下面語句直接恢復:

flashback table 原表名 to before drop

如果記不住了,也可以直接使用**站的表名進行恢復,然後再重新命名,參照以下語句:

flashback table "**站中的表名(如:bin$dsbdfd4rdfdfdfegdfsf==$0)"to before drop rename to 新錶名

oracle的閃回功能除了以上基本功能外,還可以閃回整個資料庫:

使用資料庫閃回功能,可以使資料庫回到過去某一狀態, 語法如下:

sql>alter database flashback on

sql>flashback database to scn scnno;

sql>flashback database to timestamp to_timestamp('2007-2-12 12:00:00','yyyy-mm-dd hh24:mi:ss');

總結:oracle提供以上機制保證了安全操作,但同時也代來了另外乙個問題,就是空間占用,由於以上機制的執行,使用drop乙個表或者delete資料後,空間不會自動**,對於一些確定不使用的表,刪除時要同時**空間,可以有以下2種方式:

1、採用truncate方式進行截斷。(但不能進行資料回恢復了)

2、在drop時加上purge選項:drop table 表名 purge

該選項還有以下用途:

也可以通過刪除recyclebin區域來永久性刪除表 ,原始刪除表drop table emp cascade constraints

purge table emp;

刪除當前使用者的**站:

purge recyclebin;

刪除全體使用者在**站的資料:

purge dba_recyclebin

posted @

2016-08-29 16:04

赤子之心_timefast 閱讀(

...)

編輯收藏

oracle匯出與匯入指令碼

匯出指定使用者下的所有表 環境變數設定 export oracle sid bdyz export oracle base u01 oracle11g r2 export oracle home oracle base 11g export ld library path oracle home l...

Oracle方案的匯入與匯出

剛學習了oracle,對資料庫的匯入匯出。有點記不住。1 連線oracle資料庫 sql conn as sysdba 已連線。2 建立乙個操作目錄 sql create directory dump dir as e dump 注意同時需要使用作業系統命令在硬碟上建立這個物理目錄。目錄已建立。3 ...

Oracle資料的匯出與匯入

前序 關於oracle9i資料的匯出與匯入問題,折騰我好長時間了,尤其是匯入。今天在一位同事 zhangshukun 的指導下,算是終於成功了。正文 oracle資料匯出 如果是匯出本機的oracle資料庫 exp pcisv62 11 orcl file d pcisv62081226.dmp f...