管理表空间和数据文件

1.建立表空间:

create tablespace test datafile '/u01/test.dbf'  size 10M uniform size 128k
#指定区尺寸128k ,大小8K
 
#大文件表空
create bigfile tablespace big_tbs datafile  '/u01/big_tbs.dbf ' size 100G

2.建非标准表

show parameter db
alter system set db_2k_cache_size=10M
create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k
 
#见错误
SQL> alter system set db_2k_cache_size=2M;
alter system set db_2k_cache_size=2M
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-00384: Insufficient memory to grow cache
#解决
SQL> alter system set sga_max_size=400M scope=spfile;
SQL> shutdown immediate;
SQL> startup
SQL> alter system set db_2k_cache_size=10M;
System altered.

3.查看区大小与块大小

#区大小
conn y / 123
create table t(i number) tablespace test;
Insert into t values(10)
select bytes/1024 from user_segments where segment_name=upper('t');
#大小
Show parameter block(64K)
#准表空blocksize
SQL> select * from v$dbfile;
SQL> select name,block_size,status from v$datafile;
SQL> select block_size from v$datafile where file#=14;

4.删除表空间

drop tablespace test include contents and datafiles

5.查表空间:

#数据文件
select * from v$dbfile;
#所有表空
select * from v$tablespace;
#表空的数据文件
select file_name,tablespace_name from dba_data_files;

6.建立undo表空间

create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M;
#到新建的undo表空
alter system set undo_tablespace=undotbs01;

7.建立临时表空间

create temporary tablespace temp_data tempfile '/u01/temp.db'  size 5M;
create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db'  size 5M;

8.改变表空间状态

(0.)查看状态
#表空
select tablespace_name,block_size,status from dba_tablespaces;
#数据文件状
select name,block_size,status from v$datafile;
(1.)表空间脱机
alter tablespace test offline
#如果意外了数据文件
alter tablespace test offline for recover
(2.)表空间联机
alter tablespace test online
(3.)数据文件脱机
select * from v$dbfile;
alter database datafile 3 offline
(4.)数据文件联机
recover datafile 3;
alter database datafile 3 online;
(5.)使表空间只读
alter tablespace test read only
(6.)使表空间可读写
alter tablespace test read write;

9.扩展表空间

#首先看表空的名字和所属文件及空
select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space
from dba_data_files order by tablespace_name;
 
#种扩展方法
1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(加一个datafile)
2.alter database datafile '/u01/test.dbf' resize 20M;
3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;
 
#定后看表空信息
select a.tablespace_name,a.bytes total,b.bytes used,c.bytes free,(b.bytes*100)/a.bytes "% used",(c.bytes*100)/a.bytes "% free" from sys.sm$ts_avail a,sys.sm$ts_used b,sys.sm$ts_free c where a.tablespace_name=b.tablespace_name and a.tablespace_name=c.tablespace_name;

10.移动表空间的数据文件

#先确定数据文件据在表空
SQL>select tablespace_name,file_name from dba_data_files where file_name='/u01/test.dbf';
#open
SQL>alter tablespace test offline;
SQL>host move /u01/test.dbf  /u01/oracle/test.dbf;
SQL>alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf';
SQL>alter tablespace test offline;
 
#mount
SQL>shutdown immediate;
SQL>startup mount
SQL>host move /u01/test.dbf  /u01/oracle/test.dbf;
SQL>alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';

11.表空间和数据文件常用的数据字典与动态性能视图

v$dbfile
v$datafile
dba_segments
user_segments
dba_data_files
v$tablespace
dba_tablespaces
user_tablespaces