sql - DB2(v10.5.0.5) 如何将自增列添加到已存在的表中

标签 sql db2

我正在尝试在 DB2 的现有表中添加一个自动递增列。

DB2 版本是 v10.5.0.5

以下是我的查询:

alter table DB2INST1.AAA_BJ_BOND 
ADD COLUMN id INTEGER NOT NULL DEFAULT 0;

ALTER TABLE DB2INST1.AAA_BJ_BOND ALTER COLUMN id 
set generated always as identity (start with 1);

但是我得到了以下错误:

"com.ibm.db2.jcc.am.SqlSyntaxErrorException: ALTER TABLE "DB2INST1.AAA_BJ_BOND" 
specified attributes for column "ID" that are not compatible with the existing 
column.. SQLCODE=-190, SQLSTATE=42837, DRIVER=4.13.127"

我该怎么做才能解决这个问题?

最佳答案

您必须先删除列的默认值。 SQL0190N的描述中提到了这一点:

If SET GENERATED ALWAYS AS (expression) is specified, but the column is already defined with a form of generation (default, identity, or expression) and there is no corresponding DROP in the same statement.

ALTER TABLE DB2INST1.AAA_BJ_BOND 
ALTER COLUMN id drop default;

ALTER TABLE DB2INST1.AAA_BJ_BOND ALTER COLUMN id 
set generated always as identity (start with 1);

关于sql - DB2(v10.5.0.5) 如何将自增列添加到已存在的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54002166/

相关文章:

mysql - 为什么Mysql时间输出值显示为hhmmss而不是hh :mm:ss?

db2 - 我收到 "Operation timed out. ERRORCODE=-4499, SQLSTATE=08001"连接到远程 DB2

sql - 在更新级联时出现意外错误

db2 - 终止引发线

ubuntu - Ubuntu 上的 unixODBC 和 DB2 ODBC 连接问题

sql - 数据库不区分大小写索引?

MySQL : getting top 100 records for each category

php - 奇怪的 SQL 错误 IIS 7

c# - SQL插入一行或多行数据?

sql - 使用 AS MERGE 的存储过程不返回任何内容?