PostgreSQL设置自增字段

kyaa111 6月前 ⋅ 250 阅读

postgres 自增需要先进行创建自增序列, 指定自增值, 最小值, 最大值:

CREATE SEQUENCE "public"."tt_id_seq" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;

设置序列所有者:

ALTER TABLE "public"."tt_id_seq" OWNER TO "postgres";

设置自增从1开始:

SELECT setval('"public"."tt_id_seq"', 1, false);

自增字段添加默认建立连接:

nextval('tt_id_seq'::regclass)

CREATE TABLE "public"."test" (
  "id" int4 NOT NULL DEFAULT nextval('tt_id_seq'::regclass),
  "name" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
  PRIMARY KEY ("id")
)