您的位置:首页 > 数据库 > Oracle

oracle表空间管理

2012-10-24 12:32 423 查看
管理表空间

一: 设置表空间的只读状态

1 read-only的前提条件:

表空间必须online

表空间不能是undo 表空间或者system表空间

表空间不能处于在线备份状态

2 使表空间read-only 的语句

alter tablespace zx read only;

3 使表空间可读可写

alter tablespace zx read write;

4 eg:

SQL> alter tablespace zx read only;

Tablespace altered.

SQL> alter tablespace zx read write;

Tablespace altered.

二 表空间的维护

1 增加表空间的大小

增加数据文件

alter tablespace zx add datafile '/oracle/CRM2/CRM/zx3.dbf' size 1m;

调整数据文件大小为自动增长

alter database datafile '/oracle/CRM2/CRM/zx2.dbf' autoextend on;

eg:

SQL> alter database datafile '/oracle/CRM2/CRM/zx2.dbf' autoextend on;

Database altered.

SQL> alter database datafile '/oracle/CRM2/CRM/zx2.dbf' autoextend off;

Database altered.

三大数据文件表空间的创建和修改

1 大数据文件表空间创建注意事项

指定bigfile关键字oracle将创建一个本地管理以及段自动管理的表空间。

创建时可以指定 extent management local和segment space management auto。

如果指定 extent management dictionary 或者 segment space managemnet manual 将报错。

2 语句

create bigfile tablespace zxbigtbs

datafile '/oracle/CRM2/CRM/zxbig1.dbf' size 1G;

size 单位可以指定k/m/g/t/

3 查询数据库内部的大表空间

可查询视图dba_tablespaces、user_tablespaces、v$tablespace 的bigfile字段

eg:

SQL> select tablespace_name,bigfile from dba_tablespaces;

TABLESPACE_NAME BIG

------------------------------ ---

SYSTEM NO

SYSAUX NO

TEMP NO

USERS NO

UNDOTBS2 NO

ZX NO

ZXBIGTBS YES

4 修改大数据文件的表空间

调整大小

alter tablespace zxbigtbs resize 2G;

调整数据文件自动扩展

alter tablespace zxbigtbs autoextend on next 20G;

四 临时表空间

1 新安装的oracle 默认会创建一个temp临时表空间

用户可以创建额外的临时表空间

可以给数据库每一个用户指定一个临时表空间

用户不能明确的在临时表空间中创建对象

2 默认的临时表空间

如果用户没有明确指定临时表空间,则用户默认临时表空间为temp

更改数据库默认临时表空间

alter database default temporary tablespace 表空间名字;

确认当前数据库默认的临时表空间

select property_name,property_value from database_properties where property_name='DEFAULT_TEMP_TABLESPACE'

PROPERTY_NAME PROPERTY_VALUE

------------------------------ ------------------------------

DEFAULT_TEMP_TABLESPACE TEMP

3 创建本地管理的临时表空间

create temporary tablespace mytemp tempfile '/oracle/CRM2/CRM/mytemp.dbf' size 200M autoextend on next 10M maxsize 1G;

4 创建一个大数据文件的临时表空间以及对该大表空间的修改

create bigfile temporary tablespace mynewtemp tempfile '/oracle/CRM2/CRM/newtemp.dbf' size 2G;

SQL> alter tablespace mynewtemp resize 3G;

Tablespace altered.

SQL> alter tablespace mynewtemp autoextend on next 20G maxsize 40G;

Tablespace altered.

5 11g下查询临时表空间的使用率

SQL> select * from dba_temp_free_space;

TABLESPACE_NAME TABLESPACE_SIZE ALLOCATED_SPACE FREE_SPACE

------------------------------ --------------- --------------- ----------

TEMP 228589568 228589568 227540992

MYNEWTEMP 3221225472 1048576 3220176896

5 临时表空间组

临时表空间组的特性:

a 至少包括一个表空间,对表空间个数无明确限制

b 可以把临时表空间组名,作为数据库默认的表空间,或者分配给用户。

c 如果临时表空间组被作为默认的临时表空间,则不能直接丢弃组内任何成员,需要先从组内移除该表空间。

创建临时表空间组

临时表空间组不能直接被创建,只能间接创建

语句:

方式一: create temporary tablespace ltemp1 tempfile '/oracle/CRM2/CRM/ltemp01.dbf' size 50M tablespace group tempgroup1;

方式二: create temporary tablespace ltemp2 tempfile '/oracle/CRM2/CRM/ltemp02.dbf' size 50M;

alter tablespace ltemp2 tablespace group tempgroup2;

eg:

方式一:SQL> create temporary tablespace ltemp1 tempfile '/oracle/CRM2/CRM/ltemp01.dbf' size 50M tablespace group tempgroup1;

Tablespace created.

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

方式二:SQL> create temporary tablespace ltemp2 tempfile '/oracle/CRM2/CRM/ltemp02.dbf' size 50M;

Tablespace created.

SQL> alter tablespace ltemp2 tablespace group tempgroup2;

Tablespace altered.

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

TEMPGROUP2 LTEMP2

对临时表空间组的修改

移动组内的成员语句:alter tablespace ltemp2 tablespace group tempgroup1;

从组内删除某个临时表空间语句: alter tablespace ltemp2 tablespace group ‘’;

eg: 移动组内成员(注意ltemp2属于组tempgroup2也可以直接从组2移动到组1)

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

TEMPGROUP2 LTEMP2

SQL> alter tablespace ltemp2 tablespace group tempgroup1;

Tablespace altered.

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

TEMPGROUP1 LTEMP2

eg:删除组内某个临时表空间

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

TEMPGROUP1 LTEMP2

SQL> alter tablespace ltemp2 tablespace group '';

Tablespace altered.

SQL> select * from dba_tablespace_groups;

GROUP_NAME TABLESPACE_NAME

------------------------------ ------------------------------

TEMPGROUP1 LTEMP1

分配一个临时表空间组作为数据库默认临时表空间

语句:alter database default temporary tablespace 组名;

eg

SQL> alter database default temporary tablespace tempgroup1;

Database altered

select property_name,property_value from database_properties where property_name='DEFAULT_TEMP_TABLESPACE'

PROPERTY_NAME PROPERTY_VALUE

------------------------------ ------------------------------

DEFAULT_TEMP_TABLESPACE TEMPGROUP1

五:重命名表空间

注意事项

1 compatible 参数必须设置成10.0.0 或更高

2 不能重命名system、sysaux表空间

3 重命名时应先检查表空间对应的数据位置是否离线,如果离线,重命名则报错。

4 最好表空间read write 否则数据文件头部记录的表空间名不更新。

5 当重命名时,将更新所有和该表空间有关的数据字典,控制文件,和在线数据文件头部。

6 如果表空间是临时表空间,更名后,视图database_properties将自动更新并使用新的临时表空间名。

语句 alter tablespace 表空间名 rename to 新表空间名

eg:

SQL> alter tablespace mytemp rename to testtemp;

Tablespace altered.

SQL> select tablespace_name from dba_tablespaces;

TABLESPACE_NAME

------------------------------

SYSTEM

SYSAUX

TEMP

USERS

UNDOTBS2

ZX

ZXBIGTBS

TESTTEMP

LTEMP1

LTEMP2

六 drop 表空间

参数说明:

including contents 将丢弃表空间所有段 如果表空间是空的,不包括任何表,视图,结构,则不需要指定该参数

cascade constraints 将丢弃所有引用和约束

including contents and datafiles 丢弃表空间的同时删除数据文件

语句drop tablespace 表空间 including contents and datafiles;

eg

SQL> select tablespace_name from dba_tablespaces;

TABLESPACE_NAME

------------------------------

SYSTEM

SYSAUX

TEMP

USERS

UNDOTBS2

ZX

ZXBIGTBS

TESTTEMP

LTEMP1

LTEMP2

10 rows selected.

SQL> drop tablespace testtemp including contents and datafiles;

Tablespace dropped.

本文出自 “myblog” 博客,请务必保留此出处http://jiujian.blog.51cto.com/444665/1035174
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: