如何使用表中的现有行修改 Oracle 中的数据类型 [英] How to modify data type in Oracle with existing rows in table

查看:21
本文介绍了如何使用表中的现有行修改 Oracle 中的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不删除表数据的情况下将列的数据类型从 number 更改为 varchar2?

How can I change DATA TYPE of a column from number to varchar2 without deleting the table data?

推荐答案

你不能.

但是,您可以使用新数据类型创建新列、迁移数据、删除旧列并重命名新列.类似的东西

You can, however, create a new column with the new data type, migrate the data, drop the old column, and rename the new column. Something like

ALTER TABLE table_name
  ADD( new_column_name varchar2(10) );

UPDATE table_name
   SET new_column_name = to_char(old_column_name, <<some format>>);

ALTER TABLE table_name
 DROP COLUMN old_column_name;

ALTER TABLE table_name
 RENAME COLUMN new_column_name TO old_coulumn_name;

如果你的代码依赖于表中列的位置(你真的不应该有),你可以重命名表并使用表的原始名称在表上创建一个视图,以公开按照您的代码预期的顺序排列列,直到您可以修复该错误代码.

If you have code that depends on the position of the column in the table (which you really shouldn't have), you could rename the table and create a view on the table with the original name of the table that exposes the columns in the order your code expects until you can fix that buggy code.

这篇关于如何使用表中的现有行修改 Oracle 中的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆