表在线重定义(无主键)

联系:手机/微信(+86 17813235971) QQ(107644445)

标题:表在线重定义(无主键)

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

1、T2表结构

SQL> desc t2
Name           Type          Nullable Default Comments
-------------- ------------- -------- ------- --------
OWNER          VARCHAR2(30)  Y
OBJECT_NAME    VARCHAR2(128) Y
SUBOBJECT_NAME VARCHAR2(30)  Y
OBJECT_ID      NUMBER        Y
DATA_OBJECT_ID NUMBER        Y
OBJECT_TYPE    VARCHAR2(19)  Y
CREATED        DATE          Y
LAST_DDL_TIME  DATE          Y
TIMESTAMP      VARCHAR2(19)  Y
STATUS         VARCHAR2(7)   Y
TEMPORARY      VARCHAR2(1)   Y
GENERATED      VARCHAR2(1)   Y
SECONDARY      VARCHAR2(1)   Y

2、创建中间表

CREATE TABLE T2_1
AS
SELECT * FROM t2 WHERE 1=0;

3、验证T2是否用于重定义(因没有主键,采用rowid实现)

EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE(user, 'T2', DBMS_REDEFINITION.cons_use_rowid);

4、执行表的在线重定义

EXEC DBMS_REDEFINITION.START_REDEF_TABLE(USER, 'T2', 'T2_1','OWNER OWNER, OBJECT_NAME OBJECT_NAME, SUBOBJECT_NAME  SUBOBJECT_NAME, OBJECT_ID OBJECT_ID, DATA_OBJECT_ID DATA_OBJECT_ID, OBJECT_TYPE OBJECT_TYPE, CREATED CREATED, LAST_DDL_TIME LAST_DDL_TIME, TIMESTAMP TIMESTAMP, STATUS STATUS, TEMPORARY TEMPORARY, GENERATED GENERATED, SECONDARY SECONDARY',DBMS_REDEFINITION.cons_use_rowid);

说明:
1)采用单引号列出T2与T2_1表列的对应关系
2)列与列之间采用单引号分割,单引号后面要有空格
5、同步数据(可选)

exec dbms_redefinition.sync_interim_table(user, 'T2', 'T2_1');

6、执行结束在线定义过程

EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE(user, 'T2', 'T2_1');

7、删除中间表

drop table t2_1 purge;

8、处理T2表(删除隐藏列)

SQL> select col#,name,type# from SYS.COL$ WHERE OBJ#=(select object_id from dba_objects where object_name='T2');
      COL# NAME                                TYPE#
---------- ------------------------------ ----------
         1 OWNER                                   1
         2 OBJECT_NAME                             1
         3 SUBOBJECT_NAME                          1
         4 OBJECT_ID                               2
         5 DATA_OBJECT_ID                          2
         6 OBJECT_TYPE                             1
         7 CREATED                                12
         8 LAST_DDL_TIME                          12
         9 TIMESTAMP                               1
        10 STATUS                                  1
        11 TEMPORARY                               1
        12 GENERATED                               1
        13 SECONDARY                               1
         0 SYS_C00014_11081015:39:40$              1
--发现一个多余隐藏列SYS_C00014_11081015:39:40$,我们需要删除
SQL>  alter table t2 set unused ("SYS_C00014_11081015:39:40$");
Table altered
SQL>  alter table t2 drop unused columns;
Table altered

表在线重定义(有主键)

One thought on “表在线重定义(无主键)

  1. 分飞,拜读大作,很详细,谢谢。
    测试时,发现user需要加引号,才能正常执行。

  2. 分飞,拜读大作,很详细,谢谢。
    测试时,发现user需要加引号,才能正常执行。

    user表示当前用户,如果你加入了引号,表示特定的字符串

    SQL> show user;
    USER is "PNJHJ"
    SQL> select user from dual;
    USER
    ------------------------------
    PNJHJ
    

发表评论

邮箱地址不会被公开。 必填项已用*标注

19 − 4 =