Tablespace表空間刪除

一、普通表空間刪除:
Oracle 11g刪除表空間語法描述:
DROP TABLESPACE tablespace_name   [ including contents [ and datafiles ] [ CASCADE CONSTRAINT 搜索] ]; 
  無選項 -- 當(dāng)表空間為空才能刪除;  
  including contents — 刪除表空間及對象;  
  including contents and datafiles — 刪除表空間、對象及數(shù)據(jù)文件;  
  including contents CASCADE CONSTRAINT — 刪除關(guān)聯(lián);  
  including contents and datafiles cascade constraint -- 含前兩項。


生成腳本:
select 'drop tablespace '||tablespace_name||' including contents and datafiles cascade constraint;' from dba_data_files  where tablespace_name not in('SYSTEM','SYSAUX','USERS','EXAMPLE','UNDOTBS2','UNDOTBS1')




二、分區(qū)表空間刪除:


select 'alter table '||owner||'.'||segment_name||' drop partition '||partition_name||' ;'
from dba_segments
where segment_name in (select distinct segment_name
from dba_segments
where tablespace_name = 'p1'
and segment_type like '%PART%')
and tablespace_name <> 'p1'; 


得出: 
alter table CP.IDX_CP_HANDLE_BATCH_NO drop partition SYS_P200 ;
alter table CP.IDX_CP_HANDLE_REQUEST_ID drop partition SYS_P200 ;
alter table CP.IDX_CP_PAYMENT_REQUEST_ID drop partition SYS_P201 ;
alter table CP.IDX_CP_PAYMENT_TRAN_NO drop partition SYS_P201 ;
alter table CP.IDX_CP_REQUEST_ID drop partition SYS_P199 ;
alter table CP.IDX_CP_REQUEST_TRAN_NO drop partition SYS_P199 ;
alter table CP.TBL_CP_HANDLE drop partition SYS_P200 ;
alter table CP.TBL_CP_PAYMENT drop partition SYS_P201 ;
alter table CP.TBL_CP_REQUEST drop partition SYS_P199 ; 


三、異常處理:
報錯有下面幾種:
一. ORA-23515
--- ORA-23515: materialized views and/or their indices exist in the tablespace
drop tablespace crm_data including contents and datafiles
*
ERROR at line 1:
ORA-23515: materialized views and/or their indices exist in the tablespace


意思是:該表空間 CRM_DATA含有物化視圖,或者含有物化視圖的索引
解決辦法:
-- 首先刪掉該表空間下的的物化視圖
select 'drop  materialized view '||owner||'.'||segment_name||' ;'
  from dba_segments
 where segment_name in (select mview_name from dba_mviews)
   and tablespace_name = 'CRM_DATA'


-- 然后刪除該表空間下的其他表空間下物化視圖在本表空間下創(chuàng)建的索引
select *
  from dba_segments
 where tablespace_name = 'CRM_DATA'
   and segment_name in
       (select index_name
          from dba_indexes
         where table_name in (select mview_name from dba_mviews));
二. ORA-02429
---ORA-02429: cannot drop index used for enforcement of unique/primary key
drop tablespace crm_idx including contents cascade constraints
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-02429: cannot drop index used for enforcement of unique/primary key
ORA-02429的意思是: 讓你刪除該表空間下面的 primary key 和 unique key
處理辦法:
select 'alter table '||owner||'.'||table_name||' drop constraint '||constraint_name||' ;'
  from dba_constraints
 where constraint_type in ('U', 'P')
   and (index_owner, index_name) in
       (select owner, segment_name
          from dba_segments
         where tablespace_name = 'CRM_IDX');


三. ORA-14404
--ORA-14404: partitioned table contains partitions in a different tablespace
drop tablespace crm_arc_data including contents and datafiles
*
ERROR at line 1:
ORA-14404: partitioned table contains partitions in a different tablespace
意思是: 本表空間下面有這么樣一個或一些分區(qū)表的分區(qū): this partition OR partitions的table所包含的全部 partitions不在一個表空間下面:
處理辦法:
select 'alter table '||owner||'.'||segment_name||' drop partition '||partition_name||' ;'
  from dba_segments
 where segment_name in (select distinct segment_name
                          from dba_segments
                         where tablespace_name = 'CRM_ARC_DATA'
                           and segment_type like '%PART%')
   and tablespace_name <> 'CRM_ARC_DATA';
殺手锏: 直接drop 這個分區(qū)表(如果允許的話)


四. ORA-02449
--- ORA-02449: unique/primary keys in table referenced by foreign keys
drop tablespace crm_data including contents and datafiles
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
意思是: 這個要刪除的表空間 里面含有這么樣的一些主鍵: 其他表空間的表在這些主鍵上建有外鍵
處理辦法: 去掉這些垃圾外鍵
select 'alter table '||owner||'.'||table_name||' drop constraint '||constraint_name||' ;'
  from dba_constraints
 where constraint_type = 'R'
   and table_name in (select segment_name
                        from dba_segments
                       where tablespace_name = 'CRM_DATA'
                         and segment_type like '%TABLE%');                       
如果還是不行的話,就用這個語句來刪表空間吧:
drop tablespace crm_data including contents cascade constraints


網(wǎng)站名稱:Tablespace表空間刪除
當(dāng)前地址:http://muchs.cn/article28/picdcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航網(wǎng)站營銷、網(wǎng)站制作、企業(yè)建站、網(wǎng)站內(nèi)鏈用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)