黄金年代。查询篇 1.查询oracle表空间的选拔处境 select b.file_id 文件ID,
b.tablespace_name 表空间, b.file_name 物理文件名, b.bytes 总字节数,
(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系统顾客的默许表空间和一时表空间 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 = USEKuga order by bytes
desc 卡塔尔(قطر‎ where rownum = 30 5.询问当前顾客暗中认可表空间的采用处境 select
tablespacename,sum(totalContent卡塔尔(英语:State of Qatar),sum(usecontent卡塔尔,sum(sparecontent卡塔尔(英语:State of Qatar),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

–查询表空间应用状态
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;
— 查看表空间对象占用意况
Select Segment_Name “段名”,
       Segment_Type “段类型”,
       TableSpace_Name “表空间”,
       Sum(bytes卡塔尔(英语:State of Qatar) / 1024 / 1024 “空间侵吞(MB卡塔尔”
  From User_Extents
 Group By Segment_Type, Segment_Name, TableSpace_Name
 order by Sum(bytes) / 1024 / 1024 desc;

tablespacename 6.询问客商表空间的表 select * from user_tables

风姿浪漫、建构表空间 CREATE TABLESPACE test DATAFILE
‘c:/oracle/oradata/db/test01.dbf’ SIZE 50M UNIFORM SIZE 1M;
#钦命区尺寸为128k,如不钦赐,区尺寸默感到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中查阅刚成立的表空间的音讯
二、建立UNDO表空间 CREATE UNDO TABLESPACE test_undo DATAFILE
‘c:/oracle/oradata/db/test_undo.dbf’ SIZE 50M
UNDO表空间的EXTENT是由本土管理的,而且在创制时的SQL语句中一定要动用DATAFILE和EXTENT
MANAGEMENT子句。
ORACLE规定在任何时刻只好将三个还原表空间授予数据库,即在叁个实例中得以有七个还原表空间存在,但只好有二个为运动的。能够行使ALTE宝马X5SYSTEM命令举行还原表空间的切换。 SQL ALTEOdyssey SYSTEM SET UNDO_TABLESPACE =
test_undo; 三、创建一时表空间 CREATE TEMPORARY TABLESPACE
test_temp TEMPFILE ‘/oracle/oradata/db/test_temp.dbf’ SIZE 50M
查看系统当下暗许的不常表空间 select * from dba_properties where
property_name like ‘DEFAULT%’ 更动系统暗中同意有的时候表空间 alter database
default temporary tablespace test_temp; 四、改动表空间状态
1.使表空间脱机 ALTEENVISION TABLESPACE test OFFLINE;
纵然是离奇删除了数据文件,则必须含有RECOVE奇骏选项 ALTERAV4 TABLESPACE game
test FOLacrosse RECOVE酷威; 2.使表空间联机 ALTE瑞虎 TABLESPACE test ONLINE;
3.使数据文件脱机 ALTE冠道 DATABASE DATAFILE 3 OFFLINE; 4.使数据文件联机
ALTETiggo DATABASE DATAFILE 3 ONLINE; 5.使表空间只读 ALTEPAJERO TABLESPACE test
READ ONLY; 6.使表空间可读写 ALTEQX56 TABLESPACE test READ W智跑ITE;
五、删除表空间 DROP TABLESPACE test INCL ING CONTENTS AND DATAFILES
CASCADE CONSTRAINTS; DROP TABLESPACE 表空间名 [INCL ING CONTENTS [AND
DATAFILES] [CASCADE CONSTRAINTS]] 1. INCL ING CONTENTS
子句用来删除段 2. AND DATAFILES 子句用来删除数据文件 3. CASCADE
CONSTRAINTS 子句用来删除全体的引用完整性节制 六、扩大表空间
首先查看表空间的名字和所属文件 select tablespace_name, file_id,
file_name, round(bytes/(1024*1024),0) total_space from
dba_data_files order by tablespace_name; 1.日增数据文件 ALTER
TABLESPACE test ADD DATAFILE ‘/oracle/oradata/db/test02.dbf’ SIZE 1000M;
2.手动扩大数据文件尺寸 ALTEENVISION DATABASE DATAFILE
‘c:/oracle/oradata/db/test01.dbf’ RESIZE 100M; 3.设定数据文件自动扩大ALTE揽胜极光 DATABASE DATAFILE ‘c:/oracle/oradata/db/test01.dbf’ AUTOEXTEND ON
NEXT 100M MAXSIZE 200M;

–查询表空间的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;

设定后可从dba_tablespace中查看表空间音讯,从v$datafile中查阅相应的数据文件音信

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成立表空间 SYS客户在CMD下以DBA身份登录: 在CMD中打sqlplus
/nolog 然后再 conn / as sysdba //创制一时表空间 create temporary
tablespace user_temp tempfile
‘D:/oracle/oradata/Oracle9i/user_temp.dbf’ size 50m autoextend on next
50m maxsize 20480m extent management local; //创设数据表空间 create
tablespace test_data logging datafile
‘D:/oracle/oradata/Oracle9i/user_data.dbf’ size 50m autoextend on next
50m maxsize 20480m extent management local; //创设客商并钦赐表空间
create user username identified by password default tablespace
user_data temporary tablespace user_temp; 查询表空间应用情状 SELECT
UPPE奥迪Q3(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
O奥迪Q5DE奥迪Q3 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; –查询表空间的总体量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 #钦定区尺寸为128k ,块大小为私下认可8K #大文件表空间
create bigfile tablespace big_tbs datafile ‘/u01/big_tbs.dbf ‘ size
100G 2.建非标准化准表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 #周边错误 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.查看区大小与块大小#区大小 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(默认64K) #非标准表空间的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.查表空间:#查数据文件 select *
from v$dbfile; #抱有表空间 select * from v$tablespace;
#表空间的数据文件 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.创造有时表空间create temporary
tablespace temp_data tempfile ‘/u01/temp.db’ size 5M; create bigfile
temporary tablespace bigtem tempfile ‘/u01/bigtemp.db’ size 5M;
8.退换表空间状态 (0.卡塔尔(英语:State of Qatar)查看情况 #表空间状态 select
tablespace_name,block_size,status from dba_tablespaces;
#数据文件状态 select name,block_size,status from v$datafile;
(1.卡塔尔(قطر‎表空间脱机alter tablespace test offline #譬喻意外删除了数据文件
alter tablespace test offline for recover (2.卡塔尔表空间联机alter tablespace
test online (3.卡塔尔(英语:State of Qatar)数据文件脱机select * from v$dbfile; alter database
datafile 3 offline (4.卡塔尔数据文件联机recover datafile 3; alter database
datafile 3 online; (5.卡塔尔(英语:State of Qatar)使表空间只读alter tablespace test read only
(6.卡塔尔使表空间可读写alter tablespace test read write;
9.扩充表空间#率先查看表空间的名字和所属文件及空间 select
tablespace_name, file_id, file_name,round(bytes/(1024*1024),0)
total_space from dba_data_files order by tablespace_name;
#两种扩展方法 1.alter tablespace test add datafile ‘/u01/test02.dbf’
size 10M(自动加一个datafile卡塔尔 2.alter database datafile ‘/u01/test.dbf’
resize 20M; 3.alter database datafile ‘/u01/test.dbf’ autoextend on next
10M maxsize 1G; #设定后查看表空间新闻 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.移动表空间的数据文件
#先鲜明数据文件据在表空间 SQLselect tablespace_name,file_name from
dba_data_files where file_name=’/u01/test.dbf’; #open状态 SQLalter
tablespace test offline; SQLhost move /u01/test.dbf
/u01/oracle/test.dbf; SQLalter tablespace test rename datafile
‘/u01/test.dbf’ to ‘/u01/oracle/test.dbf’; SQLalter tablespace test
offline; #mount状态 SQLshutdown immediate; SQLstartup mount SQLhost
move /u01/test.dbf /u01/oracle/test.dbf; SQLalter database rename file
‘/u01/test.dbf’ to ‘/u01/oracle/test.dbf’;
11.表空仲春数据文件常用的数据字典与动态品质视图v$dbfile v$datafile
dba_segments user_segments dba_data_files v$tablespace
dba_tablespaces user_tablespaces –查询表空间应用情状 SELECT
UPPERubicon(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
O牧马人DE卡宴 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;
–查询表空间的总体积 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; //给顾客赋予权限
grant connect,resource to username;
//未来以该顾客登陆,创立的其他数据库对象都归于user_temp
和user_data表空间, 那就毫无在每创造三个目的给其钦点表空间了 撤权:
revoke 权限… from 客商名; 删除顾客命令 drop user user_name cascade;
创立表空间 CREATE TABLESPACE data01 DATAFILE
‘/oracle/oradata/db/DATA01.dbf’ SIZE 500M UNIFORM SIZE 128k;
#钦赐区尺寸为128k,如不钦命,区尺寸默感觉64k 剔除表空间 DROP
TABLESPACE data01 INCL ING CONTENTS AND DATAFILES; 生龙活虎、创立表空间 CREATE
TABLESPACE data01 DATAFILE ‘/oracle/oradata/db/DATA01.dbf’ SIZE 500M
UNIFORM SIZE 128k; #钦命区尺寸为128k,如不钦点,区尺寸默以为64k
二、建设布局UNDO表空间 CREATE UNDO TABLESPACE UNDOTBS02 DATAFILE
‘/oracle/oradata/db/UNDOTBS02.dbf’ SIZE 50M
#留意:在OPEN状态下一些时刻只可以用一个UNDO表空间,借使要用新建的表空间,必需切换成该表空间:
ALTEHaval SYSTEM SET undo_tablespace=UNDOTBS02; 三、建构一时表空间 CREATE
TEMPORA安德拉Y TABLESPACE temp_data TEMPFILE
‘/oracle/oradata/db/TEMP_DATA.dbf’ SIZE 50M 四、退换表空间状态
1.使表空间脱机 ALTEHighlander TABLESPACE game OFFLINE;
假使是想获得删除了数据文件,则必需包涵RECOVE中华V选项 ALTE奥迪Q5 TABLESPACE game
OFFLINE FOLAND RECOVEENVISION; 2.使表空间联机 ALTE揽胜极光 TABLESPACE game ONLINE;
3.使数据文件脱机 ALTE本田UR-V DATABASE DATAFILE 3 OFFLINE; 4.使数据文件联机
ALTEPRADO DATABASE DATAFILE 3 ONLINE; 5.使表空间只读 ALTECR-V TABLESPACE game
READ ONLY; 6.使表空间可读写 ALTECR-V TABLESPACE game READ WTiggoITE;
五、删除表空间 DROP TABLESPACE data01 INCL ING CONTENTS AND DATAFILES;
六、扩张表空间 首先查看表空间的名字和所属文件 select tablespace_name,
file_id, file_name, round(bytes/(1024*1024),0) total_space from
dba_data_files order by tablespace_name; 1.扩张数据文件 ALTER
TABLESPACE game ADD DATAFILE ‘/oracle/oradata/db/GAME02.dbf’ SIZE 1000M;
2.手动扩充数据文件尺寸 ALTEKoleos DATABASE DATAFILE
‘/oracle/oradata/db/GAME.dbf’ RESIZE 4000M; 3.设定数据文件自动扩展 ALTE奇骏DATABASE DATAFILE ‘/oracle/oradata/db/GAME.dbf AUTOEXTEND ON NEXT 100M
MAXSIZE 10000M; 设定后查看表空间音讯 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

–查询表空间的总体积
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;
— 查看表空间应用情形
select sum(bytes/1024/1024) sizeMB from dba_free_space z where
z.tablespace_name=’DATA6′;
— 查看索引表空间侵吞
select s.index_name,
       s.table_name,
       s.tablespace_name,
       s.initial_extent,
       s.next_extent
  from user_indexes s
 where s.index_name = ‘PK_LOG_INTER_CALLED_DETAIL’;
— 查看表空间文件
select file_name,tablespace_name,bytes/1024/1024 from dba_data_files
where tablespace_name=’USERS’;
— 查看表空间剩余
select sum(bytes/1024/1024) sizeMB from dba_free_space z where
z.tablespace_name=’USERS’;
— 校勘表空间文件大小
alter database datafile ‘/home/oracle/oradata/easyweb/data3_01.dbf’
resize 10000M MAXSIZE UNLIMITED;
alter database datafile ‘/home/oracle/oradata/easyweb/data6_01.dbf’
AUTOEXTEND ON NEXT 200M MAXSIZE UNLIMITED;
— 查看全体表空间文件
SELECT FILE_NAME,TABLESPACE_NAME,AUTOEXTENSIBLE FROM
dba_data_files;
— 设置表空间活动扩展
ALTEHighlander DATABASE DATAFILE ‘/data/oradata/easyweb/users01.dbf’ AUTOEXTEND
ON; –展开自动增加
ALTE奥迪Q5 DATABASE DATAFILE ‘/data/oradata/easyweb/users01.dbf’ AUTOEXTEND
ON NEXT 200M ; –每一趟活动拉长200m
ALTE奥迪Q7 DATABASE DATAFILE ‘/home/oracle/oradata/easyweb/users01.dbf’
AUTOEXTEND ON NEXT 200M MAXSIZE 10000M;
–每回活动增进200m,数据表最大不当先1G
–MAXSIZE UNLIMITED;

–一个表空间最大为32g,当先32g索要充实,比方:
alter tablespace ZSK ADD  datafile ‘/mc_data/oradata/zsk/zsk_01.dbf’
SIZE 30G;

相关文章