日期:2014-05-16  浏览次数:20526 次

oracle修改表空间(转)
简单介绍
在 Oracle 10g 以前的版本,更改表空间名字是几乎不可能的事情,除非删除,重新创建,大费周章。Oracle 10g 新添加了一项更改表空间名字的功能,使得更改表空间名字瞬间即可完成。是个较为人性化的功能。

SQL> COL FILE_NAME format a70
SQL> SET linesize 120
SQL> SET pagesize 99
SQL> COL TABLESPACE_NAME format a10
SQL>
SQL> SELECT file_name, tablespace_name FROM dba_data_files;

FILE_NAME TABLESPACE
---------------------------------- ----------
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/users01.dbf USERS
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/sysaux01.dbf SYSAUX
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/undotbs01.dbf UNDOTBS1
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/system01.dbf SYSTEM
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/example01.dbf EXAMPLE
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/foo1.dbf FOO


6 rows selected.

该命令的语法很简单:
/********************************语法在这里!!!*************************************************/

ALTER TABLESPACE tablespacename RENAME TO newtablespacename;

tablespacename 和newtablespacename 分别对应原来的表空间名字和更改后的表空间名字:

实战演练
注意:在操作前后都请做好控制文件的备份工作

SQL>ALTER TABLESPACE foo RENAME TO test;

Tablespace altered.

SQL> SELECT file_name, tablespace_name FROM dba_data_files;

FILE_NAME TABLESPACE
---------------------------------- ----------
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/users01.dbf USERS
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/sysaux01.dbf SYSAUX
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/undotbs01.dbf UNDOTBS1
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/system01.dbf SYSTEM
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/example01.dbf EXAMPLE
/u01/app/oracle/product/10.1.0/db_1/oradata/TEST/foo1.dbf TEST

因为 system 和 sysaux 这两个表空间的特殊性,是不可以更名的:

SQL> ALTER TABLESPACE system RENAME TO mysystem;
ALTER TABLESPACE system RENAME TO mysystem
*
ERROR at line 1:
ORA-00712: cannot rename system tablespace

SQL> ALTER TABLESPACE sysaux RENAME TO mysysaux;
ALTER TABLESPACE sysaux RENAME TO mysysaux
*
ERROR at line 1:
ORA-13502: Cannot rename SYSAUX tablespace

可以对 undo tablespace 重新命名,如果使用的是 spfile ,而不是 pfile, Oracle 会自动对 spfile 中的 undo_tablespace 进行更改(不过要在数据库重新启动之后才可以观察到), 如果使用的是 pfile ,要对其进行手工更改。我们看看 spfile 的变化情况:

SQL> ALTER tablespace undotbs1 RENAME TO undotbs;

Tablespace altered.

SQL>
SQL> show parameter pfile

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
spfile string /u01/app/oracle/product/10.1.0
/db_1/dbs/spfileTEST.ora
SQL> show parameters undo

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 180355072 bytes
Fixed Size 777996 bytes
Variable Size 128983284 bytes
Database Buffers 50331648 bytes
Redo Buffers 262144 bytes
Database mounted.
Database opened.
SQL> show parameters undo

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS
SQL>

对脱机表空间的更名是不允许的:

SQL> ALTER TABLESPACE TEST OFFLINE;

Tablespace altered.

SQL> ALTER TABLESPACE test RENAME TO testoffline;
ALTER TABLESPACE test RENAME TO testoffline
*
ERROR at