表空间和数据文件
Oracle 在逻辑上以表空间存储数据,而实际上以数据文件进行存储。
表空间:
– 某一时刻只能属于一个数据库
– 由一个或多个数据文件组成
– 可进一步划分为逻辑存储单元
数据文件:
– 只能属于一个表空间和一个数据库
– 是方案对象数据的资料档案库
表空间类型
SYSTEM 表空间
– 随数据库创建
– 包含数据字典
– 包含SYSTEM 还原段
非SYSTEM 表空间
– 用于分开存储段
– 易于空间管理
– 控制分配给用户的空间量
获取表空间信息
通过以下查询可获取表空间和数据文件的信息:
? 表空间:
– DBA_TABLESPACES
– V$TABLESPACE
? 数据文件信息:
– DBA_DATA_FILES
– V$DATAFILE
? 临时文件信息:
– DBA_TEMP_FILES
– V$TEMPFILE
创建表空间
create tablespace tablespace—name
[datafile clause]
[minimum extent integer[k|m]]
[blocksize integer [k]]
[logging|nologging]
[default storage_clause ]
[online|offline]
[permanent|temporary]
[extent_management_clause]
[segment_management_clause]
desc v$datafile
select name from v$datafile; 查看数据文件存放目录
创建uplooing表空间大小100M
create tablespace uplooking datafile '/opt/app/oracle/oradata/sytong1/uplooking01.dbf' size 100M;
查看数据文件
col file_name for a45;
select tablespace_name,file_name,AUTOEXTENSIBLE from dba_data_files;
TABLESPACE_NAME FILE_NAME AUT
------------------------------ --------------------------------------------- ---
USERS /opt/app/oracle/oradata/sytong1/users01.dbf YES
SYSAUX /opt/app/oracle/oradata/sytong1/sysaux01.dbf YES
UNDOTBS1 /opt/app/oracle/oradata/sytong1/undotbs01.dbf YES
SYSTEM /opt/app/oracle/oradata/sytong1/system01.dbf YES
UPLOOKING /opt/app/oracle/oradata/sytong1/uplooking01.dbf NO
修改uplooking大小为110M,
alter database datafile '/opt/app/oracle/oradata/sytong1/uplooking01.dbf' resize 120M;
aut改为on
alter database datafile '/opt/app/oracle/oradata/sytong1/uplooking01.dbf' autoextend on;
alter user sys default tablespace uplooking;
alter database datafile '/opt/app/oracle/oradata/sytong1/uplooking01.bdf'
查看undo 表空间
show parameter undo;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
SQL> select name from v$rollname;
NAME
------------------------------
SYSTEM
_SYSSMU1$
_SYSSMU2$
_SYSSMU3$
_SYSSMU4$
_SYSSMU5$
_SYSSMU6$
_SYSSMU7$
_SYSSMU8$
_SYSSMU9$
_SYSSMU10$
11 rows selected.
创建undo1表空间
create undo tablespace undo1 datafile '/opt/app/oracle/oradata/sytong1/undo01.dbf' size 40M;
切换undo表空间
alter system set undo_tablespace=undo1;
desc v$undostat;
show parameter undo;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDO1
查看回滚
select name from v$rollname;
NAME
------------------------------
SYSTEM
_SYSSMU11$
_SYSSMU12$
_SYSSMU13$
_SYSSMU14$
_SYSSMU15$
_SYSSMU16$
_SYSSMU17$
_SYSSMU18$
_SYSSMU19$
_SYSSMU20$
11 rows selected.
查询临时表空间
select tablespace_name,file_name from dba_temp_files;
TABLESPACE_NAME FILE_NAME
------------------------------ ---------------------------------------------
TEMP /opt/app/oracle/oradata/sytong1/temp01.dbf
set autot on exp
select * from scott.dba1;
查询scott临时表空间
select username,default_tablespace,temporary_tablespace from dba_users where username='SCOTT';
USERNAME DEFAULT_TABLESPACE TEMPORARY_TABLESPACE
SCOTT USERS TEMP
修改用户临时表空间,尽量不要用系统临时表空间
alter user scott temporary tablespace uplooking;
改为只读P164
alter tablespace uplooking read only;
select * from user_recyclebin; 查看闪回区
查看是否脱机,offline
select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME STATUS
------------------------------ ---------
SYSTEM ONLINE
UNDOTBS1 ONLINE
SYSAUX ONLINE
TEMP ONLINE
USERS ONLINE
UPLOOKING ONLINE
UNDO1 ONLINE
使uplooking脱机
alter tablespace uplooking offline;
创建表空间,初始50m每一次扩展10M最大999M
create tablespace test003 datafile '/home/oracle/test03.dbf' size 10M default storage(initial 50M next 10M maxextents 999);
把aut改为yes,必须要在online状态下才能修改
alter database datafile '/home/oracle/test03.dbf' autoextend on;
查看是否改成功
select tablespace_name,file_name,autoextensible from dba_data_files;
TABLESPACE_NAME FILE_NAME AUT
TEST003 /home/oracle/test03.dbf YES
UNDO1 /opt/app/oracle/oradata/sytong1/undo01.dbf NO
移动数据文件
移动dbf文件,先要offline,才能移动,把/home/oracle目录下的表空间移到默认目录(或相反),然后改名、online
查看表空间
select file_id,tablespace_name,file_name,AUTOEXTENSIBLE from dba_data_files;
查看状态
select tablespace_name,status from dba_tablespaces;
修改
alter database datafile test003 offline;
移动
mv -v /home/oracel/test03.dbf /opt/app/oracle/oradata/sytong1/
改名
alter database rename file '/home/oracle/test03.dbf' to '/opt/app/oracle/oradata/sytong1/test03.dbf';
online
recover datafile 7; 介质恢复7是file_id 非归档如果数据文件离开时间太久需要介质恢复
alter database datafile 7 online;
如果是系统数据文件,先关闭数据库,然后mount,移动数据文件,改名,然后open
shutdown immediate
startup mount
移动系统数据文件
mv system01.dbf ../
改名
sql>alter database rename file '/opt/app/oracle/oradata/sytong1/system01.dbf' to '/opt/app/oracle/system01.dbf'
开启数据库
alter database open;