Postgres使用序列更新表id

时间:2017-10-03 08:51:48

标签: postgresql

我正在尝试将用户表的序列连接到id字段的自动增量值。

我创建了以下序列,

CREATE SEQUENCE "USER_MGMT"."USER_SEQ"
    INCREMENT 1
    START 1000
    MINVALUE 1000
    MAXVALUE 99999999
    CACHE 1;

ALTER SEQUENCE "USER_MGMT"."USER_SEQ"
    OWNER TO postgres;

以下是我的表,

-- Table: "USER_MGMT"."USER"

-- DROP TABLE "USER_MGMT"."USER";

CREATE TABLE "USER_MGMT"."USER"
(
    "USER_ID" bigint NOT NULL,
    "FIRST_NAME" character varying(50) COLLATE pg_catalog."default" NOT NULL,
    "LAST_NAME" character varying(50) COLLATE pg_catalog."default" NOT NULL,
    "EMAIL_ID" character varying(100) COLLATE pg_catalog."default" NOT NULL,
    "DESK_NUMBER" bigint,
    "MOBILE_NUMBER" bigint,
    "IS_ACTIVE" boolean NOT NULL DEFAULT true,
    "CREATED_BY" character varying(100) COLLATE pg_catalog."default",
    "MODIFIED_BY" character varying(100) COLLATE pg_catalog."default",
    "DATE_CREATED" timestamp without time zone NOT NULL,
    "DATE_MODIFIED" timestamp without time zone,
    CONSTRAINT "USER_ID_PK" PRIMARY KEY ("USER_ID"),
    CONSTRAINT "EMAIL_ID_UK" UNIQUE ("EMAIL_ID"),
    CONSTRAINT "MOBILE_NUMBER_UK" UNIQUE ("MOBILE_NUMBER"),
    CONSTRAINT "USER_ID_UK" UNIQUE ("USER_ID")
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE "USER_MGMT"."USER"
    OWNER to postgres;

我想将此序列连接到USER_ID列,因此它将自动递增。

表名和字段应为大写,

我正在尝试执行以下查询,但它无效

ALTER TABLE USER_MGMT.USER ALTER COLUMN USER_ID SET DEFAULT nextval('USER_MGMT.USER_SEQ');

它在控制台中显示以下错误消息。

ERROR:  schema "user_mgmt" does not exist
********** Error **********

1 个答案:

答案 0 :(得分:1)

这是因为当您使用双引号时,您正在创建区分大小写对象标识符或更准确 - 此对象将具有在创建期间在查询中给出的精确大小写的标识符。如果你不加倍引用它们,那么它们将被转换为小写。

所以你需要的是停止使用双引号,以小写创建对象或在alter查询中使用双引号:

ALTER TABLE "USER_MGMT"."USER" ALTER COLUMN "USER_ID" SET DEFAULT nextval('"USER_MGMT"."USER_SEQ"');