1. 程式人生 > >Oracle表的建立.表空間建立刪除,匯入匯出等

Oracle表的建立.表空間建立刪除,匯入匯出等

建立表空間;
先在E盤建立oradate再在裡面建一個temp,
再開啟命令列cmd在寫入sqlplus再寫入sys/[email protected] as sysdba再寫入
create tablespace FUNDS5DAT logging datafile 'E:\oradate\temp\FUNDS5DAT.dbf' size 500m autoextend on next 100m maxsize 30720m extent management local;


刪除非空表空間
DROP TABLESPACE FUNDS5DAT INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS


新增表空間容量
alter tablespace XPADS50_DATA add datafile 'E:\oradate\XPADS50\XPADS50_DATA_OTHER.dbf' SIZE 500m autoextend on next 100m maxsize 30720m;


建立臨時表空間
create temporary tablespace XPADS50_TEMP_DATA tempfile 'E:\oradate\XPADS50\XPADS50_TEMP_DATA.dbf' size 100M autoextend on next 50M maxsize 30720M extent management local; 


建立使用者指定表空間
create user xpads50 identified by xpads50 
default tablespace XPADS50_DATA temporary tablespace XPADS50_TEMP_DATA; 


//刪除使用者以及使用者所有的物件 
drop user zfmi cascade; 
//cascade引數是級聯刪除該使用者所有物件,經常遇到如使用者有物件而未加此引數則使用者刪不了的問題,所以習慣性的加此引數 


查詢使用者下所有以BLOB,CLOB格式儲存的表名
select owner,table_name,column_name,data_type from all_tab_columns where data_type in ('BLOB','CLOB') and owner = 'FUNDS50'; 


//刪除表空間 
刪除表空間temp,但不刪除其檔案   
用:drop tablespace temp; 
刪除表空間temp同時刪除表空間的資料物件 
用drop tablespace temp including contents;
刪除表空間temp及其包含資料物件以及資料檔案 
用drop tablespace temp including contents and datafiles;
//including contents 刪除表空間中的內容,如果刪除表空間之前表空間中有內容,而未加此引數,表空間刪不掉,所以習慣性的加此引數 
//including datafiles 刪除表空間中的資料檔案 
//cascade constraints 同時刪除tablespace中表的外來鍵參照 
如果刪除表空間之前刪除了表空間檔案,解決辦法: 


如果在清除表空間之前,先刪除了表空間對應的資料檔案,會造成資料庫無法正常啟動和關閉。 
可使用如下方法恢復(此方法已經在oracle9i中驗證通過): 
下面的過程中,filename是已經被刪除的資料檔案,如果有多個,則需要多次執行;tablespace_name是相應的表空間的名稱。 
$ sqlplus /nolog 
SQL> conn / as sysdba; 
如果資料庫已經啟動,則需要先執行下面這行: 
SQL> shutdown abort 
SQL> startup mount 
SQL> alter database datafile 'filename' offline drop; 
SQL> alter database open; 
SQL> drop tablespace tablespace_name including contents; 


//授予message使用者DBA角色的所有許可權 
GRANT DBA TO zfmi; 


//給使用者授予許可權 
grant connect,resource to zfmi; (db2:指定所有許可權) 


匯入匯出命令: 


Oracle資料匯入匯出imp/exp就相當於oracle資料還原與備份。exp命令可以把資料從遠端資料庫伺服器匯出到本地的dmp檔案, imp命令可以把dmp檔案從本地匯入到遠處的資料庫伺服器中。 利用這個功能可以構建兩個相同的資料庫,一個用來測試,一個用來正式使用。 


執行環境:可以在SQLPLUS.EXE或者DOS(命令列)中執行, 
DOS中可以執行時由於 在oracle 8i 中 安裝目錄ora81BIN被設定為全域性路徑, 
該目錄下有EXP.EXE與IMP.EXE檔案被用來執行匯入匯出。 
oracle用java編寫,SQLPLUS.EXE、EXP.EXE、IMP.EXE這兩個檔案有可能是被包裝後的類檔案。 
SQLPLUS.EXE呼叫EXP.EXE、IMP.EXE所包裹的類,完成匯入匯出功能。 


下面介紹的是匯入匯出的例項。 
資料匯出: 
1 將資料庫TEST完全匯出,使用者名稱system 密碼manager 匯出到D:daochu.dmp中 
   exp system/
[email protected]
file=d:daochu.dmp full=y 
2 將資料庫中system使用者與sys使用者的表匯出 
   exp system/[email protected] file=d:daochu.dmp owner=(system,sys) 
3 將資料庫中的表inner_notify、notify_staff_relat匯出 
    exp aichannel/[email protected] file= d:datanewsmgnt.dmp tables=(inner_notify,notify_staff_relat) 


4 將資料庫中的表table1中的欄位filed1以"00"打頭的資料匯出 
   exp system/
[email protected]
file=d:daochu.dmp tables=(table1) query=" where filed1 like '00%'" 


上面是常用的匯出,對於壓縮,既用winzip把dmp檔案可以很好的壓縮。 
也可以在上面命令後面 加上 compress=y 來實現。 


資料的匯入 
1 將D:daochu.dmp 中的資料匯入 TEST資料庫中。 
   imp system/[email protected] file=d:daochu.dmp 
   imp aichannel/
[email protected]
full=y file=d:datanewsmgnt.dmp ignore=y 
   上面可能有點問題,因為有的表已經存在,然後它就報錯,對該表就不進行匯入。 
   在後面加上 ignore=y 就可以了。 
2 將d:daochu.dmp中的表table1 匯入 
imp system/[email protected] file=d:daochu.dmp tables=(table1) 


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


注意: 
操作者要有足夠的許可權,許可權不夠它會提示。 
資料庫時可以連上的。可以用tnsping TEST 來獲得資料庫TEST能否連上。 


附錄一: 
給使用者增加匯入資料許可權的操作 
第一,啟動sql*puls 
第二,以system/manager登陸 
第三,create user 使用者名稱 IDENTIFIED BY 密碼 (如果已經建立過使用者,這步可以省略) 
第四,GRANT CREATE USER,DROP USER,ALTER USER ,CREATE ANY VIEW , 
   DROP ANY VIEW,EXP_FULL_DATABASE,IMP_FULL_DATABASE, 
      DBA,CONNECT,RESOURCE,CREATE SESSION TO 使用者名稱字 
第五, 執行-cmd-進入dmp檔案所在的目錄, 
      imp userid=system/manager full=y file=*.dmp 
      或者 imp userid=system/manager full=y file=filename.dmp 


執行示例: 
F:WorkOracle_Databackup>imp userid=test/test full=y file=inner_notify.dmp 


螢幕顯示 
Import: Release 8.1.7.0.0 - Production on 星期四 2月 16 16:50:05 2006 
(c) Copyright 2000 Oracle Corporation. All rights reserved. 


連線到: Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production 
With the Partitioning option 
JServer Release 8.1.7.0.0 - Production 


經由常規路徑匯出由EXPORT:V08.01.07建立的檔案 
已經完成ZHS16GBK字符集和ZHS16GBK NCHAR 字符集中的匯入 
匯出伺服器使用UTF8 NCHAR 字符集 (可能的ncharset轉換) 
. 正在將AICHANNEL的物件匯入到 AICHANNEL 
. . 正在匯入表                  "INNER_NOTIFY"          4行被匯入 
準備啟用約束條件... 
成功終止匯入,但出現警告。 




附錄二: 
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 


imp funds40/funds40 file=