我有以下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
在您的查询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将执行以下操作:
你可以获得更多信息here
您可以创建序列或创建具有串行类型的id列。