8.1 表空间和数据文件的关系
表空间可以看成oracle数据库的逻辑结构
数据文件可以看成oracle数据库的物理结构
SQL> col tablespace_name for a10;
SQL> col file_name for a50;
SQL> col bytes for 999,999,999;
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
8.2 oracle 11g的默认表空间
表空间
|
说明
|
example
|
存放各样例的数据 |
system
|
存放数据字典,包括表,视图,存储过程的定义等
|
sysaux
|
system表空间的辅助表空间,存储除数据字典外的其他数据对象
|
temp
|
存放SQL语句处理的表和索引的信息,比如数据排序就占有此空间
|
undotbs1
|
存放撤销数据的表空间
|
users
|
存放“应用系统”所使用的数据库对象
|
SQL> col segment_type for a20;
SQL> col segment_name for a30;
SQL> col owner for a10;
SQL> select segment_type,segment_name,owner from dba_segments where tablespace_name='USERS';
SEGMENT_TYPE SEGMENT_NAME OWNER
-------------------- ------------------------------ ----------
TABLE DEPT SCOTT
TABLE EMP SCOTT
TABLE SALGRADE SCOTT
NESTED TABLE ACTION_TABLE OE
NESTED TABLE LINEITEM_TABLE OE
TABLE PURCHASEORDER OE
NESTED TABLE PRODUCT_REF_LIST_NESTEDTAB OE
NESTED TABLE SUBCATEGORY_REF_LIST_NESTEDTAB OE
TABLE CATEGORIES_TAB OE
TABLE JOBS_TEMP HR
INDEX PK_DEPT SCOTT
SEGMENT_TYPE SEGMENT_NAME OWNER
-------------------- ------------------------------ ----------
INDEX PK_EMP SCOTT
LOBINDEX SYS_IL0000074466C00004$$ OE
INDEX SYS_C0011033 OE
LOBINDEX SYS_IL0000074470C00005$$ OE
LOBINDEX SYS_IL0000074470C00010$$ OE
INDEX SYS_C0011034 OE
LOBINDEX SYS_IL0000074465C00004$$ OE
LOBINDEX SYS_IL0000074465C00005$$ OE
LOBINDEX SYS_IL0000074465C00008$$ OE
LOBINDEX SYS_IL0000074465C00012$$ OE
LOBINDEX SYS_IL0000074465C00017$$ OE
SEGMENT_TYPE SEGMENT_NAME OWNER
-------------------- ------------------------------ ----------
LOBINDEX SYS_IL0000074465C00026$$ OE
LOBINDEX SYS_IL0000074465C00033$$ OE
INDEX LINEITEM_TABLE_MEMBERS OE
INDEX ACTION_TABLE_MEMBERS OE
INDEX SYS_C0011037 OE
INDEX SYS_FK0000074515N00009$ OE
INDEX SYS_FK0000074515N00007$ OE
INDEX SYS_C0011038 OE
INDEX SYS_C0011039 OE
INDEX SYS_C0011040 OE
INDEX SYS_C0011041 OE
SEGMENT_TYPE SEGMENT_NAME OWNER
-------------------- ------------------------------ ----------
INDEX SYS_C0011128 HR
LOBSEGMENT SYS_LOB0000074466C00004$$ OE
LOBSEGMENT SYS_LOB0000074470C00005$$ OE
LOBSEGMENT SYS_LOB0000074470C00010$$ OE
LOBSEGMENT NAMESPACES567_L OE
LOBSEGMENT EXTRADATA566_L OE
LOBSEGMENT SYS_XDBPD$561_L OE
LOBSEGMENT SYS_XDBPD$562_L OE
LOBSEGMENT SYS_XDBPD$563_L OE
LOBSEGMENT SYS_XDBPD$564_L OE
LOBSEGMENT SYS_XDBPD$565_L OE
44 rows selected.
SQL> select owner,count(segment_name) from dba_segments where tablespace_name='SYSAUX' group by owner;
OWNER COUNT(SEGMENT_NAME)
---------- -------------------
MDSYS 262
CTXSYS 55
OLAPSYS 117
SYSTEM 149
EXFSYS 58
APEX_03020 467
0
DBSNMP 8
ORDSYS 7
SYSMAN 578
OWNER COUNT(SEGMENT_NAME)
---------- -------------------
XDB 885
ORDDATA 148
SYS 927
WMSYS 49
13 rows selected.
8.3 创建表空间
建议将不同类型的数据对象存放到不同的表空间
创建表空间必须考虑以下几点:
(1)是创建小文件表空间,还是大文件表空间(默认为小文件表空间)
(2)是使用局部盘区管理方式,还是使用传统的目录盘区管理方式(默认为局部盘区管理)
(3)是手动管理段空间,还是自动管理段空间(默认是自动)
(4)是否用于临时段或撤销段的特殊表空间
8.3.1 创建表空间的语法
语法格式:
create
[smallfile|bigfile]|[undo]
tablespace
tablespace_name
datafile
'/path/filename'
size
num[k/m]
reuse
[,'/path/filename'
size
num[k/m]
reuse]
[,...]
[autoextend [on|off] next num[k/m]
[
maxsize
num[k/m] |
unlimited]
[
mininum extent
num[k/m]]
[
default storage
storage]
[
online | offline]
[
logging | nologging]
[
permanent | temporary]
[extent management dictionary | local [autoallocate | uniform size
num[k/m]]]
1、语法中的关键字:
small|bigfile:表示创建的所需文件表空间还是大文件表空间
reuse:表示若该文件存在,则清除该文件再重新建立该文件
autoextend [on|off] next:表示数据文件为自动扩展或非自动扩展,如果是自动扩展,则需要设置next的值
maxsize:表示当数据文件自动扩展时,允许数据文件扩展的最大长度字节数,unlimited关键字表示不需要指定字节长度
minimum extend:指定最小的长度,由操作系统和数据库的块决定
default storage:指定以后要创建的表、索引及簇的储存参数值
online|offline:指定在线或离线
logging|nologging:指定该表空间内的表在加载数据时是否产生日志,默认为logging
permanent|temporary:指定创建的表空间是永久性表空间或临时表空间,默认为永久性表空间
extend management dictionary | local:指定表空间的扩展方式是使用数据字典管理还是本地化管理,默认为本地化管理
autoallocate|uniform size:如果采用本地化管理表空间,在表空间扩展时,指定没出盘区扩展的大小是由系统自动指定还是按照等同大小进行。
2、语法中的参数
tablespace_name:表示要创建的表空间名称
'/path/filename':表示数据文件的路径和名称
8.3.2 通过本地化管理方式创建表空间
SQL> col tablespace_name for a10;
SQL> col file_name for a50;
SQL> col bytes for 999,999,999;
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
SQL> create tablespace tbs_test_1 datafile '/ora/app/oracle/oradata/stone1/datafile1.dbf'
2 size 10m
3 extent management local uniform size 256k;
Tablespace created.
SQL> create tablespace tbs_test_2 datafile '/ora/app/oracle/oradata/stone1/datafile2.dbf'
2 size 10m
3 extent management local autoallocate;
Tablespace created.
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
7 rows selected.
8.3.3 通过段空间管理方式创建表空间
只有本地化管理方式的表空间,才能在其基础上进一步建立段空间管理方式
使用“segment space management manual/local”语句
1、手动段空间管理方式
PCT_FREE,PCT_USED
SQL> create tablespace tbs_test_3
2 datafile '/ora/app/oracle/oradata/stone1/datafile3.dbf'
3 size 10m
4 extent management local autoallocate
5 segment space management manual;
Tablespace created.
2、自动段空间管理方式
创建表空间首选方式
需要注意以下两种情况:
a、自动段空间管理方式不能用于创建临时表空间和系统表空间
b、oracle推荐使用自动段空间管理方式管理永久表空间,但默认情况下却是manual,所有在创建表空间时需要明确指定为auto
SQL> create tablespace tbs_test_4
2 datafile '/ora/app/oracle/oradata/stone1/datafile4.dbf'
3 size 10m
4 extent management local autoallocate
5 segment space management auto;
Tablespace created.
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 10,485,760
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
9 rows selected.
8.3.4 创建非标准块表空间
需要注意以下几点:
a、表空间的非标准块的大小为基本块的倍数。
b、oracle通常使用SGA自动共享内存管理,因此需要设置初始化参数db_16k_cache_size=16K。
c、这种块较大的表空间通常用来存放大对象(LOB)类型
SQL> alter system set db_16k_cache_size=16k scope=both;
System altered.
SQL> create tablespace tbs_test_5
2 datafile '/ora/app/oracle/oradata/stone1/datafile5.dbf'
3 size 10m
4 autoextend on next 4m maxsize unlimited
5 blocksize 16k
6 extent management local autoallocate
7 segment space management auto;
Tablespace created.
8.3.5 建立大文件表空间
大文件(bigfile)表空间注意用在存储区域网络(san),磁盘阵列,自动存储管理上等
SQL> create bigfile tablespace tbs_big
2 datafile '/ora/app/oracle/oradata/stone1/datafilebig.dbf'
3 size 10m;
Tablespace created.
SQL> alter tablespace tbs_big resize 20m;
Tablespace altered.
SQL> alter tablespace tbs_test_4 resize 20m;
alter tablespace tbs_test_4 resize 20m
*
ERROR at line 1:
ORA-32773: operation not supported for smallfile tablespace TBS_TEST_4
SQL> alter database datafile '/ora/app/oracle/oradata/stone1/datafile4.dbf'
2 resize 20m;
Database altered.
8.4 维护表空间和数据文件
8.4.1 设置数据库默认表空间
SQL> alter database default tablespace tbs_test_1;
Database altered.
8.4.2 更改表空间的状态
设置表空间只读必须满足下列条件:
a、该表空间必须为online状态
b、该表空间不能包含任何回滚段
c、该表空间不能在归档模式下
SQL> alter tablespace tbs_test_2 read only;
Tablespace altered.
SQL> alter tablespace tbs_test_2 read write;
Tablespace altered.
8.4.3 重命名表空间
只能对普通的表空间进行重命名,不能对system和sysaux表空间进行重命名,也不能对已经处于offline状态的表空间进行重命名。
SQL> alter tablespace tbs_test_5 rename to tbs_test_5_new;
Tablespace altered.
8.4.4 删除表空间
语法格式:
drop tablespace tbs_name [including contents] [cascade constraints]
tbs_name:表空间名称
including contents:删除表空间的同时删除表空间中的数据,如果不指定该参数,而表空间又有数据时,则oracle会提示错误。
cascade constraints:删除表空间的同时也删除相关的完整性约束,如果完整性操作,而没有该参数,在会报错
SQL> drop tablespace tbs_test_5_new
2 including contents
3 cascade constraints;
Tablespace dropped.
8.4.5 维护表空间中的数据文件
1、向表空间添加数据文件
SQL> select tablespace_name,file_name,bytes from dba_data_files order by tablespace_name;
TABLESPACE FILE_NAME BYTES
---------- -------------------------------------------------- ------------
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf 104,857,600
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf 555,745,280
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf 713,031,680
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf 20,971,520
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf 10,485,760
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf 10,485,760
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf 10,485,760
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf 20,971,520
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf 89,128,960
USERS /ora/app/oracle/oradata/stone1/users01.dbf 5,242,880
10 rows selected.
SQL> alter tablespace users
2 add datafile '/ora/app/oracle/oradata/stone1/users02.dbf'
3 size 10m
4 autoextend on next 5m maxsize unlimited;
Tablespace altered.
2、从表空间中删除数据文件
SQL> alter tablespace users
2 drop datafile '/ora/app/oracle/oradata/stone1/users02.dbf'
3 ;
Tablespace altered.
3、对数据文件的自动扩展设置
可以在alter database或者alter tablespace语句中进行设置
SQL> select tablespace_name,file_name,autoextensible from dba_data_files;
TABLESPACE FILE_NAME AUTOEXTEN
---------- -------------------------------------------------- ---------
USERS /ora/app/oracle/oradata/stone1/users01.dbf YES
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf YES
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf YES
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf YES
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf YES
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf NO
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf
NO
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf NO
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf NO
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf NO
10 rows selected.
SQL> alter database
2 datafile '/ora/app/oracle/oradata/stone1/datafile2.dbf'
3 autoextend on next 5m maxsize unlimited;
Database altered.
SQL> select tablespace_name,file_name,autoextensible from dba_data_files;
TABLESPACE FILE_NAME AUTOEXTEN
---------- -------------------------------------------------- ---------
USERS /ora/app/oracle/oradata/stone1/users01.dbf YES
UNDOTBS1 /ora/app/oracle/oradata/stone1/undotbs01.dbf YES
SYSAUX /ora/app/oracle/oradata/stone1/sysaux01.dbf YES
SYSTEM /ora/app/oracle/oradata/stone1/system01.dbf YES
EXAMPLE /ora/app/oracle/oradata/stone1/example01.dbf YES
TBS_TEST_1 /ora/app/oracle/oradata/stone1/datafile1.dbf NO
TBS_TEST_2 /ora/app/oracle/oradata/stone1/datafile2.dbf
YES
TBS_TEST_3 /ora/app/oracle/oradata/stone1/datafile3.dbf NO
TBS_TEST_4 /ora/app/oracle/oradata/stone1/datafile4.dbf NO
TBS_BIG /ora/app/oracle/oradata/stone1/datafilebig.dbf NO
10 rows selected.