当前位置: 首页 > news >正文

删除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';

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

转载于:https://www.cnblogs.com/ritchy/p/10647651.html

相关文章:

  • 设计模式:对问题行之有效的解决方式。其实它是一种思想。
  • Linux实战教学笔记02:计算机系统硬件核心知识
  • pytorch Debug —交互式调试工具Pdb (ipdb是增强版的pdb)-1-使用说明
  • 程序结构
  • 设置selinux开机自动关闭
  • Python通用编程 - 第四章:字符编码
  • 微信小程序商店域名由wxapp.dreawer.com永久更换为:store.dreawer.com
  • Java数据解析之JSON
  • 浏览器的兼容性
  • Java基础3:深入理解String及包装类
  • Java基础7:关于Java类和包的那些事
  • 关于从业以来第一个项目的总结
  • 引用还是指针?
  • 大神教你如何解决Linux系统80端口被占用
  • java B2B2C springmvc mybatis多租户电子商城系统-gateway 限流
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • Android开源项目规范总结
  • CAP理论的例子讲解
  • CentOS从零开始部署Nodejs项目
  • C语言笔记(第一章:C语言编程)
  • ES2017异步函数现已正式可用
  • java中的hashCode
  • JS基础之数据类型、对象、原型、原型链、继承
  • oschina
  • vue2.0项目引入element-ui
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 对超线程几个不同角度的解释
  • 记录一下第一次使用npm
  • 理清楚Vue的结构
  • 两列自适应布局方案整理
  • 全栈开发——Linux
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • #162 (Div. 2)
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • $.proxy和$.extend
  • %check_box% in rails :coditions={:has_many , :through}
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (C++17) optional的使用
  • (C语言)逆序输出字符串
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (转)关于pipe()的详细解析
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .NET Core 版本不支持的问题
  • .NET Core/Framework 创建委托以大幅度提高反射调用的性能
  • .NET 中的轻量级线程安全
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .net对接阿里云CSB服务
  • .net实现客户区延伸至至非客户区
  • .NET文档生成工具ADB使用图文教程