oracle表空间操作详解-入门基础

建立表空间
CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' SIZE 500M
UNIFORM SIZE 128k;             #指定区尺寸为128k,如不指定,区尺寸默认为64k

删除表空间
DROP TABLESPACE data01 INCLUDING CONTENTS AND DATAFILES;

修改表空间大小
alter database datafile '/path/NADDate05.dbf' resize 100M               

移动表至另一表空间
alter table move tablespace room1;

一、建立表空间
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表空间,如果要用新建的表空间,必须切换到该表空间:

ALTER SYSTEM SET undo_tablespace=UNDOTBS02;

三、建立临时表空间
CREATE TEMPORARY TABLESPACE temp_data
TEMPFILE '/oracle/oradata/db/TEMP_DATA.dbf' SIZE 50M

四、改变表空间状态

1.使表空间脱机
ALTER TABLESPACE game OFFLINE;
如果是意外删除了数据文件,则必须带有RECOVER选项
ALTER TABLESPACE game OFFLINE FOR RECOVER;

2.使表空间联机
ALTER TABLESPACE game ONLINE;

3.使数据文件脱机
ALTER DATABASE DATAFILE 3 OFFLINE;

4.使数据文件联机
ALTER DATABASE DATAFILE 3 ONLINE;

5.使表空间只读
ALTER TABLESPACE game READ ONLY;

6.使表空间可读写
ALTER TABLESPACE game READ WRITE;

五、删除表空间
DROP TABLESPACE data01 INCLUDING 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.手动增加数据文件尺寸
  ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf'
  RESIZE 4000M;

3.设定数据文件自动扩展
  ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf'
  AUTOEXTEND ON NEXT 100M
  MAXSIZE 10000M;

4.设定后查看表空间信息
  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;

 

 

5.ORACLE表空间的备份与恢复方法

       表空间备份与恢复主要针对于大型数据库中,某个表空间数据变化非常大,增长速度非常快的情况。表空间的备份实现脚本:
run {
allocate channel d1 type disk;
backup tablespace "TEST","USERS" format ’d:/backup/tb_%d_%s_%p_%t’;
release channel d1;
}
 
       恢复时,如果用户是要恢复被删除的表空间中的表或视图,或者是使用者用drop tablespace正常命令删除了表空间,此时控制文件中记录的数据库结构也跟着做了改变,此时只能用不完全恢复,让数据库恢复到以前的一个时间点或SCN。先恢复控制文件,再恢复表空间。

Shutdown abort;
Startup nomount;
run {
Allocate channel d1 type disk;
Restore controlfile from ‘d:/backup/CTL_TEST_0_1_6555’;
Release channel d1 ;
Alter database open; //要为打开状态
 
set until time "TO_DATE(’08/08/2007 10:50:00’,’MM/DD/YYYY HH24:MI:SS’)";
sql ’alter tablespace TEST, USERS offline immediate’;
allocate channel d1 type disk;
restore tablespace  TEST, USERS;
recover tablespace  TEST, USERS;
release channel d1;
sql ’alter tablespace TEST, USERS online’;
}

注意:After you perform TSPITR on a tablespace, you cannot use backups of that tablespace
from before the TSPITR was completed and the tablespace put back on line. If you
start using the recovered tablespaces without taking a backup, you are running your
database without a usable backup of those tablespaces. For this example, the users
and tools tablespaces must be backed up, as follows:
RMAN> BACKUP TABLESPACE users, tools;
You can then safely bring the tablespaces online, as follows:
RMAN> SQL "ALTER TABLESPACE users, tools ONLINE";
Your recovered tablespaces are now ready for use.

没有更多推荐了,返回首页