SQL 如何更改 derby 数据库的列数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2385020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:36:11  来源:igfitidea点击:

How to alter a column datatype for derby database?

sqldatabasederby

提问by jl.

I am trying to alter a datatype for a derby db column. The current price column is set as DECIMAL(5,0). I would like to alter it to DECIMAL(7,2). I did this :

我正在尝试更改 derby db 列的数据类型。当前价格列设置为 DECIMAL(5,0)。我想将其更改为 DECIMAL(7,2)。我这样做了:

alter table item alter column price set data type DECIMAL(7,2);

But it did not work, and showing the error:

但它不起作用,并显示错误:

Error: Only columns of type VARCHAR may have their length altered. 

May I know how is it possible to alter it? Thank you.

我可以知道如何改变它吗?谢谢你。

回答by u?6??n? ???u?p

Here is the Derby SQL script to change column MY_TABLE.MY_COLUMN from BLOB(255) to BLOB(2147483647):

这是将列 MY_TABLE.MY_COLUMN 从 BLOB(255) 更改为 BLOB(2147483647) 的 Derby SQL 脚本:

ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BLOB(2147483647);
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;

回答by jr1964

I think you can do like this:

我认为你可以这样做:

ALTER TABLE SCHEMA.TABLE ALTER "COLUMN-NAME" SET DATA TYPE VARCHAR(255);

(column-Name SET DATA TYPE VARCHAR(integer))for Datatype String as an example...

(column-Name SET DATA TYPE VARCHAR(integer))以数据类型字符串为例...

回答by Aleks

You can alter table like this:

您可以像这样更改表:

ALTER TABLE [table] ALTER COLUMN [column] SET DATA TYPE [type];

Or in Rails, just use:

或者在Rails,只需使用:

change_column :table_name, :column_name, :integer

回答by Bryan Pendleton

Here's a slightly more complicated way to alter the column's data type in this fashion:

下面是一种稍微复杂一点的方法来以这种方式改变列的数据类型:

  1. Add a new column, of the desired data type
  2. Issue "update ... set new-column = old-column to copy the data from the old column to the new column
  3. drop the old column
  4. Rename the new column to have the name of the old column.
  1. 添加所需数据类型的新列
  2. 发出“update ... set new-column = old-column 将数据从旧列复制到新列
  3. 删除旧列
  4. 将新列重命名为旧列的名称。

Slightly more steps, but in the end the effect will be the same.

步骤稍微多一点,但最终效果是一样的。

If you have trouble working out the exact details of the SQL to do this, let us know and we'll help.

如果您在计算执行此操作的 SQL 的确切详细信息时遇到困难,请告诉我们,我们会提供帮助。

回答by Pavunkumar

Posgtes Solution :

邮政解决方案:

ALTER TABLE prices_table ALTER price_column TYPE decimal (7,2 )

ALTER TABLE price_table ALTER price_column TYPE 十进制 (7,2)