Oracle数据库查看表空间以及修改小空间自动扩展

 -- 1.当前数据文件情况
SELECT B.NAME
      ,A.NAME
      ,A.BYTES/1024/1024  -- 单位为MB
  FROM V$DATAFILE A
      ,V$TABLESPACE B
 WHERE A.TS# = B.TS# ORDER BY B.NAME;
 
 -- 2.表空间情况
SELECT B.NAME
      ,SUM(BYTES)/1024/1024
  FROM V$DATAFILE A
      ,V$TABLESPACE B
 WHERE A.TS# = B.TS#
 GROUP BY B.NAME
 ORDER BY B.NAME ASC;
 
 -- 3.已使用表空间
SELECT TABLESPACE_NAME
     ,SUM(BYTES)/1024/1024
  FROM DBA_SEGMENTS
 GROUP BY TABLESPACE_NAME
 ORDER BY TABLESPACE_NAME ASC;
 
 -- 4.剩余表空间
 SELECT TABLESPACE_NAME
       ,SUM(BYTES)/1024/1024 
  FROM DBA_FREE_SPACE 
  GROUP BY TABLESPACE_NAME 
  ORDER BY TABLESPACE_NAME;

-- 5.查看占用比例
SELECT D.TABLESPACE_NAME
       ,SPACE "SUM_SPACE(M)"
       ,BLOCKS SUM_BLOCKS
       ,SPACE - NVL(FREE_SPACE, 0) "USED_SPACE(M)"
       ,ROUND((1 - NVL(FREE_SPACE, 0) / SPACE) * 100, 2) "USED_RATE(%)"
       ,FREE_SPACE "FREE_SPACE(M)"
  FROM (SELECT TABLESPACE_NAME
               ,ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE
               ,SUM(BLOCKS) BLOCKS
          FROM DBA_DATA_FILES
         GROUP BY TABLESPACE_NAME) D,
       (SELECT TABLESPACE_NAME
               ,ROUND(SUM(BYTES) / (1024 * 1024), 2) FREE_SPACE
          FROM DBA_FREE_SPACE
         GROUP BY TABLESPACE_NAME) F
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
UNION ALL --IF HAVE TEMPFILE 
SELECT D.TABLESPACE_NAME,
SPACE "SUM_SPACE(M)"
       ,BLOCKS SUM_BLOCKS
       ,USED_SPACE "USED_SPACE(M)"
       ,ROUND(NVL(USED_SPACE, 0) / SPACE * 100, 2) "USED_RATE(%)"
       ,NVL(FREE_SPACE, 0) "FREE_SPACE(M)"
  FROM (SELECT TABLESPACE_NAME
               ,ROUND(SUM(BYTES) / (1024 * 1024), 2) SPACE
               ,SUM(BLOCKS) BLOCKS
          FROM DBA_TEMP_FILES
         GROUP BY TABLESPACE_NAME) D,
       (SELECT TABLESPACE_NAME
               ,ROUND(SUM(BYTES_USED) / (1024 * 1024), 2) USED_SPACE
               ,ROUND(SUM(BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE
          FROM V$TEMP_SPACE_HEADER
         GROUP BY TABLESPACE_NAME) F
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  
 
-- 6.修改表空间自动扩展
ALTER DATABASE DATAFILE 'E:\ORACLEDATA\LUOC_DATA.DBF' AUTOEXTEND ON; 

-- 7.未指定默认表空间而创建用户,则系统应该是指定其默认为system表空间
SELECT USERNAME
      ,DEFAULT_TABLESPACE
      ,TEMPORARY_TABLESPACE 
  FROM DBA_USERS 
  WHERE USERNAME = 'QAOE';  

你可能感兴趣的:(Oracle数据库查看表空间以及修改小空间自动扩展)