欢迎光临
我们一直在努力

oracle 11g在线重定义 普通表变分区表

第一种方法

    使用主键id进行重定义

–create user test identified by 1 account unlock; 

–grant resource 

–grant create any table, alter any table, drop any table, lock any table, select any table to test; 

–<1> 创建测试表,以下使用在线重定义把表转换为分区表,created为分区键, object_id 为主键

drop table test01 purge;

create table test01 as select * from dba_objects where object_id is not null;

alter table test01 add primary key(object_id); 

select * from test01;

–<2> 创建分区表

create table test01_new 

partition by range(created)

–INTERVAL(NUMTOYMINTERVAL(1,’month’))

interval(numtodsinterval(1,’day’))

store in (users)

(

  partition p0 values less than (to_date(‘2008-01-01′,’yyyy-mm-dd’) )

as select * from test01 where created is not null and 1!=1;

alter table test01_new add primary key(object_id); 

–判断目标表是否可以使用主键进行在线重定义,也可以使用rowid

exec dbms_redefinition.can_redef_table(‘TEST’, ‘TEST01’, dbms_redefinition.cons_use_pk);

–把用户test的test01表的定义改为test01_new表的定义

exec dbms_redefinition.start_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–将中间表与原始表同步。(仅当要对表 TEST01 进行更新时才需要执行该操作。)

exec dbms_redefinition.sync_interim_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–结束重定义表

exec dbms_redefinition.finish_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–如果重定义失败,解锁

exec dbms_redefinition.abort_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–优点:

–保证数据的一致性,在大部分时间内,表T都可以正常进行DML操作。

–只在切换的瞬间锁表,具有很高的可用性。这种方法具有很强的灵活性,对各种不同的需要都能满足。

–而且,可以在切换前进行相应的授权并建立各种约束,可以做到切换完成后不再需要任何额外的管理操作。

–不足:实现上比上面两种略显复杂,适用于各种情况。

–然而,在线表格重定义也不是完美无缺的。下面列出了Oracle9i重定义过程的部分限制:

–你必须有足以维护两份表格拷贝的空间。   

–你不能更改主键栏。   

–表格必须有主键。   

–必须在同一个大纲中进行表格重定义。   

–在重定义操作完成之前,你不能对新加栏加以NOT NULL约束。   

–表格不能包含LONG、BFILE以及用户类型(UDT)。   

–不能重定义链表(clustered tables)。   

–不能在SYS和SYSTEM大纲中重定义表格。   

–不能用具体化视图日志(materialized VIEW logs)来重定义表格;不能重定义含有具体化视图的表格。   

–不能在重定义过程中进行横向分集(horizontal subsetting)

第二种方法

使用rowid进行重定义,一般在没有主键的情况下使用

–其他如上

–判断目标表是否可以使用主键进行在线重定义,也可以使用rowid

exec dbms_redefinition.can_redef_table(‘TEST’, ‘TEST01’, dbms_redefinition.cons_use_rowid);

–把用户test的test01表的定义改为test01_new表的定义

exec dbms_redefinition.start_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’,null,2);

–将中间表与原始表同步。(仅当要对表 TEST01 进行更新时才需要执行该操作。)

exec dbms_redefinition.sync_interim_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–结束重定义表

exec dbms_redefinition.finish_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

–如果重定义失败,解锁

exec dbms_redefinition.abort_redef_table(‘TEST’, ‘TEST01’, ‘TEST01_NEW’);

第三种方法

–<3>重命名表

alter table test01  rename to test01_bak;

alter table test01_bak rename to test01 ;

–<4>检查是否缺漏数据,补齐数据

select * from test01 t1

where up_time is not null –索引键不能有空数据,否则插入失败

  and not exists (select 1 from test01  t2 where t1.id = t2.id )

insert into test01 

select * from test01_bak  t1

where up_time is not null –索引键不能有空数据,否则插入失败

  and not exists (select 1 from test01  t2 where t1.id = t2.id )

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。