刪除Oracle用戶及表空間

--轉載自 https://blog.csdn.net/sunny05296/article/details/81126548
--以sysdba用戶登陸,查找須要刪除的用戶
conn / as sysdba函數

--查找用戶
select * from dba_users;
select username from dba_users;
select username from dba_users where username='JACK';
select username from all_users where username='JACK';this

--查看全部表空間總大小、已使用大小、剩餘大小
select a.tablespace_name,
total "Total(M)",
free "Free(M)",
total - free "Used(M)",
round(((total - free) / total) * 100, 2) "Used(%)"
from (select tablespace_name, sum(bytes) / 1024 / 1024 total
from dba_data_files
group by tablespace_name) a,
  (select tablespace_name, sum(bytes) / 1024 / 1024 free
from dba_free_space
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name;spa

--查找表空間存儲文件的路徑
select * from dba_data_files; .net

--查看全部表佔用的表空間的大小
select t.segment_name, t.segment_type, sum(t.bytes / 1024 / 1024) "佔用空間(M)"
from dba_segments t
where t.segment_type='TABLE'
group by OWNER, t.segment_name, t.segment_type;對象

--查看錶所屬的用戶
select owner,table_name from dba_tables where table_name='TEST_TBL01';blog

--查看用戶所在的表空間(查看當前用戶的缺省表空間)
conn JACK
select username,default_tablespace from user_users;索引

--查看錶所屬的表空間(查看全部表)
select table_name,tablespace_name from user_tables;it

--查看錶所屬的表空間(查看指定表)
select table_name,tablespace_name from user_tables where table_name=upper('&table_name'); io

--刪除用戶
--注意:指定 cascade 會刪除用戶下的全部對象(包括表、視圖、主鍵、外鍵、索引等;但不會刪除存儲過程、函數、包)。若是不指定則僅僅只刪除用戶,通常建議指定
conn /as sysdba 
drop user myusername cascade;table

--刪除表空間
drop tablespace mytablespace including contents and datafiles cascade constraint;

例如:刪除用戶JACK及表空間USER_DATA:
--刪除用戶JACK,級聯刪除
drop user JACK cascade;
--刪除表空間,及對應的表空間文件也刪除掉。注意:若是多個用戶使用相同的表空間,刪除用戶時不要刪除表空間
drop tablespace USER_DATA including contents and datafiles cascade constraint;

--Tablespace表空間刪除
--轉自 http://blog.itpub.net/28793776/viewspace-1587612
1、普通表空間刪除:
Oracle 11g刪除表空間語法描述:
DROP TABLESPACE tablespace_name [ including contents [ and datafiles ] [ CASCADE CONSTRAINT 搜索] ];
無選項 -- 當表空間爲空才能刪除;
including contents — 刪除表空間及對象;
including contents and datafiles — 刪除表空間、對象及數據文件;
including contents CASCADE CONSTRAINT — 刪除關聯;
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');

2、分區表空間刪除:
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 ;

3、異常處理:
報錯有下面幾種:
一. 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'

-- 而後刪除該表空間下的其餘表空間下物化視圖在本表空間下建立的索引
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
意思是: 本表空間下面有這麼樣一個或一些分區表的分區: 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 這個分區表(若是容許的話)

四. ORA-02449--- ORA-02449: unique/primary keys in table referenced by foreign keysdrop 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

相關文章
相關標籤/搜索