操作命令:

删除空的表空间,但是不包含物理文件

drop tablespace tablespace_name;

删除非空表空间,但是不包含物理文件

drop tablespace tablespace_name including contents;

删除空表空间,包含物理文件

drop tablespace tablespace_name including datafiles;

删除非空表空间,包含物理文件

drop tablespace tablespace_name including contents and datafiles;

如果其他表空间中的表有外键等约束关联到了本表空间中的表的字段,就要加上 CASCADE CONSTRAINTS

drop tablespace tablespace_name including contents and datafiles CASCADE CONSTRAINTS;

操作顺序:

在没有ASM的单机操作环境中,6、7、8、11、12可跳过。

1. --用户/表空间 对应
SELECT u.username u_name,
         d.tablespace_name ts_name,
         ROUND(NVL(a.bytes,0),2) ts_size
FROM dba_users u,sys.dba_tablespaces d, 
    (SELECT tablespace_name,
         sum(bytes) / 1024 / 1024 / 1024 bytes
    FROM dba_data_files
    GROUP BY  tablespace_name) a
WHERE u.default_tablespace = d.tablespace_name(+)
        AND d.tablespace_name = a.tablespace_name(+)
ORDER BY  1 asc; 

2.--离线表空间
alter tablespace BAOYW offline;

3.--查看用户session是否全部断开(KILL SESSION)
select 'alter system kill session '||''''||sid||','||serial#||''''||';' from v$session where username = 'ZDJC';

4.--锁定用户
alter user BAOYW account lock;

5.--删除用户
drop user BAOYW CASCADE;

--磁盘组 --磁盘组
6.--删除前查看ASM磁盘组大小
select group_number,name,total_mb,free_mb,state from v$asm_diskgroup;

7.--删除前查看ASM磁盘大小
select group_number,name,os_mb,total_mb,free_mb,state,failgroup_type from v$asm_disk;

8.--删除前查看磁盘组使用情况
select a.name,b.name,b.os_mb,b.total_mb/1024,b.free_mb/1024,b.state,failgroup,b.failgroup_type,path
  from v$asm_diskgroup a, v$asm_disk b
 where a.group_number = b.group_number
   and b.name like 'DATADG%';

9.--检查确认数据文件路径、状态、大小
select file_name,file_id,tablespace_name,bytes,status,online_status from dba_data_files where tablespace_name = 'BAOYW';
 
10.--开始删除 表空间 操作 开始删除 表空间 操作
drop tablespace BAOYW including contents and datafiles;

11.--删除完成后,手动平衡磁盘组
sqlplus / as sysasm
ALTER DISKGROUP DATADG REBALANCE POWER 1;

12.--再次查看磁盘组,确认是否回收了相关空间
select group_number,name,total_mb,free_mb,state from v$asm_diskgroup;

注意事项:

如果drop tablespace语句中含有datafiles,那datafiles之前必须有contents关键字,不然会提示ora-01911错误。

Oracle数据库中删除了表空间物理文件XXX.ora后导致用drop tablespace删除表空间失败,解决方法如下:

用sqlplus /nolog命令进入oracle数据库执行如下命令:
  sql>conn /as sysdba;
  sql>startup;(如果数据库已启动则不需要此命令)
  sql>alter database datafile ''/home/oracle/XXX.ora'' offline drop;(/home/oracle/XXX.ora为表空间文件的物理路径)
  sql>drop tablespace XXX;

执行完后,重启数据库即可。