db2中将表中的某一字段设置为自增长

DB2可以使用generated always as identity 将某一个字段指定为自增长的字段:

这表示id自动是一个自增长的字段,它从1开始增加每次增加1。也可以通过generated

将字段的值通过其他字段的计算的来,比如;

create table strategy
  (
  strategy_id decimal(17)
  GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1 )
  primary key not null,
  strategy_name varchar(200),
  area_code decimal(6,0)
  );
create table user(name varchar(30),
  n1 integer,
  n2 integer ,
  id integer generated always as (n1+n2))

还有另外一种是使用序列(SEQUENCE )

原文地址:https://www.cnblogs.com/heml/p/3365767.html