Oracle表空間和數據文件詳解(二)

在SYSTEM模式下,從DBA_DATA_FILES數據字典中獲得。
Col tablespace_name for a10;
Col file_name for a50;
Col bytes for 999,999,999;
Select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;

查詢users表空間內存放的數據對象及其類型和擁有者
Col owner for a10;
Col segment_type for a20;
Col segment_name for a30;
Select segment_type,segment_name,owner from dba_segments where tablespace_name='USERS';

查詢sysaux表空間所存放的用戶及其所擁有的對象數量
Select owner,count(segment_name) from dba_segments where tablespace_name='SYSAUX' group by owner;

create [smallfile/bigfile] tablespace tablespace_name
datafile '/path/filename' size num[k/m] reuse [,...]
[autoextend [on|off] next num [k/m]
[maxsize [unlimited | num[k/m]]]]
[mininum extent num[k/m]]
[default storage storage]
[online|offline]
[logging|nologging]
[permanent|temporary]
[extent management dictionary|local [autoallocate|uniform size num[k/m]]];

關鍵字
smallfile/bigfile 創建小文件表空間還是大文件表空間
autoextend [on|off] next 數據文件爲自動擴展還是非自動擴展 如果自動擴展需要設置next的值
maxsize 當數據文件自動擴展時,允許數據文件擴展的最大長度字節數,unlimited則不需要指定字節長度
mininum extent 最小的長度,由操作系統和數據庫的塊決定
online|offline 創建表空間時可以指定在線或者離線
permanent|temporary 永久表空間還是臨時表空間,默認永久性表空間
logging|nologging 該表空間內的表在加載數據時是否產生日誌,默認產生日誌,即使設置nologging,但進行insert、update、delete時,oracle仍會將操作信息記錄到redo log buffer中
extent management dictionary|local 表空間擴展方式是使用數據字典管理還是本地化管理,默認本地化管理,不推薦數據字典管理
autoallocate|uniform size 如果採用本地化管理,表空間擴展時,指定每次盤區擴展的大小是由系統自動指定還是按照等同大小進行,如果按照等同大小進行,默認每次擴展的大小是1MB
參數
 tablespace_name 表空間名稱
'/path/filename' 數據文件的路徑和名字 reuse表示如果文件存在則清除該文件再重新建立該文件,如果文件不存在則創建
default storage storage 指定以後要創建表、索引、簇的存儲參數值

通過本地化管理方式創建表空間
通過本地化管理方式創建一個表空間,其擴展大小爲等同256kb
create tablespace tbs_test_1 datafile 'D:\oraclefiles\datafile1.dbf'
size 10m
extent management local uniform size 256k;

通過本地化管理方式創建一個表空間,其擴展大小爲自動管理
create tablespace tbs_test_2 datafile 'D:\oraclefiles\datafile2.dbf'
size 10m
extent management local autoallocate;

通過段空間管理方式創建表空間
段空間管理方式是建立在本地化表空間管理方式基礎上,使用
segment space management manual/auto 手工段和自動段兩種空間管理方式
手工段空間管理方式是使用pct_free\pct_userd參數標識可供插入使用的數據塊
通過本地化管理方式創建表空間,擴展大小爲自動管理,段空間管理方式爲手工
create tablespace tbs_test_3 datafile 'D:\oraclefiles\datafile3.dbf'
size 20m
extent management local autoallocate
segment space management manual;
自動段管理,數據庫使用位圖來標識哪些數據塊可以用於插入操作,哪些數據塊需要從自由列表上取下,此時會忽略pct_free\pct_userd參數
通過本地化管理方式創建表空間,擴展大小爲自動管理,段空間管理方式爲自動
create tablespace tbs_test_4 datafile 'D:\oraclefiles\datafile4.dbf'
size 20m
extent management local autoallocate
segment space management auto;
自動段管理方式不能用於創建臨時表空間和系統表空間
oracle本身推薦自動段空間方式管理永久表空間

創建非標準塊的表空間,塊大小是標準塊的2倍
Alter system set db_16k_cache_size=16 M scope=both;
create tablespace tbs_test_5 datafile 'D:\oraclefiles\datafile5.dbf'
size 64m reuse
autoextend on next 4m maxsize unlimited
blocksize 16k
extent management local autoallocate
segment space management auto;

創建一個大文件表空間,指定一個數據文件,並且數據文件大小爲2gb
create bigfile tablespace tbs_test_big datafile 'D:\oraclefiles\datafilebig.dbf'
size 2g;
修改大文件表空間其空間大小改變爲1gb
alter tablespace tbs_test_big resize 1g;

傳統表空間如果改變數據文件大小
alter database datafile  'D:\oraclefiles\datafile5.dbf'
resize 100m;

設置默認表空間
alter database default temprory tablespace temp_1;
alter database default tablespace tbs_example;

更改表空間讀寫狀態
設置表空間只讀必須滿足
表空間爲online狀態;不包含任何回滾段;不能在歸檔模式下
alter tablespace tbs_test_3 read only;
alter tablespace tbs_test_3 read write;

重命名錶空間
數據庫管理員只能對普通表空間重命名,不能對system和sysaux表空間進行重命名,也不能對處於offline狀態的表空間重命名
alter tablespace tbs_test_3 rename to tbs_test_3_new;

刪除表空間
drop tablespace tbs_test_3
including contents   --刪除表空間的同時刪除表空間中的數據
cascade constraints; --刪除表空間的同時刪除完整性限制

向表空間中添加數據文件
向users表空間中添加一個新的數據文件users02.dbf,該文件支持自動擴展,擴展能力爲每次擴展5mb,並且該文件的最大空間不受限制
alter tablespace users add datafile 'd:\app\Administrator\oradata\orcl\users02.dbf'
size 10m autoextend on next 5m maxsize unlimited;

從表空間中刪除文件
刪除users表空間中users02.dbf數據文件
alter tablespace users drop  datafile 'd:\app\Administrator\oradata\orcl\users02.dbf';

對數據文件的自動擴展設置
select file_name,autoextensible from dba_data_files
where tablespace_name='TBS_TEST_5';
alter database datafile  'D:\oraclefiles\datafile5.dbf'
autoextend on next 10m maxsize unlimited;

創建臨時表空間
create tempory tablespace temp_01 tempfile
 'D:\oraclefiles\tempfile1.tpf' size 300m;

查詢臨時表空間信息
col file_name for a40;
col tablespace_name for a10;
select file_name,bytes,tablespace_name from dba_temp_files;

臨時表空間組
創建
CREATE TEMPORARY TABLESPACE tp1 TEMPFILE
    'D:\oraclefiles\tempfile1.tpf'
     SIZE 10M   
     TABLESPACE GROUP temp_group1;
CREATE TEMPORARY TABLESPACE tp2 TEMPFILE
    'D:\oraclefiles\tempfile2.tpf'
     SIZE 10M   
     TABLESPACE GROUP temp_group1;

轉移
CREATE TEMPORARY TABLESPACE tp3 TEMPFILE
    'D:\oraclefiles\tempfile3.tpf'
     SIZE 10M   
     TABLESPACE GROUP temp_group2;
ALTER TABLESPACE tp1 TABLESPACE GROUP temp_group2;

把臨時表空間分配給指定用戶使用
alter user hr temporary tablespace group2;

設置默認的臨時表空間組
alter database orcl default temporary tablespace group2;

刪除臨時表空間組
drop tablespace tp1 including contents and datafiles;
drop tablespace tp2 including contents and datafiles;
select * from dba_tablespace_groups where group_name='GROUP2';
組的刪除時通過刪除組內所有的臨時表空間實現

撤銷表空間
創建
create undo tablespace undo_tbs1
datafile   'D:\oraclefiles\undofile1.dbf'
size 3g;
修改
alter tablespace ...add datafile
alter tablespace...rename datafile
achivlog alter tablespace ...begin backup/end backup
切換undo表空間
alter system set undo_tablespace=undo_tbs1;
刪除
alter system set undo_tablespace=undo_tbs2;
drop tablespace undo_tbs1;
查看信息
當前例程正在使用的undo表空間
show parameter undo_tablespace;
實例的所有undo表空間
select tablespace_name from dba_tablespaces where contents='UNDO';

發佈了48 篇原創文章 · 獲贊 50 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章