ORACLE - 管理表空間和數據文件

ORACLE表空間是一個邏輯分區,一個數據文件只能屬於一個表空間,一個表空間能夠擁有多個數據文件。數據庫

通常狀況下,若是一個實例分配給多個應用使用,須要建立不一樣的表空間,每一個用戶使用本身的表空間。oracle

1、表空間的建立與受權ide

首先查看錶空間的使用狀況:spa

select tablespace_name,sum(bytes)/1024/1024 as MB from dba_data_files group by tablespace_name;

---剩餘容量(在固定大小的狀況下)
select tablespace_name, count(*) as extends, round(sum(bytes) / 1024 / 1024, 2) as MB, sum(blocks) as blocks from dba_free_space group by tablespace_name;

--查看錶空間與數據文件對應關係
select tablespace_name, file_name, bytes/1024/1024 from dba_data_files;

--查詢表空間存放的對象及其擁有者
 select distinct segment_type, owner, tablespace_name from dba_segments where tablespace_name='SC';

 

1. 建立表空間操作系統

create tablespace you_tabs_name datafile  '/opt/oracle/oradata/orcl/ts_01.dbf' size 50m autoextend on;

2. 建立臨時表空間(通常用於數據查詢時排序操做,在數據量較大內存不足時)code

SQL> create temporary tablespace temp2 tempfile '/opt/oracle/oradata/temp02.dbf' size 100m autoextend on;
--能夠爲新建立的表空間增長數據文件
SQL> alter tablespace temp2 add tempfile '/opt/oracle/oradata/temp03.dbf' size 100m ;

3. 建立一個用戶指定默認表空間和臨時表空間對象

create user youuname identified by 「yourpwd」 default tablespace ts_01 temporary tablespace temp2 profile default quota unlimited on ts_01

4. 建立完成後可查詢某個用戶的默認表空間blog

select default_tablespace, temporary_tablespace, d.username  from dba_users d where d.username='SCOTT';

5.若是用戶已經建立,能夠受權新的表空間給用戶使用排序

alter user uname quota unlimited on  ts_01;
alter user scott default tablespace ts_01; -- 更改用戶的默認表空間

 

6. 當一個表空間再也不使用時,可對該表空間下的數據文件離線(離線前千萬別在操做系統層刪除數據文件)索引

alter database datafile '/opt/oracle/oradata/orcl/ts_01.dbf' offline;

 

7. 設置表空間爲只讀,用於備份和恢復

表空間只讀狀態下,不能進行insert,update,delete,但能夠刪除索引和目錄。
必須知足如下條件:
(1)    必須爲online
(2)    不能包含任何回滾段
(3)    表空間不能在歸檔模式或數據發行中
SQL>alter tablespace tablespace_name read only;
恢復讀寫狀態
SQL>alter tablespace tablespace_name read write;

 

8. 重命名錶空間

更變名稱後,原表空間存放的對象也同時更變,包括用戶對於這表空間的權限也同步更改
條件:
(1)    只能對通常表空間改名,不能更變system,sysaux改名
(2)    必須處於online狀態
SQL>alter tablespace old_name rename to new_name;

 

 

2、臨時表空間操做

臨時表空間通常用於數據量比較大的查詢結果中排序操做(當可以使用內存比較小的狀況下):

1. 查看目前現有的臨時表空間

SQL> select tablespace_name,file_name,bytes/1024/1024 file_size,autoextensible from dba_temp_files;
SQL> select name,bytes/1024/1024 from v$tempfile;

2. 建立臨時表空間和更改一個用戶臨時表空間

SQL> create temporary tablespace temp2 tempfile '/opt/oracle/oradata/temp02.dbf' size 100m autoextend on;
--能夠爲新建立的表空間增長數據文件 SQL> alter tablespace temp2 add tempfile '/opt/oracle/oradata/temp03.dbf' size 100m ;

--更改用戶臨時表空間

  SQL> alter user scott temporary tablespace temp1;

 

3. 可修改數據庫管理默認使用的臨時表空間

SQL> alter database default temporary tablespace temp1;

4. 若是一個臨時表空間沒有用戶在使用時,可刪除

SQL> drop tablespace temp;
而後再經過操做系統命令刪除數據文件便可

5. 更改表空間大小或設置自動擴展

alter database tempfile '/opt/oracle/oradata/orcl/sc_temp_01.dbf' resize 50M;
alter database tempfile 'opt/oracle/oradata/oracle/sc_temp_01.dbf' autoextend on next 3M maxsize unlimited;

6. 刪除一個數據文件

alter database tempfile '/opt/oracle/oradata/orcl/sc_temp_01.dbf' offline;//先把文件離線
alter database tempfile '/opt/oracle/oradata/orcl/sc_temp_01.dbf' drop;

--完全刪除一個臨時表空間,謹慎使用
drop tablespace temp1 including contents and datafiles

 

3、UNDO表空間

undo表空間做用:回退事務,讀一致性,事務恢復(斷電,內存故障,後臺進程故障等),閃回操做,UNDO 不屬於任何用戶,由庫公用,默認是自動增加的,直至耗完磁盤.

 

--顯示正在使用的undo表空間
show parameter undo_tablespace;

--查詢全部undo表空間
select tablespace_name from dba_tablespaces where contents='UNDO';

 

 

 

1 .建立UNDO表空間,這裏設置固定大小2G

SQL>create undo tablespace undotbs02 datafile '/xx/xxx/' size 2G;

2. UNDO表空間離線和刪除

SQL>alter system set undo_tablespace=undotbs02;
SQL>alter tablespace tablespace_name offline;
SQL>drop tablespace undotbs02;

離線時,必需要有替代的undo表空間。若某個事務正在使用,則還不能離線,查詢SQL以下:

SQL>SELECT SEGMENT_NAME, XACTS, V.STATUS FROM V$ROLLSTAT V, DBA_ROLLBACK_SEGS WHERE TABLESPACE_NAME = 'undotbs02' AND SEGMENT_ID = USN; 

當沒有數據行時才能夠離線,若是有數據行,可查看是否有事務在進行:
SELECT S.SID, S.SERIAL#, S.USERNAME, R.NAME
"ROLLBACK" FROM V$SESSION S, V$TRANSACTION T, V$ROLLNAME R WHERE S.TADDR = T.ADDR AND T.XIDUSN = R.USN;
相關文章
相關標籤/搜索