日期:2014-05-16  浏览次数:20478 次

Oracle 如何修改列不为空的时候的数据类型


–新增临时列 
alter table tablename add filedname_temp number(2);
–将临时列的值置空

update zyt set id_temp=null; -----#alter table tablename modify filedname null;
–将要更新的字段值挪到临时列,并置空该列
 update tablename set filedname_temp=filedname,filedname=null;
 commit;
 –修改列的数据类型为varchar2
 alter table tablename modify filedname varchar2(20);
 –将要临时列值重新挪到该列,并置空临时列
 update tablename set filedname=filedname_temp,filedname_temp=null;
 commit;
 –删除临时列
 alter table tablename drop column filedname_temp;
 –给该列不能为空
 alter table tablename modify filedname not null;
 –执行查询测试
 select * from tablename ;
 使用这种方式,既不用使列名发生变化,也不会发生表迁移,但有个缺点是表要更新两次,而且当如果数据量较大时,产生的undo和redo也更多,前提也是要停机才进行操作,如果不停机 ,也可以采用在线重定义方式来做。

注:请自行更换tablename和filedname为自己的实际值。