SQL 在 DB2 中减少列长度的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9579075/
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
Way to decrease column length in DB2
提问by Vicky
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)
.
回答by Ian Bjorhovde
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:
在 DB2 9.7 for Linux/UNIX/Windows 中,您可以使用 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.
在以前版本的 DB2 for Linux/UNIX/Windows 中,您不能使用这种方法来减小列的大小。您要么必须删除/重新创建表,要么经历添加列、复制数据和删除旧列的过程。
回答by GreenhouseVeg
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 指出的那样,虽然可以使用ALTER
语句来减少 DB for LUW 中的列长度,但在 DB2 for 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)
,这是一个无赖。
回答by Kayser
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)