主頁(yè) > 知識(shí)庫(kù) > Oracle 表空間查詢與操作方法

Oracle 表空間查詢與操作方法

熱門標(biāo)簽:外呼系統(tǒng)口號(hào) 百度地圖標(biāo)注員是干什么 外呼系統(tǒng)鄭州 北京外呼系統(tǒng)公司排名 地圖標(biāo)注付款了怎么找不到了 溫州人工外呼系統(tǒng) 貴陽(yáng)智能電銷機(jī)器人官網(wǎng) 沈陽(yáng)400電話是如何辦理 北京營(yíng)銷外呼系統(tǒng)廠家
一。查詢篇
1.查詢oracle表空間的使用情況
select b.file_id  文件ID,
  b.tablespace_name  表空間,
  b.file_name     物理文件名,
  b.bytes       總字節(jié)數(shù),
  (b.bytes-sum(nvl(a.bytes,0)))   已使用,
  sum(nvl(a.bytes,0))        剩余,
  sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  from dba_free_space a,dba_data_files b
  where a.file_id=b.file_id
  group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  order by b.tablespace_name
2.查詢oracle系統(tǒng)用戶的默認(rèn)表空間和臨時(shí)表空間
select default_tablespace,temporary_tablespace from dba_users
3.查詢單張表的使用情況
select segment_name,bytes from dba_segments where segment_name = 'RE_STDEVT_FACT_DAY' and owner = USER
RE_STDEVT_FACT_DAY是您要查詢的表名稱
4.查詢所有用戶表使用大小的前三十名
select * from (select segment_name,bytes from dba_segments where owner = USER order by bytes desc ) where rownum = 30
5.查詢當(dāng)前用戶默認(rèn)表空間的使用情況
select tablespacename,sum(totalContent),sum(usecontent),sum(sparecontent),avg(sparepercent)
from
(
SELECT b.file_id as id,b.tablespace_name as tablespacename,b.bytes as totalContent,(b.bytes-sum(nvl(a.bytes,0))) as usecontent,sum(nvl(a.bytes,0)) as sparecontent,sum(nvl(a.bytes,0))/(b.bytes)*100 as sparepercent
FROM dba_free_space a,dba_data_files b
WHERE a.file_id=b.file_id and b.tablespace_name = (select default_tablespace from dba_users where username = user)
group by b.tablespace_name,b.file_name,b.file_id,b.bytes
)
GROUP BY tablespacename
6.查詢用戶表空間的表
select * from user_tables
==================================================================================
一、建立表空間
CREATE TABLESPACE test
DATAFILE 'c:/oracle/oradata/db/test01.dbf' SIZE 50M
UNIFORM SIZE 1M; #指定區(qū)尺寸為128k,如不指定,區(qū)尺寸默認(rèn)為64k

CREATE TABLESPACE test
DATAFILE 'c:/oracle/oradata/db/test01.dbf' SIZE 50M
MINIMUM EXTENT 50K EXTENT MANAGEMENT LOCAL
DEFAULT STORAGE (INITIAL 50K NEXT 50K MAXEXTENTS 100 PCTINCREASE 0);
可從dba_tablespaces中查看剛創(chuàng)建的表空間的信息
二、建立UNDO表空間
CREATE UNDO TABLESPACE test_undo
DATAFILE 'c:/oracle/oradata/db/test_undo.dbf' SIZE 50M
UNDO表空間的EXTENT是由本地管理的,而且在創(chuàng)建時(shí)的SQL語(yǔ)句中只能使用DATAFILE和EXTENT MANAGEMENT子句。
ORACLE規(guī)定在任何時(shí)刻只能將一個(gè)還原表空間賦予數(shù)據(jù)庫(kù),即在一個(gè)實(shí)例中可以有多個(gè)還原表空間存在,但只能有一個(gè)為活動(dòng)的??梢允褂肁LTER SYSTEM命令進(jìn)行還原表空間的切換。
SQL> ALTER SYSTEM SET UNDO_TABLESPACE = test_undo;
三、建立臨時(shí)表空間
CREATE TEMPORARY TABLESPACE test_temp
TEMPFILE '/oracle/oradata/db/test_temp.dbf' SIZE 50M
查看系統(tǒng)當(dāng)前默認(rèn)的臨時(shí)表空間
select * from dba_properties where property_name like 'DEFAULT%'
改變系統(tǒng)默認(rèn)臨時(shí)表空間
alter database default temporary tablespace test_temp;
四、改變表空間狀態(tài)
1.使表空間脫機(jī)
ALTER TABLESPACE test OFFLINE;
如果是意外刪除了數(shù)據(jù)文件,則必須帶有RECOVER選項(xiàng)
ALTER TABLESPACE game test FOR RECOVER;
2.使表空間聯(lián)機(jī)
ALTER TABLESPACE test ONLINE;
3.使數(shù)據(jù)文件脫機(jī)
ALTER DATABASE DATAFILE 3 OFFLINE;
4.使數(shù)據(jù)文件聯(lián)機(jī)
ALTER DATABASE DATAFILE 3 ONLINE;
5.使表空間只讀
ALTER TABLESPACE test READ ONLY;
6.使表空間可讀寫
ALTER TABLESPACE test READ WRITE;
五、刪除表空間
DROP TABLESPACE test INCL ING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;
DROP TABLESPACE 表空間名 [INCL ING CONTENTS [AND DATAFILES] [CASCADE CONSTRAINTS]]
1. INCL ING CONTENTS 子句用來(lái)刪除段
2. AND DATAFILES 子句用來(lái)刪除數(shù)據(jù)文件
3. CASCADE CONSTRAINTS 子句用來(lái)刪除所有的引用完整性約束

六、擴(kuò)展表空間
首先查看表空間的名字和所屬文件
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
1.增加數(shù)據(jù)文件
ALTER TABLESPACE test
ADD DATAFILE '/oracle/oradata/db/test02.dbf' SIZE 1000M;
2.手動(dòng)增加數(shù)據(jù)文件尺寸
ALTER DATABASE DATAFILE 'c:/oracle/oradata/db/test01.dbf'
RESIZE 100M;
3.設(shè)定數(shù)據(jù)文件自動(dòng)擴(kuò)展
ALTER DATABASE DATAFILE 'c:/oracle/oradata/db/test01.dbf'
AUTOEXTEND ON NEXT 100M
MAXSIZE 200M;
設(shè)定后可從dba_tablespace中查看表空間信息,從v$datafile中查看對(duì)應(yīng)的數(shù)據(jù)文件信息
==================================================================================
create tablespace scgl
datafile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\scgl2.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
create tablespace test_data
logging
datafile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\user_data.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
create user scgl identified by qwer1234
default tablespace scgl
temporary tablespace scgl_temp;
tempfile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\user_temp.dbf'
create temporary tablespace scgl_temp
tempfile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\scgl_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
grant connect,resource, dba to scgl;
oracle創(chuàng)建表空間 SYS用戶在CMD下以DBA身份登陸:
在CMD中打sqlplus /nolog
然后再
conn / as sysdba
//創(chuàng)建臨時(shí)表空間
create temporary tablespace user_temp
tempfile 'D:\oracle\oradata\Oracle9i\user_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
//創(chuàng)建數(shù)據(jù)表空間
create tablespace test_data
logging
datafile 'D:\oracle\oradata\Oracle9i\user_data.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
//創(chuàng)建用戶并指定表空間
create user username identified by password
default tablespace user_data
temporary tablespace user_temp;
查詢表空間使用情況
SELECT UPPER(F.TABLESPACE_NAME) "表空間名",
D.TOT_GROOTTE_MB "表空間大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
F.TOTAL_BYTES "空閑空間(M)",
F.MAX_BYTES "最大塊(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
查詢表空間的free space
select tablespace_name,
count(*) as extends,
round(sum(bytes) / 1024 / 1024, 2) as MB,
sum(blocks) as blocks
from dba_free_space
group by tablespace_name;
--查詢表空間的總?cè)萘?
select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;
查詢表空間使用率
select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB / total.MB) * 100, 2) || '%' as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;
-----------------------------------------------------------------------------------------------------------------------------
1.建立表空間:create tablespace test datafile '/u01/test.dbf' size 10M uniform size 128k
#指定區(qū)尺寸為128k ,塊大小為默認(rèn)8K
#大文件表空間 create bigfile tablespace big_tbs datafile '/u01/big_tbs.dbf ' size 100G
2.建非標(biāo)準(zhǔn)表show parameter db alter system set db_2k_cache_size=10M create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k
#常見(jiàn)錯(cuò)誤
SQL> alter system set db_2k_cache_size=2M; alter system set db_2k_cache_size=2M ERROR at line 1: ORA-02097: parameter cannot be modified because specified value is invalid ORA-00384: Insufficient memory to grow cache
#解決
SQL> alter system set sga_max_size=400M scope=spfile; SQL> shutdown immediate; SQL> startup SQL> alter system set db_2k_cache_size=10M; System altered.
3.查看區(qū)大小與塊大小#區(qū)大小 conn y / 123 create table t(i number) tablespace test; Insert into t values(10) select bytes/1024 from user_segments where segment_name=upper('t');
#塊大小 Show parameter block(默認(rèn)64K)
#非標(biāo)準(zhǔn)表空間的blocksize SQL> select * from v$dbfile; SQL> select name,block_size,status from v$datafile; SQL> select block_size from v$datafile where file#=14;
4.刪除表空間drop tablespace test including contents and datafiles
5.查表空間:#查數(shù)據(jù)文件 select * from v$dbfile; #所有表空間 select * from v$tablespace;
#表空間的數(shù)據(jù)文件 select file_name,tablespace_name from dba_data_files;
6.建立undo表空間create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M;
#切換到新建的undo表空間 alter system set undo_tablespace=undotbs01;
7.建立臨時(shí)表空間create temporary tablespace temp_data tempfile '/u01/temp.db' size 5M; create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db' size 5M;
8.改變表空間狀態(tài)
(0.)查看狀態(tài)
#表空間狀態(tài) select tablespace_name,block_size,status from dba_tablespaces;
#數(shù)據(jù)文件狀態(tài) select name,block_size,status from v$datafile;
(1.)表空間脫機(jī)alter tablespace test offline
#如果意外刪除了數(shù)據(jù)文件 alter tablespace test offline for recover
(2.)表空間聯(lián)機(jī)alter tablespace test online
(3.)數(shù)據(jù)文件脫機(jī)select * from v$dbfile; alter database datafile 3 offline
(4.)數(shù)據(jù)文件聯(lián)機(jī)recover datafile 3; alter database datafile 3 online;
(5.)使表空間只讀alter tablespace test read only
(6.)使表空間可讀寫alter tablespace test read write;
9.擴(kuò)展表空間#首先查看表空間的名字和所屬文件及空間 select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name; #三種擴(kuò)展方法
1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(自動(dòng)加一個(gè)datafile)
2.alter database datafile '/u01/test.dbf' resize 20M;
3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;
#設(shè)定后查看表空間信息
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;
10.移動(dòng)表空間的數(shù)據(jù)文件
#先確定數(shù)據(jù)文件據(jù)在表空間
SQL>select tablespace_name,file_name from dba_data_files where file_name='/u01/test.dbf';
#open狀態(tài)
SQL>alter tablespace test offline; SQL>host move /u01/test.dbf /u01/oracle/test.dbf; SQL>alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf'; SQL>alter tablespace test offline;
#mount狀態(tài) SQL>shutdown immediate; SQL>startup mount SQL>host move /u01/test.dbf /u01/oracle/test.dbf; SQL>alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';
11.表空間和數(shù)據(jù)文件常用的數(shù)據(jù)字典與動(dòng)態(tài)性能視圖v$dbfile v$datafile dba_segments user_segments dba_data_files v$tablespace dba_tablespaces user_tablespaces
--查詢表空間使用情況
SELECT UPPER(F.TABLESPACE_NAME) "表空間名",
D.TOT_GROOTTE_MB "表空間大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
F.TOTAL_BYTES "空閑空間(M)",
F.MAX_BYTES "最大塊(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
--查詢表空間的free space
select tablespace_name,
count(*) as extends,
round(sum(bytes) / 1024 / 1024, 2) as MB,
sum(blocks) as blocks
from dba_free_space
group by tablespace_name;
--查詢表空間的總?cè)萘?
select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;
--查詢表空間使用率
select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB / total.MB) * 100, 2) || '%' as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;
//給用戶授予權(quán)限
grant connect,resource to username;
//以后以該用戶登錄,創(chuàng)建的任何數(shù)據(jù)庫(kù)對(duì)象都屬于user_temp 和user_data表空間,
這就不用在每創(chuàng)建一個(gè)對(duì)象給其指定表空間了
撤權(quán):
revoke 權(quán)限... from 用戶名;
刪除用戶命令
drop user user_name cascade;

建立表空間
CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' SIZE 500M
UNIFORM SIZE 128k; #指定區(qū)尺寸為128k,如不指定,區(qū)尺寸默認(rèn)為64k

刪除表空間
DROP TABLESPACE data01 INCL ING CONTENTS AND DATAFILES;
一、建立表空間
CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' SIZE 500M
UNIFORM SIZE 128k; #指定區(qū)尺寸為128k,如不指定,區(qū)尺寸默認(rèn)為64k
二、建立UNDO表空間
CREATE UNDO TABLESPACE UNDOTBS02
DATAFILE '/oracle/oradata/db/UNDOTBS02.dbf' SIZE 50M
#注意:在OPEN狀態(tài)下某些時(shí)刻只能用一個(gè)UNDO表空間,如果要用新建的表空間,必須切換到該表空間:
ALTER SYSTEM SET undo_tablespace=UNDOTBS02;
三、建立臨時(shí)表空間
CREATE TEMPORARY TABLESPACE temp_data
TEMPFILE '/oracle/oradata/db/TEMP_DATA.dbf' SIZE 50M
四、改變表空間狀態(tài)
1.使表空間脫機(jī)
ALTER TABLESPACE game OFFLINE;
如果是意外刪除了數(shù)據(jù)文件,則必須帶有RECOVER選項(xiàng)
ALTER TABLESPACE game OFFLINE FOR RECOVER;
2.使表空間聯(lián)機(jī)
ALTER TABLESPACE game ONLINE;
3.使數(shù)據(jù)文件脫機(jī)
ALTER DATABASE DATAFILE 3 OFFLINE;
4.使數(shù)據(jù)文件聯(lián)機(jī)
ALTER DATABASE DATAFILE 3 ONLINE;
5.使表空間只讀
ALTER TABLESPACE game READ ONLY;
6.使表空間可讀寫
ALTER TABLESPACE game READ WRITE;
五、刪除表空間
DROP TABLESPACE data01 INCL ING CONTENTS AND DATAFILES;

六、擴(kuò)展表空間
首先查看表空間的名字和所屬文件
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
1.增加數(shù)據(jù)文件
ALTER TABLESPACE game
ADD DATAFILE '/oracle/oradata/db/GAME02.dbf' SIZE 1000M;
2.手動(dòng)增加數(shù)據(jù)文件尺寸
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf'
RESIZE 4000M;
3.設(shè)定數(shù)據(jù)文件自動(dòng)擴(kuò)展
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf
AUTOEXTEND ON NEXT 100M
MAXSIZE 10000M;

設(shè)定后查看表空間信息
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
您可能感興趣的文章:
  • oracle 查詢表名以及表的列名
  • oracle表空間表分區(qū)詳解及oracle表分區(qū)查詢使用方法
  • oracle查詢語(yǔ)句大全(oracle 基本命令大全一)
  • oracle數(shù)據(jù)庫(kù)常用的99條查詢語(yǔ)句
  • ORACLE查詢刪除重復(fù)記錄三種方法
  • oracle常用sql查詢語(yǔ)句部分集合(圖文)
  • ORACLE查詢表最近更改數(shù)據(jù)的方法
  • Oracle 查詢表信息獲取表字段及字段注釋
  • Oracle 數(shù)據(jù)庫(kù)連接查詢SQL語(yǔ)句
  • Oracle查詢表里的重復(fù)數(shù)據(jù)方法
  • oracle基本查詢用法入門示例

標(biāo)簽:潮州 包頭 衢州 溫州 通遼 定西 淮北 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle 表空間查詢與操作方法》,本文關(guān)鍵詞  Oracle,表,空間,查詢,與,操作,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Oracle 表空間查詢與操作方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Oracle 表空間查詢與操作方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章