简体   繁体   English

在 DB2 中减少列长度的方法

[英]Way to decrease column length in DB2

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) .假设我有一个表 temp,其中列col1定义为VARCHAR(80) I want to reduce it to VARCHAR(60) .我想将它减少到VARCHAR(60)

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.在以前版本的 DB2 for Linux/UNIX/Windows 中,您不能使用这种方法来减小列的大小。 You either had to drop/recreate the table, or go through a process of adding a column, copying data, and removing the old column.您要么必须删除/重新创建表,要么经历添加列、复制数据和删除旧列的过程。

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)数据类型变化,这是一个无赖。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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