Is there a way to decrease the column length in DB2?
是否有一种方法可以减少DB2中的列长度?
Say I have a table temp with column col1
defined as VARCHAR(80)
. I want to reduce it to VARCHAR(60)
.
假设有一个表temp,其中列col1定义为VARCHAR(80)。我想把它减少到VARCHAR(60)。
3 个解决方案
#1
19
In DB2 9.7 for Linux/UNIX/Windows, you can use the ALTER TABLE statement to reduce the length of a column, assuming that no values in the column exceed the new column size:
在Linux/UNIX/Windows的DB2 9.7中,您可以使用ALTER TABLE语句来减少列的长度,假设列中没有任何值超过新的列大小:
ALTER TABLE temp
ALTER COLUMN col1 SET DATA TYPE VARCHAR(60);
If any values in the column exceed the desired size you must handle that first.
如果列中的任何值超过所需的大小,则必须首先处理该值。
In previous versions of DB2 for Linux/UNIX/Windows, you could not utilize this method to reduce the size of the column. You either had to drop/recreate the table, or go through a process of adding a column, copying data, and removing the old column.
在以前的Linux/UNIX/Windows版本的DB2中,您不能使用这种方法来减少列的大小。您要么删除/重新创建表,要么进行添加列、复制数据和删除旧列的过程。
#2
3
As an addition to Ian's answer and Clockwork-Muse's remark:
作为伊恩的答案和时钟缪斯的评论的补充:
While it is possible, as Ian pointed out, to use ALTER
statements to reduce column length in DB for LUW, this is not the case in DB2 for z/OS as of version 10.
正如Ian所指出的,在LUW中使用ALTER语句来减少DB中的列长度是可能的,但是在DB2中,在z/OS版本10中就不是这样了。
According to this table, only data type changes from VARCHAR(n)
to VARCHAR(n+x)
are supported, which is a bummer.
根据该表,只支持从VARCHAR(n)到VARCHAR(n+x)的数据类型更改,这是一个令人不快的问题。
#3
1
You cannot reduce the length of a column. To achieve this affect you should
你不能减少一列的长度。为了达到这个效果,你应该
- create a new table with your data and with the attribute that you want.
- 用您的数据和您想要的属性创建一个新的表。
- Delete old table
- 删除旧表
- Rename the new table
- 重命名新表
If you want to increase the length, it is possible with ALTER
command
如果希望增加长度,可以使用ALTER命令
ALTER TABLE temp
ALTER COLUMN col1
SET DATA TYPE VARCHAR(60)
#1
19
In DB2 9.7 for Linux/UNIX/Windows, you can use the ALTER TABLE statement to reduce the length of a column, assuming that no values in the column exceed the new column size:
在Linux/UNIX/Windows的DB2 9.7中,您可以使用ALTER TABLE语句来减少列的长度,假设列中没有任何值超过新的列大小:
ALTER TABLE temp
ALTER COLUMN col1 SET DATA TYPE VARCHAR(60);
If any values in the column exceed the desired size you must handle that first.
如果列中的任何值超过所需的大小,则必须首先处理该值。
In previous versions of DB2 for Linux/UNIX/Windows, you could not utilize this method to reduce the size of the column. You either had to drop/recreate the table, or go through a process of adding a column, copying data, and removing the old column.
在以前的Linux/UNIX/Windows版本的DB2中,您不能使用这种方法来减少列的大小。您要么删除/重新创建表,要么进行添加列、复制数据和删除旧列的过程。
#2
3
As an addition to Ian's answer and Clockwork-Muse's remark:
作为伊恩的答案和时钟缪斯的评论的补充:
While it is possible, as Ian pointed out, to use ALTER
statements to reduce column length in DB for LUW, this is not the case in DB2 for z/OS as of version 10.
正如Ian所指出的,在LUW中使用ALTER语句来减少DB中的列长度是可能的,但是在DB2中,在z/OS版本10中就不是这样了。
According to this table, only data type changes from VARCHAR(n)
to VARCHAR(n+x)
are supported, which is a bummer.
根据该表,只支持从VARCHAR(n)到VARCHAR(n+x)的数据类型更改,这是一个令人不快的问题。
#3
1
You cannot reduce the length of a column. To achieve this affect you should
你不能减少一列的长度。为了达到这个效果,你应该
- create a new table with your data and with the attribute that you want.
- 用您的数据和您想要的属性创建一个新的表。
- Delete old table
- 删除旧表
- Rename the new table
- 重命名新表
If you want to increase the length, it is possible with ALTER
command
如果希望增加长度,可以使用ALTER命令
ALTER TABLE temp
ALTER COLUMN col1
SET DATA TYPE VARCHAR(60)