0. 创建表空间
create tablespace cloud datafile '/oracle/oradata/sky/cloud01.dbf' size 500M;
1. 表空间的删除
(1) 删除空的表空间,但是不包含物理文件
drop tablespace tablespace_name;
(2) 删除非空表空间,但是不包含物理文件
drop tablespace tablespace_name including contents;
(3) 删除空表空间,包含物理文件
drop tablespace tablespace_name including datafiles;
(4) 删除非空表空间,包含物理文件
drop tablespace tablespace_name including contents and datafiles;
(5) 如果其他表空间中的表有外键等约束关联到了本表空间中的表的字段,就要加上cascade constraints
drop tablespace tablespace_name including contents and datafiles cascade constraints;
2. 增加表空间容量
(1) 使用resize扩大原有的数据文件
alter database datafile '/oracle/oradata/sky/cloud02.dbf' resize 2g;
(2) 增加数据文件
alter tablespace cloud add datafile '/oracle//oradata/sky/cloud03.dbf' size 2g;
3. 设置表空间是否自动扩展
(1) 在添加数据文件时设置表空间为自动扩展(12c默认是不自动扩展)
alter tablespace cloud add datafile '/oracle//oradata/sky/cloud03.dbf' size 2g autoextend on ;
alter tablespace cloud add datafile '/oracle//oradata/sky/cloud03.dbf' size 2g autoextend on next 200M maxsize unlimited;
alter tablespace cloud add datafile '/oracle//oradata/sky/cloud03.dbf' size 2g autoextend on next 200M maxsize 8g;
(2) 现有数据文件文件设置自动增长
alter database datafile '/oracle/oradata/sky/cloud02.dbf' autoextend on;
(3) 现有数据文件文件设置非自动增长
alter database datafile '/oracle/oradata/sky/cloud02.dbf' autoextend off;
4. 修改用户默认表空间
alter user cloud default tablespace cloud;
5. 查询表空间使用率
SELECT upper(f.tablespace_name) "表空间名",
d.total "表空间大小(M)",
d.total - f.total_bytes "已使用空间(M)",
to_char(round((d.total -f.total_bytes) / d.total *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) total
FROM sys.dba_data_files dd
GROUP BY dd.tablespace_name) d
WHERE d.tablespace_name =f.tablespace_name
ORDER BY 4 DESC;