MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 数据库 / 正文

Oracle 表空间管理

2024-11-26 17:32 huorong 数据库 6 ℃ 0 评论

表空间修改/扩展

增加现有表空间的容量:

查询现各表空间容量

SQL> select file_name,tablespace_name,bytes/1024/1024 from dba_data_files;

FILE_NAME				       TABLESPACE BYTES/1024/1024
--------------------------------------------- ---------- ---------------
/u01/app/oracle/oradata/ORCL/system01.dbf     SYSTEM		     850
/u01/app/oracle/oradata/ORCL/sysaux01.dbf     SYSAUX		     750
/u01/app/oracle/oradata/ORCL/undotbs01.dbf    UNDOTBS1		     310
/u01/app/oracle/oradata/ORCL/users01.dbf      USERS		     100
/u02/ts/ts1.dbf 			      TS1		      50
/u02/ts/ts22.dbf			      TS22		      20
/u01/app/oracle/oradata/ORCL/tsts41.dbf       TS4		      50

将TS1的空间调整到60MB

SQL> alter database datafile '/u02/ts/ts1.dbf' resize 60M;
Database altered.

为表空间增加数据文件:

为TS22表空间增加一个20M大小的数据文件。

SQL> alter tablespace ts22 add datafile '/u02/ts/ts22-1.dbf' size 20M;
Tablespace altered.

允许表空间自动扩展到多大:

允许TS4自动扩展到100M

SQL> alter database datafile '/u01/app/oracle/oradata/ORCL/tsts41.dbf' 
  2   autoextend on next 10M maxsize 100M; 
Database altered.

再次查询一下:

SQL> select tablespace_name,file_name,
  2  autoextensible,increment_by,maxbytes/1024/1024 as max_mb  
  3  from dba_data_files 
  4  where tablespace_name = 'TS4';

TABLESPACE_NAME      FILE_NAME				      AUTOEX INCREMENT_BY     MAX_MB
-------------------- ---------------------------------------- ------ ------------ ----------
TS4		     /u01/app/oracle/oradata/ORCL/tsts41.dbf  YES	     1280	 100

表空间的使用

默认临时表空间使用临时表空间组

假设已经创建了临时表空间组g1

 SQL>alter database  default temporary tablespace g1;

为scott用户指定默认的临时表空间temp,不使用系统默认的临时表空间

SQL>alter user scott temporary tablespace temp1;

给scott用户指定默认的表空间ts2

SQL>alter user scott default tablespace ts2;

给数据库指定默认的表空间 ts1,不使用默认的 users 表空间

SQL>alter database default tablespace ts1;

给表指定默认的表空间:

SQL> create table tb1(id number)
2 tablespace ts7;

表空间状态改变

SQL>alter tablespace ts1 offline;   --表空间离线,不能读写数据
SQL>alter tablespace ts1 online;   --恢复表空间的在线状态
SQL>alter tablespace ts1 read only;    --使表空间处于只读状态
SQL>alter tablespace ts1 read write;  --表空间为读写状态

表空间删除

删除临时表空间组:

停止(删除)表空间tmp1,tmp2的临时表空间组,停止临时表空间组,需要指定一个数据库默认的临时表。

SQL>alter database default temporary tablespace tmp1;   --指定数据库默认临时表空间。
SQL>alter tablespace tmp1 tablespace group '';
SQL>alter tablespace tmp2 tablespace group '';

删除用户的表空间:

一、不带参数(选项)

直接删除表空间不带参数,如果表空间已分配给某个用户,或者某个表使用的情况下,是不能直接删除,系统会提示,表控件不是空的,需要使用 INCLUDING CONTENTS选项进行删除;

SQL> create tablespace ts6
  2  datafile '/u02/ts/ts6.dbf' size 10M;

SQL> create table tb2(id number) 
  2  tablespace ts6;

SQL> drop tablespace ts6;
drop tablespace ts6
*
ERROR at line 1:
ORA-01549: tablespace not empty, use INCLUDING CONTENTS option

如果需要使用上面的方法删除ts6表空间,则需要将tb2的表空间移动到其他表空间。然后再到系统中删除对于的数据文件。

SQL> alter table tb2 move tablespace ts5;
Table altered.

SQL> drop tablespace ts6;
Tablespace dropped.

[oracle@host01 ts]$ rm ts6.dbf -rf

二、带including contents参数(选项)

including contents 选项会删除表空间中的元数据,导致使用该表空间的表的数据全部删除,数据文件会保留,需要手动删除。

SQL> drop tablespace ts5 including contents;
Tablespace dropped.

[oracle@host01 ts]$ rm ts5

三、删除表空间时将数据文件一起删除()

SQL> create tablespace ts7
  2  datafile '/u02/ts/ts7' size 10m;

Tablespace created.

SQL> create table tb1(id number)
  2  tablespace ts7;

Table created.

SQL>  select a.table_name,a.tablespace_name,b.file_name 
  2  from user_tables a
  3  join dba_data_files b on b.tablespace_name =  a.tablespace_name
  4  where table_name = 'TB1';

TABLE_NAME	     TABLESPACE_NAME	  FILE_NAME
-------------------- -------------------- ----------------------------------------
TB1		     TS7		  /u02/ts/ts7

SQL> select * from tb1;  

	ID
----------
	 1
	 2

SQL>  drop tablespace ts7 including contents and datafiles;

Tablespace dropped.


SQL> select a.table_name,a.tablespace_name,b.file_name
  2  from user_tables a
  3  join dba_data_files b on b.tablespace_name =  a.tablespace_name
  4  where table_name = 'TB1';

no rows selected

SQL> select * from tb1;
select * from tb1
              *
ERROR at line 1:
ORA-00942: table or view does not exist

数据文件在线移动:

SQL> create table tb2 (id number)
  2  tablespace ts4;

Table created.


SQL> select a.table_name,a.tablespace_name,b.file_name
  2 from user_tables a
  3 join dba_data_files b on b.tablespace_name =  a.tablespace_name
  4 where table_name = 'TB2'; 

TABLE_NAME	     TABLESPACE_NAME	  FILE_NAME
-------------------- -------------------- ----------------------------------------
TB2		     TS4		  /u01/app/oracle/oradata/ORCL/tsts41.dbf

SQL> alter database move datafile '/u01/app/oracle/oradata/ORCL/tsts41.dbf' to
  2  '/u02/ts/ts4.dbf';

Database altered.

SQL> select a.table_name,a.tablespace_name,b.file_name
  2 from user_tables a
  3 join dba_data_files b on b.tablespace_name =  a.tablespace_name
  4 where table_name = 'TB2';  

TABLE_NAME	     TABLESPACE_NAME	  FILE_NAME
-------------------- -------------------- ----------------------------------------
TB2		     TS4		  /u02/ts/ts4.dbf


Tags:oracle 表删除

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言