Oracle 查看錶空間名稱及大小和刪除表空間及數據文件方法

--1、查看錶空間的名稱及大小 
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size 
FROM dba_tablespaces t, dba_data_files d 
WHERE t.tablespace_name = d.tablespace_name 
GROUP BY t.tablespace_name; 
--2、查看錶空間物理文件的名稱及大小 
SELECT tablespace_name, 
file_id, 
file_name, 
round(bytes / (1024 * 1024), 0) total_space 
FROM dba_data_files 
ORDER BY tablespace_name; 
--3、查看回滾段名稱及大小 
SELECT segment_name, 
tablespace_name, 
r.status, 
(initial_extent / 1024) initialextent, 
(next_extent / 1024) nextextent, 
max_extents, 
v.curext curextent 
FROM dba_rollback_segs r, v$rollstat v 
WHERE r.segment_id = v.usn(+) 
ORDER BY segment_name; 
--4、查看控制文件 
SELECT NAME FROM v$controlfile; 
--5、查看日誌文件 
SELECT MEMBER FROM v$logfile; 
--6、查看錶空間的使用情況 
SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name 
FROM dba_free_space 
GROUP BY tablespace_name; 
SELECT a.tablespace_name, 
a.bytes total, 
b.bytes used, 
c.bytes free, 
(b.bytes * 100) / a.bytes "% USED ", 
(c.bytes * 100) / a.bytes "% FREE " 
FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c 
WHERE a.tablespace_name = b.tablespace_name 
AND a.tablespace_name = c.tablespace_name; 
--7、查看數據庫庫對象 
SELECT owner, object_type, status, COUNT(*) count# 
FROM all_objects 
GROUP BY owner, object_type, status; 
--8、查看數據庫的版本  
SELECT version 
FROM product_component_version 
WHERE substr(product, 1, 6) = 'Oracle'; 
--9、查看數據庫的創建日期和歸檔方式 
SELECT created, log_mode, log_mode FROM v$database; 

 

--1G=1024MB 
--1M=1024KB 
--1K=1024Bytes 
--1M=11048576Bytes 
--1G=1024*11048576Bytes=11313741824Bytes 
SELECT a.tablespace_name "表空間名", 
total "表空間大小", 
free "表空間剩餘大小", 
(total - free) "表空間使用大小", 
total / (1024 * 1024 * 1024) "表空間大小(G)", 
free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", 
(total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", 
round((total - free) / total, 4) * 100 "使用率 %" 
FROM (SELECT tablespace_name, SUM(bytes) free 
FROM dba_free_space 
GROUP BY tablespace_name) a, 
(SELECT tablespace_name, SUM(bytes) total 
FROM dba_data_files 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name = b.tablespace_name 

如何查詢當前用戶的表空間名稱?因爲oracle建立索引,需要知道當前用戶的表空間,查找了一下資料

 --查詢語法--
 select default_tablespace from dba_users where username='登錄用戶'
   

如,我的登錄用戶是TMS21,那麼查詢語法是

 /* 查看用戶所屬的表空間 */
 select default_tablespace from dba_users where username='TMS21';

相關查詢的用法,也順便記錄一下

1)查詢當前用戶表空間

 /* 查看用戶所屬的表空間 */
 select default_tablespace from dba_users where username='TMS21';

2)查詢所有表空間

  /*查看所有的表空間 */
   -- 1 )方式1:dba_tablespaces --
   select * from dba_tablespaces;
    --2 )方式2:v$tablespace --
   select * from v$tablespace;   

3)查詢用戶下所有表

  /* 查看用戶下面的所有的表 */ 
  -- 1 )方式1:user_tables --
   select * from user_tables;
   --2 )方式2: dba_tables --
   select * from dba_tables where owner='TMS21';

4)查詢表空間下的用戶

  /*查看錶空間下有多少用戶,tablespace_name表空間 的名字一定要大寫 */
   select distinct s.owner from dba_segments s where s.tablespace_name ='TMS21';  

PS:因爲我的表空間名稱跟用戶一樣,所以4)中的表空間都是TMS21(以查詢結果爲主)

 

--刪除空的表空間,但是不包含物理文件
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;

 

以system用戶登錄,查找需要刪除的用戶:

--查找用戶
select * from dba_users;
--查找工作空間的路徑
select * from dba_data_files; 
--刪除用戶
drop user 用戶名稱 cascade;
--刪除表空間
drop tablespace 表空間名稱 including contents and datafiles cascade constraint;

例如:刪除用戶名成爲ABC,表空間名稱爲ABC

--刪除用戶,及級聯關係也刪除掉
drop user ABC cascade;
--刪除表空間,及對應的表空間文件也刪除掉
drop tablespace ABC including contents and datafiles cascade constraint;

刪除無任何數據對象的表空間:
首先使用PL/SQL界面化工具,或者使用oracle自帶的SQL PLUS工具,連接需要刪除的表空間的oracle數據局庫。
確認當前用戶是否有刪除表空間的權限,如果沒有 drop tablespace,請先用更高級的用戶(如sys)給予授權或者直接用更高級的用戶。
用drop tablespace xxx ,刪除需要刪除的表空間。
刪除有任何數據對象的表空間
使用drop tablespace xxx including contents and datafiles;來刪除表空間。
注意事項:
如果drop tablespace語句中含有datafiles,那datafiles之前必須有contents關鍵字,不然會提示ora-01911錯誤

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章