我正在尝试在 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/