创建表空间
表空间:最大的逻辑存储文件,与物理上的一个或多个数据文件对应,每个数据库至少拥有一个表空间,表空间的大小等于构成表空间的所有数据文件的大小总和,用于存储用户在数据库中存储的所有内容。
分为基本表空间、临时表空间、大文件表空间、非标准数据块表空间和撤销表空间。
基本表空间:用于存储用户的永久性数据
临时表空间:排序、汇总时产生的临时数据
大文件表空间:存储大型数据,如LOB
非标准数据块表空间:创建数据块大小不同的表空间
撤销表空间:存储事务的撤销数据,在数据恢复时使用
创建表空间语句:
create [temporary|undo]tablespace tablespace_name[datafile|tempfile] 'filename' size
size K|M[reuse] //已经存在是否指定reuse
[autoextend off|on //数据文件是否自动扩展
[next number K|M maxsize unlimited|number K|M]
][……]
[mininum extent number K|M]
[blocksize number K] //初始化参数数据库大小,只能用于持久表空间
[online|offline] //online表空间可用
[logging|nologging]
[force logging]
[default storage storage] //设置默认存储参数
[compress|nocompress] //压缩数据段内数值
[premanent|temporary] //持久保存数据库对象|临时
[extent management dictionary|local //数据字典管理方式|本地管理,一般本地
[autoallocate|uniform size number K|M]]
[segment space management auto|manual]; //表空间段的管理方式自动|手动
系统默认表空间:
system:系统表空间,用于存储系统的数据字典、系统的管理信息和用户数据表等。
sysaux:辅助系统表空间,减少系统表空间负荷,体改系统作业效率,oracle系统自动维护,一
般不用于存储数据结构。
temp:临时表空间。
undotbsl:撤销表空间,用于在自动撤销管理方式下存储撤销信息。
users:用户表空间。
打开SQL*PLUS
SQL> select tablespace_name from dba_tablespaces; 查询数据库中存在的表空间,也可查看user_tablespaces
SQL> create tablespace myspace
2 datafile 'D:\oradata\myspace.dbf'
3 size 10M autoextend on next 5M maxsize 100M;
SQL> select tablespace_name from dba_tablespaces;
表空间状态属性
分为online(在线)、offline(离线)、read only(只读)、read write(读写)四种,只读和读写需在在线状态下
alter tablespace tablespace_name online;
设置为offline时可进行脱机备份或升级维护
alter tablespace tablespace_name offline parameter; //参数可为normal(执行检查点)、temporary(临时方式切换到离线执行检查点时不会检查数据文件是否使用)、immediate(立即,不会执行检查点)、for recover(以恢复方式进行切换)
alter tablespace tablespace_name read only;
alter tablespace tablespace_name read write;
SQL> select tablespace_name,status from dba_tablespaces; //查看表空间状态
SQL> alter tablespace myspace offline;
设置默认表空间
默认永久性表空间为system,临时为temp
alter database default [temporary] tablespace tablespace_name;//temporary设置临时表空间
查询默认表空间:
select default_tablespace from user_users;
select property_name,property_value
from database_properties where property_name //查询默认临时表空间
in ('DEFAULT_PERMANENT_TABLESPACE','DEFAULT_TEMP_TABLESPACE'); //查询默认新用户表空间
SQL> create tablespace testspace
2 datafile 'D:\oradata\testspace.dbf'size 20M;
SQL> alter database default tablespace testspace; //修改默认临时表空间
SQL> select property_name,property_value
2 from database_properties where property_name
3 in ('DEFAULT_PERMANENT_TABLESPACE','DEFAULT_TEMP_TABLESPACE');