--转载自 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';

--查看所有表空间总大小、已使用大小、剩余大小
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;

--查找表空间存储文件的路径
select * from dba_data_files;

--查看所有表占用的表空间的大小
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';

--查看用户所在的表空间(查看当前用户的缺省表空间)
conn JACK
select username,default_tablespace from user_users;

--查看表所属的表空间(查看所有表)
select table_name,tablespace_name from user_tables;

--查看表所属的表空间(查看指定表)
select table_name,tablespace_name from user_tables where table_name=upper('&table_name');

--删除用户
--注意:指定 cascade 会删除用户下的所有对象(包括表、视图、主键、外键、索引等;但不会删除存储过程、函数、包)。如果不指定则仅仅只删除用户,一般建议指定
conn /as sysdba 
drop user myusername cascade;

--删除表空间
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
一、普通表空间删除:
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');

二、分区表空间删除:
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'

-- 然后删除该表空间下的其他表空间下物化视图在本表空间下创建的索引
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 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

删除Oracle用户及表空间的更多相关文章

  1. oracle用户与表空间操作

    oracle系统用户sys,system , sysman, scott 使用system用户登录[username/password][@server][as sysdba|sysoper]eg: ...

  2. Oracle - 用户及表空间的创建和删除

    -- 查询所有用户 SELECT USERNAME FROM ALL_USERS; -- 查询所有表空间 SELECT TABLESPACE_NAME FROM USER_TABLESPACES; - ...

  3. Oracle创建,删除用户与表空间

    1.创建表空间与用户 a:创建数据表空间 create tablespace user_data logging datafile 'D:\oracle\product\10.2.0\oradata\ ...

  4. oracle删除用户及其表空间

    oracle删除用户及其表空间 删除表空间:可以先将其offlinealter tablespace xx offline;将磁盘上的数据文件一同删除drop tablespace xxx inclu ...

  5. Oracle 删除用户和表空间////Oracle创建删除用户、角色、表空间、导入导出、...命令总结/////Oracle数据库创建表空间及为用户指定表空间

    Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用如下命令来完成. 步骤一:  删除user drop ...

  6. Oracle 创建表空间、临时表空间、创建用户并指定表空间、授权,删除用户及表空间

    /* 说明:若已经存在相应的用户和表空间,则需要先删除相应的用户和表空间 然后再全部重新建立 */ --删除用户 drop user USERNAME cascade; --删除表空间 drop ta ...

  7. (总结)Oracle 11g常用管理命令(用户、表空间、权限)

    1.启动oracle数据库: 从root切换到oracle用户进入:su - oracle 进入sqlplus环境,nolog参数表示不登录:sqlplus /nolog 以管理员模式登录:sqlpl ...

  8. oracle数据库_实例_用户_表空间之间的关系(转)

    数据库:Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件).其实Oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库.可 ...

  9. ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限

    Oracle创建用户.表空间.导入导出....命令 //创建临时表空间 create temporary tablespace ext_temptempfile 'D:\oracle\product\ ...

随机推荐

  1. centos tftp和samba的安装与配置

    Tftp服务器的安装于配置 1 安装: 命令:#yum –y install tftp 2 安装完毕之后,将tftp服务器设置为开机启动,方法:(命令)#setup→选择system server→选 ...

  2. SSM-SpringMVC-21:SpringMVC中处理器方法之返回值Object篇

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 今天要记录的是处理方法,返回值为Object的那种,我给它分了一下类: 1.返回值为Object数值(例如1) ...

  3. SSM-SpringMVC-09:SpringMVC中以继承MutiActionController类的方式实现处理器

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- MutiActionController类,多行动处理器,简单来说,就是可以一个处理器中有多个处理方法,分支 ...

  4. linux 安装python3

    下载python安装包 https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz (可选则自己想要的版本) 下载好之后上传到linux系统,开始 ...

  5. Hexo的更新 主题的更换

    1:HEXO更新 ①hexo generate ②hexo deploy 2:  HEXO主题的更换,①找到主题的github地址后 进入自己的HEXO文件夹然后 git clone xxxx(地址) ...

  6. 你不知道的JavaScript--Item28 垃圾回收机制与内存管理

    1.垃圾回收机制-GC Javascript具有自动垃圾回收机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存. 原理:垃圾收集器会定期(周期性 ...

  7. 《javascript语言精粹》读书笔记 Item1 精华与语法

    第一章 精华 任何语言都有其精华的部分和鸡肋的部分,javascript也不例外,而且鸡肋的部分还很多.但javascript的流行却不受他的质量影响. javascript为何如此流行?因为他是we ...

  8. javascript系列1--把字符串当代码来执行

    转发请标明来源:http://www.cnblogs.com/johnhou/p/javascript.html  请尊重笔者的劳动成果  --John Hou 在javascript中有多种方法可以 ...

  9. 玩转Web之html+CSS(一)---论坛首页表格的实现

    转载请说明出处,小编博客地址:http://blog.csdn.net/u012116457 最近本来想去写一个类似论坛的页面,论坛首页一般都需要一个表格去显示数据,自己简单的写了一下,先上一张图 c ...

  10. document_index_data.go

    package types type DocumentIndexData struct {     // 文档全文(必须是UTF-8格式),用于生成待索引的关键词     Content string ...