+-
错误:关系“student_id_seq”不存在SQL状态:42P01

我有以下SQL代码:

-- Table: public.student

-- DROP TABLE public.student;

CREATE TABLE public.student
(

   id integer NOT NULL DEFAULT nextval('student_id_seq'::regclass),
   country character varying(30) COLLATE pg_catalog."default" NOT NULL,
   email character varying(30) COLLATE pg_catalog."default" NOT NULL,
   first_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
   last_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
   section character varying(255) COLLATE pg_catalog."default" NOT NULL,
   sex character varying(255) COLLATE pg_catalog."default" NOT NULL,
   CONSTRAINT student_pkey PRIMARY KEY (id)
)
WITH (
  OIDS = FALSE
) 
TABLESPACE pg_default;

ALTER TABLE public.student
OWNER to postgres;

我的查询没有执行

错误:关系“student_id_seq”不存在SQL状态:42P01

1
投票

在您的查询id integer NOT NULL DEFAULT nextval('student_id_seq'::regclass)中,表示您希望您的表获取Sequence student_id_seq中的下一个值存储,以获取列id的默认值。

试试这个:

CREATE TABLE public.student (
id serial not null,
country character varying(30) COLLATE pg_catalog."default" NOT NULL,
email character varying(30) COLLATE pg_catalog."default" NOT NULL,
first_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
last_name character varying(255) COLLATE pg_catalog."default" NOT NULL,
section character varying(255) COLLATE pg_catalog."default" NOT NULL,
sex character varying(255) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT student_pkey PRIMARY KEY (id)
) WITH ( OIDS = FALSE ) TABLESPACE pg_default;

ALTER TABLE public.student OWNER to postgres;

通过将SERIAL伪类型分配给id列,PostgreSQL将执行以下操作:

创建序列对象,并将序列生成的下一个值设置为列的默认值。 将序列的所有者分配给id列;因此,删除id列或表时将删除序列对象

你可以获得更多信息here

0
投票

您可以创建序列或创建具有串行类型的id列。