我正在尝试以下操作:
psAddPK = conn.prepareStatement("ALTER TABLE users ALTER usr
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)");
psAddPK.execute();
但是得到
java.sql.SQLSyntaxErrorException:语法错误:遇到“ GENERATED”
在创建usr时定义了NOT NULL
答案 0 :(得分:2)
您不能更改该列以将其重新定义为身份,必须从头开始将其创建为身份列。或者,您可以删除该列,然后将其重新添加为身份。
Here's the Derby Alter table spec
ALTER TABLE语句使您可以:
- 在表中添加列
- 向表添加约束 从表中删除列
- 从表中删除现有约束
- 增加VARCHAR或VARCHAR FOR BIT DATA列的宽度
- 覆盖表的行级锁定(或删除覆盖)
- 更改标识列的增量值和起始值
- 更改列的可空性约束
- 更改列的默认值
语法:
ALTER TABLE table-name
{
ADD COLUMN column-definition |
ADD CONSTRAINT clause |
DROP [ COLUMN ] column-name [ CASCADE | RESTRICT ]
DROP { PRIMARY KEY | FOREIGN KEY constraint-name | UNIQUE
constraint-name | CHECK constraint-name | CONSTRAINT constraint-name }
ALTER [ COLUMN ] column-alteration |
LOCKSIZE { ROW | TABLE }
}
列更改
column-name SET DATA TYPE VARCHAR(integer) |
column-name SET DATA TYPE VARCHAR FOR BIT DATA(integer) |
column-name SET INCREMENT BY integer-constant |
column-name RESTART WITH integer-constant |
column-name [ NOT ] NULL |
column-name [ WITH | SET ] DEFAULT default-value |
column-name DROP DEFAULT
列定义
simple-column-name [ data-type ]
[ column-level-constraint ]*
[ [ WITH ] DEFAULT default-constant-expression
| generation-clause
]