Derby 将空 table 的现有列更改为标识

Derby alter existing column of empty table to be identity

我正在尝试以下操作:

psAddPK = conn.prepareStatement("ALTER TABLE users ALTER usr    
                      GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)");        

psAddPK.execute(); 

但得到

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "GENERATED"

在创建 usr 时定义为 NOT NULL

您不能更改列以将其重新定义为标识,您必须从一开始就将其创建为标识列。或者您可以删除该列,然后将其重新添加为标识。

Here's the Derby Alter table spec

The ALTER TABLE statement allows you to:

  • add a column to a table
  • add a constraint to a table drop a column from a table
  • drop an existing constraint from a table
  • increase the width of a VARCHAR or VARCHAR FOR BIT DATA column
  • override row-level locking for the table (or drop the override)
  • change the increment value and start value of the identity column
  • change the nullability constraint for a column
  • change the default value for a column

语法:

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
]