简体   繁体   中英

How to alter a column datatype for derby database?

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.

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;

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

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.

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

Posgtes Solution :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM