一個用戶要刪除數據庫的表空間,首先該用戶要具備drop tablespace的權限
在當前用戶下執行以下語句來查詢確認
select a2.username,a1.privilege from dba_sys_privs a1 , user_role_privs a2
where a1.privilege = 'DROP TABLESPACE'
and a1.grantee =a2.granted_role
SQL> conn rman/rmanbak
Connected.
SQL> select a2.username,a1.privilege from dba_sys_privs a1 , user_role_privs a2
2 where a1.privilege = 'DROP TABLESPACE'
3 and a1.grantee =a2.granted_role
4 /
USERNAME PRIVILEGE
------------------------------ ----------------------------------------
RMAN DROP TABLESPACE
如果沒有 drop tablespace,請先用更高級的用戶(如sys)給予授權
SQL> conn /as sysdba
Connected.
SQL> grant drop tablespace to rman;
Grant succeeded.
以上前提條件確認完,下面開始實驗.
確認已有的表空間
SQL> select a.TS# ,a.NAME , b.NAME from v$tablespace a,v$datafile b where a.TS# = b.TS#;
TS# NAME NAME
---------- ------------------------------ --------------------------------------------------------------------------------
0 SYSTEM /opt/app/oracle/oradata/orcl/system01.dbf
1 UNDOTBS1 /opt/app/oracle/oradata/orcl/undotbs01.dbf
2 SYSAUX /opt/app/oracle/oradata/orcl/sysaux01.dbf
4 USERS /opt/app/oracle/oradata/orcl/users01.dbf
6 RMAN /data/backup/rman/rman01.dbf
創建新的測試表空間:
SQL> create tablespace mytbs01
2 logging
3 datafile '/opt/app/oradata/orcl/mytbs01.dbf' size 10m REUSE ;
表空間已創建。
SQL> CREATE TABLESPACE "MYTBS02"
2 LOGGING
3 DATAFILE '/opt/app/oradata/orcl/MYTBS02.dbf' SIZE 10M REUSE ;
表空間已創建。
如果表空間裏面沒有任何數據對象,可以直接刪除.
SQL> drop tablespace mytbs01;
表空間已丟棄。
這種刪除方式相關的數據文件仍然存在於磁盤上.
(可以進入數據文件所在目錄/opt/app/oradata/orcl/進行查看 MYTBS01.DBF )
如果表空間裏面含有數據對象,那麼該表空間就不能像上面那樣做直接刪除了.
SQL> create tablespace mytbs01
2 logging
3 datafile '/opt/app/oradata/orcl/mytbs01.dbf' size 10m REUSE;
表空間已創建。
SQL> create table test(mobile number(13))
2 tablespace mytbs01;
表已創建。
SQL> drop tablespace mytbs01;
drop tablespace mytbs01
*
ERROR 位於第 1 行:
ORA-01549: 表空間非空,請使用 INCLUDING CONTENTS 選項
看到了吧,提示ora-01549錯誤.
如果要刪除該表空間,可加上including contents子句.
如: drop tablespace mytbs01 including contents ;
如果想在刪除表空間的同時也刪除掉對應的數據文件,那就在上面的語句最後加上 and datafiles
成爲 drop tablespace mytbs01
including contents and datafiles;
SQL> drop tablespace mytbs01
2 including contents and datafiles;
表空間已丟棄。
要注意的一點是,如果drop tablespace語句中含有datafiles,那datafiles之前必須有contents關鍵字,不然會提示ora-01911錯誤:
SQL> drop tablespace mytbs02
2 including datafiles;
including datafiles
*
ERROR 位於第 2 行:
ORA-01911: 需要 CONTENTS 關鍵字
接下來的實驗是:
如果表空間A中有一個表ta,表空間B中有一個表tb,而ta與tb有着某種關係,那麼是否可以按上面的方法直接幹掉表空間A和表空間B呢?
看試驗過程:
SQL> create tablespace mytbs01
2 logging
3 datafile '/opt/app/oradata/orcl/mytbs01.dbf' size 10m REUSE;
表空間已創建。
SQL> CREATE TABLESPACE "MYTBS02"
2 LOGGING
3 DATAFILE '/opt/app/oradata/orcl/MYTBS02.dbf' SIZE 10M REUSE ;
CREATE TABLESPACE "MYTBS02"
*
ERROR 位於第 1 行:
ORA-01543: 表空間 'MYTBS02' 已經存在
SQL> create table test(mobile number(13))
2 tablespace mytbs01;
表已創建。
SQL> create table test2(mobile number(13))
2 tablespace mytbs02;
表已創建。
SQL> alter table TEST2
2 add primary key (mobile);
表已更改。
SQL> ALTER TABLE test
2 ADD CONSTRAINT FOREIGN con_mobile REFERENCES test2(mobile);
表已更改。
以上的意思是:在表空間mytbs01上創建表test,在表空間mytbs02上創建表test2,兩個表test和test2以外鍵相關聯,test2爲主表,test爲從表.
現在嘗試能否用上面的語句直接幹掉mytbs02:
SQL> drop tablespace mytbs02;
drop tablespace mytbs02
*
ERROR 位於第 1 行:
ORA-01549: 表空間非空,請使用 INCLUDING CONTENTS 選項
SQL> drop tablespace mytbs02
2 including contents;
drop tablespace mytbs02
*
ERROR 位於第 1 行:
ORA-02449: 表中的唯一/主鍵被外部關鍵字引用
SQL> drop tablespace mytbs02
2 including contents and datafiles;
drop tablespace mytbs02
*
ERROR 位於第 1 行:
ORA-02449: 表中的唯一/主鍵被外部關鍵字引用
可見主表所在表空間因爲表與其他空間上的表有聯繫,所以沒辦法直接刪掉.
那mytbs01表空間能不能幹掉?請看:
SQL> drop tablespace mytbs01
2 including contents and datafiles;
表空間已丟棄。
SQL> drop tablespace mytbs02
2 including contents and datafiles ;
表空間已丟棄。
嘿嘿,從表test所在的表空間mytbs01能直接幹掉,而且從表的表空間幹掉後,主表test2所在的表空間mytbs02也能幹掉了!
那麼,如果我只想幹掉主表所在的表空間,又不想幹掉從表所在的表空間那怎麼辦?
很簡單,最笨的一招就是想把兩個表的關聯關係給滅了,(在上面的兩個表中,就是把那個外鍵給刪了)再把主表表所在的表空間刪了.但這種方法可不太現 實,如果一個表空間裏有成百上千個對象與別的表空間裏的對象有聯繫,總不能一個一個去"解鈴"吧? 即使你本人就是"系鈴"人,估計你也要"解"到鬱悶死!
一個更簡單的方法當然是級聯刪除了!
drop tablespace mytbs02
including contents and datafiles cascade constraints
試驗如下:
SQL> create tablespace mytbs01
2 logging
3 datafile '/opt/app/oradata/orcl/mytbs01.dbf' size 10m REUSE;
表空間已創建。
SQL> CREATE TABLESPACE "MYTBS02"
2 LOGGING
3 DATAFILE '/opt/app/oradata/orcl/MYTBS02.dbf' SIZE 10M REUSE ;
表空間已創建。
SQL> create table test(mobile number(13))
2 tablespace mytbs01;
表已創建。
SQL> create table test2(mobile number(13))
2 tablespace mytbs02;
表已創建。
SQL> alter table TEST2
2 add primary key (mobile);
表已更改。
SQL> ALTER TABLE test
2 ADD CONSTRAINT FOREIGN con_mobile REFERENCES test2(mobile);
表已更改。
SQL> drop tablespace mytbs02
2 including contents and datafiles cascade constraints;
表空間已丟棄。
OK!正常搞定!