表空间
表空间是数据库的逻辑划分,一个表空间只能属于一个数据库。所有的数据库对象都存放在指定的表空间中。但主要存放的是表, 所以称作表空间。
Oracle数据库中至少存在一个表空间,即SYSTEM的表空间。
(1)创建表空间
语法:
create tablespace 表空间名 datafile/tempfile 文件路径 [size 文件大小] [autoextend on next 新增文件大小] [maxsize 最大文件大小];
create tablespace tptest datafile 'd:/tptest01.dbf' size 10m autoextend on next 10m maxsize 1G;
(2)调整表空间
a.添加数据文件
alter tablespace tptest add datafile 'd:/tptest02.dbf' size 10m;
b.改变数据文件大小
alter tablespace datafile ‘*.dbf’ autoextend on next 20M maxsize 1000M;
c.删除数据文件
alter tablespace tablespace_name drop datafile file_name;
(3)查询表空间
a.根据默认视图查询
select name from v$tablespace;
b.根据数据文件查询表空间
select file_name,tablespace_name from dba_data_files order by tablespace_name;
c.统计数据文件使用率
select b.file_name 物理文件名, b.tablespace_name 表空间,
b.bytes/1024/1024 大小M, (b.bytes - sum(nvl(a.bytes,0))) / 1024 / 1024 已使用M,
substr((b.bytes - sum(nvl(a.bytes, 0))) / (b.bytes) * 100, 1, 5) 利用率
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.bytes
order by b.tablespace_name;
(4)删除表空间
drop tablespace tpname including contents and datafiles;
注:oracle表空间删除数据文件未删除
(5)表空间只读
alter tablespace tpname READ ONLY
(6)表空间离线在线
alter tablespace tpname OFFLINE/ONLINE
(7)表空间文件离线删除
alter tablespace tpname datafile 'tpname.dbf' offline drop;