欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2022-05-06 08:53:36
...

新增临时列 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=file

  –新增临时列

  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为自己的实际值。