UNDO数据也被称为回滚(ROLLBACK)数据,它用于确保数据的一致性。
1、undo tablespace的作用
用来存放从datafiles读出的数据块旧的镜像。
1)回退事务:当用户执行DML操作修改数据时,UNDO数据被存放到UNDO段,而新数据被放到数据段里。
2)读一致性:用户检索数据库数据时,Oracle总是使用户只能看到被提交的数据(读取提交)或特定时间点的数据(select 语句的时间点),这样可以确保数据的一致性。
3)事务恢复:事务恢复是实例恢复的一部分,它是由Oracle   server自动完成的。
4)倒叙查询(Flashback  Query):倒叙查询用于取得特定时间点的数据库数据。
2、undo的管理模式
1)manaual 手工管理:roll segment
2)auto 自动管理:undo tablespace (init parameter :undo_management  = auto) ,自动创建undo段
 
3、undo表空间管理
(1)建立新的undo表空间(处于active状态的undo tablespace 不能offline 和 drop),可以建立多个undo表空间,但一个时刻只有一个处于active。
 SQL> create undo tablespace
   2  undotbs datafile '/u01/app/oracle/oradata/anny/undotbs01.dbf' size 100m
   3  autoextend on next 10m maxsize 500m;
 
 Tablespace created.
 
 SQL> select tablespace_name,status,contents from dba_tablespaces;
 
 TABLESPACE STATUS                      CONTENTS
 ---------- --------------------------- ---------------------------
 SYSTEM     ONLINE                      PERMANENT
 RTBS       ONLINE                      UNDO
 SYSAUX     ONLINE                      PERMANENT
 TEMP       ONLINE                      TEMPORARY
 USERS      ONLINE                      PERMANENT
 TEXT       ONLINE                      PERMANENT
 LX01       ONLINE                      PERMANENT
 UNDOTBS    ONLINE                      UNDO
 
 8 rows selected.
    (2)查看当前正在使用的undo tablespace
     
 SQL> show parameter undo
 
 NAME                                 TYPE                              VALUE
 ------------------------------------ --------------------------------- ------------------------------
 undo_management                      string                            AUTO
 undo_retention                       integer                           900
 undo_tablespace                      string                            rtbs
    
   (3)切换undo
 
 SQL> alter system set undo_tablespace=undotbs;
 
 System altered.
 
 SQL> show parameter undo
 
 NAME                                 TYPE                              VALUE
 ------------------------------------ --------------------------------- ------------------------------
 undo_management                      string                            AUTO
 undo_retention                       integer                           900
 undo_tablespace                      string                            UNDOTBS
 
   (4)删除undo tablespace
   
 SQL> drop tablespace rtbs including contents and datafiles;
 
 Tablespace dropped.
 
4、查看UNDO参数
 SQL> show parameter undo
 
 NAME                                 TYPE                              VALUE
 ------------------------------------ --------------------------------- ------------------------------
 undo_management                      string                            AUTO
 undo_retention                       integer                           900
 undo_tablespace                      string                            UNDOTBS
(1)UNDO_MANAGEMENT
用于指定UNDO数据的管理方式。
如果是AUTO,则采用表空间方式;如果是manual则采用undo (回滚)segment。
09:55:48 SQL> show parameter undo
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS1
09:55:58 SQL>
(2)UNDO_TABLESPACE
指定实例所要使用的 UNDO表空间
如果没有指定回滚段,则采用system 回滚段。
在使用RAC机构时,因为一个UNDO表空间不能由多个实例同时使用,所以必须为每个实例配置一个独立的UNDO表空间。
(3)UNDO_RETENTION
控制UNDO数据的最大保留时间,默认为900秒。
设置当事务提交后,undo 数据块在undo tablespace 空间未使用完之前的的保留时间。 
 SQL> alter system set undo_retention=600;
 SQL>Alter tablespace undotbs1 retention  guarantee;(保证在retention 期间不允许被覆盖)
 SQL> Alter tablespace undotbs1 retention noguarantee;(想禁止undo表空间retention guarantee)
 
(4)undo 表空间的大小
         1)undo_retention
         2) undo block /per second 
         3) UNDO BLOCK SIZE