oracle 重定义表

摘自:http://blog.csdn.net/edcvf3/article/details/8721655


ORACLE数据库支持在线(不关库)修改表的各种物理属性和逻辑属性,包括责增加/更改/删除分区、更改表空间、增加列、删除列、将表改为IOT表等等。

个人觉得将未分区的表重定义为分区表是用的最多的,因为一些DB前期设计未考虑周全,很多大表到后期都需要分区。(对大表做在线重定义的时候,个人建议尽量少做基于原表的大的查询或大的DML操作)

现在来简单测试一下这个强大的功能:

 

[sql]  view plain copy print ?
  1. --实验环境  
  2.   
  3. 14:23:42 SCOTT@orcl> select * from v$version;  
  4.   
  5. BANNER  
  6. --------------------------------------------------------------------------------  
  7. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production  
  8. PL/SQL Release 11.2.0.1.0 - Production  
  9. CORE    11.2.0.1.0      Production  
  10. TNS for 32-bit Windows: Version 11.2.0.1.0 - Production  
  11. NLSRTL Version 11.2.0.1.0 - Production  
  12.   
  13. 已选择5行。  


[sql]  view plain copy print ?
  1.  --创建原始表  
  2.   
  3. 14:07:36 SCOTT@orcl> create table admin_emp(  
  4. 14:07:54   2  empno number(4),ename varchar2(10),job varchar2(9),deptno number(4));  
  5.   
  6. 表已创建。  
  7.   
  8. 已用时间:  00: 00: 01.01  


我们来使用主键的方式进行重定义:

  在线重定义支持按键和按ROWID方式重定义。其中按键可以是按主键或有唯一索引的键

[sql]  view plain copy print ?
  1. --给原始表加主键  
  2.   
  3. 14:13:53 SCOTT@orcl> alter table admin_emp add primary key(empno);  
  4.   
  5. 表已更改。  
  6.   
  7. 14:14:17 SCOTT@orcl> desc admin_emp  
  8.  名称                                                  是否为空? 类型  
  9.  ----------------------------------------------------- -------- -------------  
  10.   
  11.  EMPNO                                                 NOT NULL NUMBER(4)  
  12.  ENAME                                                          VARCHAR2(10)  
  13.  JOB                                                            VARCHAR2(9)  
  14.  DEPTNO                                                         NUMBER(4)  
[sql]  view plain copy print ?
  1. --验证表是否能被在线重定义  
  2. 14:14:26 SCOTT@orcl> CONN /AS SYSDBA  
  3. 已连接。  
  4. 14:14:39 SYS@orcl> BEGIN  
  5. 14:14:43   2    DBMS_REDEFINITION.CAN_REDEF_TABLE('SCOTT','admin_emp',  
  6. 14:14:43   3        DBMS_REDEFINITION.CONS_USE_PK);  
  7. 14:14:43   4  END;  
  8. 14:14:43   5  /  
  9.   
  10. PL/SQL 过程已成功完成。  
[sql]  view plain copy print ?
  1. 14:15:09 SYS@orcl> conn scott/tiger  
  2. 已连接。  
  3. 14:15:15 SCOTT@orcl> --创建中间表  
  4. 14:15:33 SCOTT@orcl> CREATE TABLE int_admin_emp  
  5. 14:16:44   2          (empno      NUMBER(5) PRIMARY KEY,  
  6. 14:16:44   3           ename      VARCHAR2(15) NOT NULL,  
  7. 14:16:44   4           job        VARCHAR2(10),  
  8. 14:16:44   5           mgr        NUMBER(5),  
  9. 14:16:44   6           hiredate   DATE DEFAULT (sysdate),  
  10. 14:16:44   7           sal        NUMBER(7,2),  
  11. 14:16:44   8           deptno     NUMBER(3) NOT NULL,  
  12. 14:16:44   9           bonus      NUMBER (7,2) DEFAULT(1000))  
  13. 14:16:44  10       PARTITION BY RANGE(empno)  
  14. 14:16:44  11         (PARTITION emp1000 VALUES LESS THAN (1000) TABLESPACE SCOTT_TBS,  
  15. 14:16:44  12          PARTITION emp2000 VALUES LESS THAN (2000) TABLESPACE USERS);  
  16.   
  17. 表已创建。  
  18.   
  19. 已用时间:  00: 00: 00.32  
  20. 14:16:46 SCOTT@orcl> --开始重定义处理  
  21. 14:17:04 SCOTT@orcl> conn /as sysdba  
  22. 已连接。  
  23. 14:17:25 SYS@orcl> BEGIN  
  24. 14:17:40   2    DBMS_REDEFINITION.START_REDEF_TABLE('SCOTT''admin_emp','int_admin_emp',  
  25. 14:17:40   3         'empno empno, ename ename, job job, deptno+10 deptno, 0 bonus',  
  26. 14:17:40   4          dbms_redefinition.cons_use_pk);  
  27. 14:17:40   5  END;  
  28. 14:17:40   6  /  
  29.   
  30. PL/SQL 过程已成功完成。  
  31.   
  32. 已用时间:  00: 00: 10.84  
  33. 14:17:52 SYS@orcl> --复制依赖对象  
  34. 14:18:29 SYS@orcl> DECLARE  
  35. 14:18:29   2  num_errors PLS_INTEGER;  
  36. 14:18:29   3  BEGIN  
  37. 14:18:29   4    DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS('SCOTT''admin_emp','int_admin_emp',  
  38. 14:18:29   5     DBMS_REDEFINITION.CONS_ORIG_PARAMS, TRUETRUETRUETRUE, num_errors);  
  39. 14:18:29   6  END;  
  40. 14:18:29   7  /  
  41.   
  42. PL/SQL 过程已成功完成。  


 

[sql]  view plain copy print ?
  1. 14:19:10 SYS@orcl> --同步中间表  
  2. 14:19:49 SYS@orcl> BEGIN  
  3. 14:19:49   2    DBMS_REDEFINITION.SYNC_INTERIM_TABLE('scott''admin_emp''int_admin_emp');  
  4. 14:19:49   3  END;  
  5. 14:19:49   4  /  
  6.   
  7. PL/SQL 过程已成功完成。  
  8.   
  9. 已用时间:  00: 00: 00.06  
  10. 14:19:50 SYS@orcl> --完成重定义  
  11. 14:20:13 SYS@orcl> BEGIN  
  12. 14:20:13   2    DBMS_REDEFINITION.FINISH_REDEF_TABLE('scott''admin_emp''int_admin_emp');  
  13. 14:20:13   3  END;  
  14. 14:20:13   4  /  
  15.   
  16. PL/SQL 过程已成功完成。  
  17.   
  18. 已用时间:  00: 00: 00.95  
  19. 14:20:17 SYS@orcl> conn scott/tiger  
  20. 已连接。  
  21. --发现表结构已经改变 表在线重定义成功  
  22. 14:20:26 SCOTT@orcl> desc admin_emp  
  23.  名称                                                  是否为空? 类型  
  24.  ----------------------------------------------------- -------- -------------------------------  
  25.   
  26.  EMPNO                                                 NOT NULL NUMBER(5)  
  27.  ENAME                                                 NOT NULL VARCHAR2(15)  
  28.  JOB                                                            VARCHAR2(10)  
  29.  MGR                                                            NUMBER(5)  
  30.  HIREDATE                                                       DATE  
  31.  SAL                                                            NUMBER(7,2)  
  32.  DEPTNO                                                NOT NULL NUMBER(3)  
  33.  BONUS                                                          NUMBER(7,2)  
  34.   
  35.   
  36. --查看是否已经重定义为分区表  
  37. 14:22:42 SCOTT@orcl> select * from admin_emp partition(emp1000);  
  38.   
  39. 未选定行  
  40.   
  41. 已用时间:  00: 00: 00.01  

--

注意:在线重定义表需要大量空闲空间。

         在线重定义表的时候可以做DML操作。

         从10gR2开始,Oracle支持单个分区表的在线重定义

一般使用包DBMS_REDEFINITION 或EM执行在线重定义。

附注, 在oracle 10g中, dbms_redefinition包增加了COPY_TABLE_DEPENDENTS, REGISTER_DEPENDENT_OBJECT, UNREGISTER_DEPENDENT_OBJECT三个存储过程, 分别用来复制, 注册, 注销表的依赖对象, 如索引, 约束, 触发器等. 并增加了DBA_REDEFINITION_ERRORS数据字典视图, 用来查看在线重定义过程中出现的错误.

OCP 题库考察此知识点:

Q9. Which statement describes the effect of table redefinition on the triggers attached to the table?


A. All triggers on the table are invalidated and are automatically revalidated(使重新生效) with the next DML execution on the table.
B. All triggers on the table are invalidated and must be manually recompiled before the next DML execution on the table.
C. All triggers on the table remain valid.
D. Only triggers that are affected by the changes to the structure of the table are invalidated and automatically revalidated with the next DML execution on the table.


Answer: A


Restrictions for Online Redefinition of Tables

The following restrictions apply to the online redefinition of tables:

  • If the table is to be redefined using primary key or pseudo-primary keys (unique keys or constraints with all component columns having not nullconstraints), then the post-redefinition table must have the same primary key or pseudo-primary key columns. If the table is to be redefined using rowids, then the table must not be an index-organized table.

  • After redefining a table that has a materialized view log, the subsequent refresh of any dependent materialized view must be a complete refresh.

  • Tables that are replicated in an n-way master configuration can be redefined, but horizontal subsetting (subset of rows in the table), vertical subsetting (subset of columns in the table), and column transformations are not allowed.

  • The overflow table of an index-organized table cannot be redefined online independently.

  • Tables with fine-grained access control (row-level security) cannot be redefined online.

  • Tables for which Flashback Data Archive is enabled cannot be redefined online. You cannot enable Flashback Data Archive for the interim table.

  • Tables with BFILE columns cannot be redefined online.

  • Tables with LONG columns can be redefined online, but those columns must be converted to CLOBS. Also, LONG RAW columns must be converted to BLOBS. Tables with LOB columns are acceptable.

  • On a system with sufficient resources for parallel execution, and in the case where the interim table is not partitioned, redefinition of a LONG column to aLOB column can be executed in parallel, provided that:

    • The segment used to store the LOB column in the interim table belongs to a locally managed tablespace with Automatic Segment Space Management (ASSM) enabled.

    • There is a simple mapping from one LONG column to one LOB column, and the interim table has only one LOB column.

    In the case where the interim table is partitioned, the normal methods for parallel execution for partitioning apply.

  • Tables in the SYS and SYSTEM schema cannot be redefined online.

  • Temporary tables cannot be redefined.

  • A subset of rows in the table cannot be redefined.

  • Only simple deterministic expressions, sequences, and SYSDATE can be used when mapping the columns in the interim table to those of the original table. For example, subqueries are not allowed.

  • If new columns are being added as part of the redefinition and there are no column mappings for these columns, then they must not be declared NOTNULL until the redefinition is complete.

  • There cannot be any referential constraints between the table being redefined and the interim table.

  • Table redefinition cannot be done NOLOGGING.

  • For materialized view logs and queue tables, online redefinition is restricted to changes in physical properties. No horizontal or vertical subsetting is permitted, nor are any column transformations. The only valid value for the column mapping string is NULL.

  • You cannot perform online redefinition on a table that is partitioned if the table includes one or more nested tables.

  • You can convert a VARRAY to a nested table with the CAST operator in the column mapping. However, you cannot convert a nested table to a VARRAY.


你可能感兴趣的:(oracle,重定义表)