下面的例子将使用手动创建与注册依赖对象的方法来执行联机重定义操作,原始表创建如下:
SQL> create table jy.t1(c1 number); Table created SQL> create index jy.t1_idx_1 on jy.t1(c1); Index created
假设在联机重定义之后列c1变为了c2。在这种情况下,使用copy_table_dependents过程试图对中间表的c1列创建索引t1_idx,因为不存在列c1就会出现错误。因此必须在列c2上创建索引然后进行注册。
联机重定义操作如下:
1.用要执行联机重定义操作的用户登录数据库
SQL> conn jy/jy@jypdb Connected.
2.验证原始表t1是否可以执行联机重定义操作
SQL> begin 2 dbms_redefinition.can_redef_table( 3 uname => 'jy', 4 tname => 't1', 5 options_flag => DBMS_REDEFINITION.CONS_USE_ROWID); 6 end; 7 / PL/SQL procedure successfully completed.
3.手动创建中间表jy.int_t1并且在列c2上创建索引jy.int_t1_idx_1
SQL> create table jy.int_t1(c2 number); Table created. SQL> create index jy.int_t1_idx_1 on jy.int_t1(c2); Index created.
4.开始执行联机重定义操作
SQL> begin 2 dbms_redefinition.start_redef_table( 3 uname => 'jy', 4 orig_table => 't1', 5 int_table => 'int_t1', 6 col_mapping => 'c1 c2', 7 options_flag => DBMS_REDEFINITION.CONS_USE_ROWID); 8 end; 9 / PL/SQL procedure successfully completed.
5.注册原始(索引t1_idx_1)与中间(int_t1_idx_1)依赖对象
SQL> begin 2 dbms_redefinition.register_dependent_object( 3 uname => 'jy', 4 orig_table => 't1', 5 int_table => 'int_t1', 6 dep_type => DBMS_REDEFINITION.CONS_INDEX, 7 dep_owner => 'jy', 8 dep_orig_name => 't1_idx_1', 9 dep_int_name => 'int_t1_idx_1'); 10 end; 11 / PL/SQL procedure successfully completed.
6.复制依赖对象
SQL> declare 2 num_errors pls_integer; 3 begin 4 dbms_redefinition.copy_table_dependents( 5 uname => 'jy', 6 orig_table => 't1', 7 int_table => 'int_t1', 8 copy_indexes => DBMS_REDEFINITION.CONS_ORIG_PARAMS, 9 copy_triggers => TRUE, 10 copy_constraints => TRUE, 11 copy_privileges => TRUE, 12 ignore_errors => TRUE, 13 num_errors => num_errors); 14 end; 15 / PL/SQL procedure successfully completed.
7.可选操作同步中间表
SQL> begin 2 dbms_redefinition.sync_interim_table( 3 uname => 'jy', 4 orig_table => 't1', 5 int_table => 'int_t1'); 6 end; 7 / PL/SQL procedure successfully completed.
8.完成联机重定义操作
SQL> begin 2 dbms_redefinition.finish_redef_table( 3 uname => 'jy', 4 orig_table => 't1', 5 int_table => 'int_t1'); 6 end; 7 / PL/SQL procedure successfully completed.
SQL> select dbms_metadata.get_ddl(object_type =>'TABLE',name =>'T1',schema => 'JY') from dual; DBMS_METADATA.GET_DDL(OBJECT_TYPE=>'TABLE',NAME=>'T1',SCHEMA=>'JY') -------------------------------------------------------------------------------- CREATE TABLE "JY"."T1" ( "C2" NUMBER ) SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "TEST" 1 row selected.
可以看到表jy.t1已经成功能联机重定义
9.等待任何查询中间表的语句执行完成后将其删除
SQL> desc jy.t1 Name Type Nullable Default Comments ---- ------ -------- ------- -------- C2 NUMBER Y SQL> drop table jy.t1 purge; Table dropped
到此重定义操作就完成了。