一、表空間和臨時表空間
二、表空間和數(shù)據(jù)文件
三、維護表空間
1. 創(chuàng)建表空間
2. 創(chuàng)建臨時表空間
3. 向表空間添加數(shù)據(jù)文件
4. 刪除表空間
一、表空間和臨時表空間
表空間:數(shù)據(jù)庫的邏輯存儲結構,用來存儲數(shù)據(jù)表、索引等數(shù)據(jù)對象。任何對象在創(chuàng)建時都需要指定存在哪個表空間。每個數(shù)據(jù)庫至少有一個表空間,如SYSTEM表空間。
臨時表空間:用來存儲臨時段,臨時段的創(chuàng)建通常與排序之類的操作有關,當該空間完成排序操作后oracle系統(tǒng)會自動釋放。我們常用的幾個操作有用到臨時空間,如:select distinct 不重復檢索、union聯(lián)合查詢、minus差值計算等。
二、表空間和數(shù)據(jù)文件
表空間:數(shù)據(jù)庫的邏輯存儲結構
數(shù)據(jù)文件:數(shù)據(jù)庫的物理存儲結構
兩者關系:
(1)每個表空間由一個或許多個數(shù)據(jù)文件組成,一個數(shù)據(jù)文件只能屬于一個表空間

(2)創(chuàng)建表空間時一定要創(chuàng)建數(shù)據(jù)文件,增加數(shù)據(jù)文件時也需指定表空間
三、維護表空間
1. 創(chuàng)建表空間
step1:查詢用戶表空間的數(shù)據(jù)文件
SQL> select name from v$datafile;
可看到系統(tǒng)中存在的數(shù)據(jù)文件,且數(shù)據(jù)文件的存儲路徑:/u01/app/oracle/oradata/ORA11GRZ/。得到數(shù)據(jù)文件的存儲路徑,在創(chuàng)建表空間時需要路徑來創(chuàng)建對應的數(shù)據(jù)文件。
step2:創(chuàng)建表空間 bdi_data,數(shù)據(jù)文件 bdi_data.dbf
SQL> create tablespace bdi_data datafile '/u01/app/oracle/oradata/ORA11GRZ/bdi_data.dbf'? size 100M reuse???
????????? autoextend on next 40M //數(shù)據(jù)文件每次自動擴展為40m
????????? maxsize unlimited //不限制數(shù)據(jù)文件擴展
????????? default storage(initial 128k next 128k minextents 2 maxextents unlimited);
?????????? //initial分配給表(段)的初始區(qū)的大小; next 第二個區(qū)的大小; minextents 指定創(chuàng)建表時,至少要分配多少個區(qū)給這個表(段);maxextents 指定可以給這個表(段)區(qū)的最大數(shù)量

以上是基本創(chuàng)建一個表空間,創(chuàng)建表空間的完整語法如下:
create [ smallfile | bigfile ] tablesapce tablespace_name datafile 'path/filename.dbf' size num[k/m] reuse???? // reuse表示文件存在則清除重建,不存在則創(chuàng)建
[ autoextend [on/off ] next num[k/m] ]? // on自動擴展,需設置next值;off 非自動擴展
[ maxsize [unlimited | num[k/m] ] ]? //自動擴展指定文件擴展最大字節(jié)數(shù)
[ mininum extent num[k/m] ] //指定最小長度,由操作系統(tǒng)和數(shù)據(jù)庫的塊決定
[ default storage storage ] // 指定表、索引等的存儲參數(shù)值
[ online | offline ] //創(chuàng)建表空間可指定為在線or離線
[ logging | nologging ] //表空間的表在加載數(shù)據(jù)時是否產(chǎn)生日志,默認為logging
[ permanent | temporary ] //表空間是永久表空間or臨時表空間,默認為永久
[ extent management dictionary|local [ autoalllocate | uniform size num[k/m] ] ]
// 表空間的擴展方式是使用數(shù)據(jù)字典管理 or 本地化管理,默認為本地化管理。oracle不推薦使用數(shù)據(jù)字典管理表空間。若為本地化管理,autoalllocate | uniform size表示在表空間擴展時,每次盤區(qū)擴展的大小是由系統(tǒng)自動指定還是按照等同大小進行。
2. 創(chuàng)建臨時表空間
step1:查詢用戶臨時表空間的數(shù)據(jù)文件
SQL> select name from v$tempfile;
臨時表空間的存儲位置: /u01/app/oracle/oradata/ORA11GRZ/
step2:創(chuàng)建臨時表空間 bdi_temp,數(shù)據(jù)文件 bdi_temp.dbf
SQL> create temporary tablespace bdi_temp tempfile '/u01/app/oracle/oradata/ORA11GRZ/bdi_temp.dbf.bdf'? size 100m reuse
????????? autoextend on next 20m //數(shù)據(jù)文件每次自動擴展為40m
??????? ? maxsize unlimited;? //不限制數(shù)據(jù)文件擴展

3. 向表空間添加數(shù)據(jù)文件
【例】向表空間 bdi_data 添加一個數(shù)據(jù)文件 bdi_data02.dbf,支持自動擴展,每次擴展40 M,且最大空間不受限制
SQL> alter tablespace bdi_data add datafile '/u01/app/oracle/oradata/ORA11GRZ/bdi_data02.dbf' size 100M? autoextend on next 40M maxsize unlimited;
4. 刪除表空間
【例】刪除表空間 bdi_data 中的數(shù)據(jù)文件 bdi_data02.dbf
SQL>alter tablespace bdi_data drop datafile '/u01/app/oracle/oradata/ORA11GRZ/bdi_data02.dbf';