1. 程式人生 > >linux oracle 表空間常用操作

linux oracle 表空間常用操作

1、

su - oracle

sqlplus / as sysdba

2、建立資料表空間

 create tablespace FUCK datafile '/data/oradata/ora11g/FUCK.DBF' size 7024M autoextend ON next 100M maxsize 8024M extent management local;;

  FUCK  表空間名字

 /data/oradata/ora11g/FUCK.DBF  存放資料庫檔案的地方,一般是安裝資料庫後有控制檔案,資料檔案和日誌檔案的資料夾,再加上要建立表空間的名字+dbf (資料檔案)

 7024M    表空間的初始大小

100M       表空間的自動增長大小

 8024M    表空間最大的大小

3、建立臨時表空間

 create temporary tablespace FUCK_temp tempfile '/data/oradata/ora11g/FUCK_temp.DBF' size 7024M autoextend on next 100M maxsize  8024M extent management local;
 

查看錶空間檔案位置:

select * from dba_data_files;

4、建立使用者並指定表空間

 create user FUCK identified by FUCK
default tablespace FUCK
temporary tablespace FUCK_temp;

5、給使用者授予許可權

grant connect,resource,dba to FUCK;

6、刪除表空間

 drop tablespace FUCK including contents and datafiles cascade constraints;

說明:

including contents 刪除表空間中的內容,如果刪除表空間之前表空間中有內容,而未加此引數,表空間刪不掉,所以習慣性的加此引數

including datafiles 刪除表空間中的資料檔案

cascade constraints 同時刪除 tablespace 中表的外來鍵參照

7、修改表空間為擴充套件並限制最大值

查詢當前資料庫中表空間是否為自動擴充套件

select tablespace_name,file_name,autoextensible from dba_data_files;


.通過修改表空間的資料檔案為自動擴充套件達到表空間為自動擴充套件的目的,並限制檔案最大值
alter database datafile '/opt/app/oracle/oradata/caacdb/system01.dbf' autoextend on NEXT 50M maxsize 31G;
alter database datafile '/opt/app/oracle/oradata/caacdb/undotbs01.dbf' autoextend on NEXT 50M maxsize 31G;
alter database datafile '/opt/app/oracle/oradata/caacdb/sysaux01.dbf' autoextend on NEXT 50M maxsize 31G;

alter database datafile '/opt/app/oracle/oradata/caacdb/users01.dbf' autoextend on NEXT 50M maxsize 31G;

開啟自動擴充套件功能語法:
alter database datafile '對應的資料檔案路徑資訊' autoextend on;
關閉自動擴充套件功能語法:
alter database datafile '對應的資料檔案路徑資訊' autoextend off;

8、檢視Oracle表空間大小及所剩空間大小

select f.tablespace_name,
       a.total,
       u.used,
       f.free,
       round((u.used / a.total) * 100) "% USED",
       round((f.free / a.total) * 100) "% FREE"
  from (select tablespace_name, sum(bytes / (1024 * 1024)) total
          from dba_data_files
         group by tablespace_name) a,
       (select tablespace_name, round(sum(bytes / (1024 * 1024))) used
          from dba_extents
         group by tablespace_name) u,
       (select tablespace_name, round(sum(bytes / (1024 * 1024))) free
          from dba_free_space
         group by tablespace_name) f
 where a.tablespace_name = f.tablespace_name
   and a.tablespace_name = u.tablespace_name;

下面這種方法更快:

select a.tablespace_name,
       a.bytes / 1024 / 1024 "Sum MB",
       (a.bytes - b.bytes) / 1024 / 1024 "used MB",
       b.bytes / 1024 / 1024 "free MB",
       round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "percent_used"
  from (select tablespace_name, sum(bytes) bytes
          from dba_data_files
         group by tablespace_name) a,
       (select tablespace_name, sum(bytes) bytes, max(bytes) largest
          from dba_free_space
         group by tablespace_name) b
 where a.tablespace_name = b.tablespace_name
 order by ((a.bytes - b.bytes) / a.bytes) desc;

9、Oracle 查看錶空間的大小及使用情況sql語句

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