---
title: "oracle 在删除表，表空间，用户时 如何释放磁盘空间 "
author: "zyfromcq"
date: "2016-03-10T11:30:47Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2016-03-10T11:30:47Z"
id: 2281898
url: "https://zyfromcq.iteye.com/blog/2281898"
---

一、drop表

执行drop table xx 语句

drop后的表被放在回收站(user_recyclebin)里，而不是直接删除掉。这样，回收站里的表信息就可以被恢复，或彻底清除。

通过查询回收站user_recyclebin获取被删除的表信息，然后使用语句

flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop [rename to <new_table_name>];

将回收站里的表恢复为原名称或指定新名称，表中数据不会丢失。

若要彻底删除表，则使用语句：drop table <table_name> purge; 

清除回收站里的信息

清除指定表：purge table <table_name>;

清除当前用户的回收站：purge recyclebin;

清除所有用户的回收站：purge dba_recyclebin;

不放入回收站，直接删除则是：drop table xx purge;

举例如下：

===============================================================================

SQL> select * from test1;

A B C

-- -- ----------

11 5 

11 10 

2 rows selected

SQL> create table test2 as select * from test1;

Table created

SQL> select * from test2;

A B C

-- -- ----------

11 5 

11 10 

2 rows selected

SQL> drop table test2;

Table dropped

SQL> select object_name, original_name, operation, type from user_recyclebin;

OBJECT_NAME ORIGINAL_NAME OPERATION TYPE

------------------------------ -------------------------------- --------- -------------------------

BIN$vQwemDg4R9mK9fYJNdYzvg==$0 TEST2 DROP TABLE

SQL> flashback table test2 to before drop rename to test3;--【to test3】将表重命名

Done

SQL> select * from test3;

A B C

-- -- ----------

11 5 

11 10 

2 rows selected

SQL> select * from test2

ORA-00942: 表或视图不存在

--彻底删除表

SQL> drop table test3 purge;

Table dropped

二、清除表中的数据

truncate操作 同没有where条件的delete操作十分相似，只是把表里的信息全部删除，但是表依然存在。

例如：truncate table XX

Truncate不支持回滚，并且不能truncate一个带有外键的表，如果要删除首先要取消外键，然后再删除。

truncate table 后，有可能表空间仍没有释放，可以使用如下语句：

alter table 表名称 deallocate UNUSED KEEP 0;

注意如果不加KEEP 0的话，表空间是不会释放的。

例如：

alter table F_MINUTE_TD_NET_FHO_B7 deallocate UNUSED KEEP 0;

或者：

TRUNCATE TABLE (schema)table_name DROP(REUSE) STORAGE才能释放表空间。

例如： truncate table test1 DROP STORAGE;

三、查询分区表存在哪些分区：

查询分区表的情况，可以在USER_TAB_PARTITIONS中查询。例如：

select 'alter table '||t.table_name ||' truncate partition ' || t.partition_name from USER_TAB_PARTITIONS t where t.table_name like 'F_%'

清除指定某个分区表的分区数据：

alter table 表名称 truncate partition 分区名称;

四、清除分区表占用的空间：

alter table 表名称 DROP partition 分区名称;

例如：

alter table F_HOUR_TD_NET_MPVOICE DROP partition P_09121913 ;

五、查询表空间信息

可以利用如下语句查询各表在存储空间的使用分情况：

SELECT TABLESPACE_NAME,TO_CHAR(SUM(BYTES)/(1024*1024),'999G999D999') CNT_MB FROM DBA_EXTENTS WHERE OWNER='&OWNER' AND SEGMENT_NAME='&TABLE_NAME' AND SEGMENT_TYPE LIKE 'TABLE%' GROUP BY TABLESPACE_NAME;

可以使用如下语句，查询存储空间情况：

Select Tablespace_Name, Sum(bytes)/1024/1024 From Dba_Segments group By Tablespace_Name

六、查询用户下的表

如果你的用户权限不是DBA： 

那你用 

select * from user_tables; 

可以查询到当前用户所拥有的表。 

如果是DBA用户： 

select * from dba_tables; 