How to alter a column datatype for derby database?

ⅰ亾dé卋堺 提交于 2019-12-12 07:17:40

问题


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 :

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.


回答1:


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

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;



回答2:


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...




回答3:


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.

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.




回答4:


You can alter table like this:

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

Or in Rails, just use:

change_column :table_name, :column_name, :integer



回答5:


Posgtes Solution :

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



来源:https://stackoverflow.com/questions/2385020/how-to-alter-a-column-datatype-for-derby-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!