drop table foo; create table foo (col_name varchar2(5)); insert into foo values('1'); insert into foo values('12'); insert into foo values('33445'); commit; create index idx1 on foo (col_name); select * from foo; desc foo
根据当前表结构建立新表,修改新表字段,将原数据灌进来,删除旧表,将新表rename为旧表名 sql
create table new as select * from foo where 1=2; alter table new modify (col_name number(5)); insert into new select * from foo; drop table foo; rename new to foo;
若是原表有索引,触发器、外键等须要新建。 oracle
create table new as select [其余的列], to_number(Char_Col) col_name from foo;
drop table foo; rename new to foo;与方法1同样要注意新建相关对象。
alter table foo add(tmp_col number(5)); update foo set tmp_col = to_number(col_name); update foo set col_name = null; --此处要当心啊,原数据全没了,必定要保证上一步正确执行 alter table foo modify (col_name number(5)); update foo set col_name = tmp_col; alter table foo drop column tmp_col;