Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2824138
  • 博文数量: 200
  • 博客积分: 2413
  • 博客等级: 大尉
  • 技术积分: 3067
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-01 22:07
文章分类

全部博文(200)

文章存档

2018年(2)

2017年(8)

2016年(35)

2015年(14)

2014年(20)

2013年(24)

2012年(53)

2011年(44)

分类: Oracle

2012-04-27 11:27:15

工作中,经常碰到某列宽度不够,数据插不进去的情况。
解决办法:
直接修改列的宽度
 alter table test modify (type varchar2(10));

由于列中存在数据,会报错
ORA-01439:column to be modified must be empty to change datetype  

想办法解决:
办法1:清空列中数据,然后再执行上面的语句。适用于数据量大。
在表上加一个临时列,用于存放原列中的数据。
SQL> desc test
 Name                                                           Null?    Type
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 ID                                                                NUMBER
 BYTES_BEGIN                                                            NUMBER
 BYTES_END                                                            NUMBER
 BYTES                                                                NUMBER
 TIME                                                                DATE
 TYPE                                                                VARCHAR2(5)

加tmp_type列,存放type列的数据
SQL> alter table test add(tmp_type varchar2(5));

type列的数据复制给tmp_type列
SQL> update test set TMP_TYPE=TYPE;
commit;

清空type列
SQL> update test set type=null;
commit;

修改type列宽度,由原来VARCHAR2(5)改为varchar2(10);    
SQL> alter table test modify (type varchar2(10));

数据移回
update test set type=tmp_type;
commit;

删除tmp_type列
alter table test drop column tmp_type;

---------


办法2:创建新表,使用于数据量不大。
create table test_tmp as select * from test where 1=2;

修改test_tmp列宽度
alter table test_tmp modify (type varchar2(10));

test数据复制到test_tmp中
insert into test_tmp select * from test;

test表rename,作为备份
alter table test rename to test_bak;

test_tmp表rename成test表
alter table test_tmp rename to test;
阅读(3117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~