温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-28 23:21:27 来源:网络 阅读:1107 作者:emperor_majesty 栏目:关系型数据库

第一种方法

    使用主键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 )


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI