gpt4 book ai didi

sql - Postgresql-Insert值出现非空冲突

转载 作者:行者123 更新时间:2023-12-03 08:41:33 25 4
gpt4 key购买 nike

insert into "StarSchema"."Customer" 
(address,address2,district,postal_code,phone)
select address,address2,district,postal_code,phone
from public.address;

我试图将另一个表中的值插入到我刚刚创建的表中。但是我不知道为什么会有非null的冲突。由于此查询不包含(customer_id)这是主键。而且我没有在模式要求非null中定义任何值。地址2中有一些值,而postal_code为null。当我在查询中删除这两列时。一切正常。有什么方法可以将所有列都放入新表中?
    CREATE TABLE "StarSchema"."Customer"
(
first_name character varying COLLATE pg_catalog."default",
last_name character varying COLLATE pg_catalog."default",
activebool boolean,
create_date date,
address character varying COLLATE pg_catalog."default",
address2 character varying COLLATE pg_catalog."default",
district character varying COLLATE pg_catalog."default",
postal_code character varying COLLATE pg_catalog."default",
city character varying COLLATE pg_catalog."default",
country character varying COLLATE pg_catalog."default",
phone character varying COLLATE pg_catalog."default",
customer_id integer NOT NULL,
email character varying COLLATE pg_catalog."default",
active integer,
CONSTRAINT "Customer_pkey" PRIMARY KEY (customer_id)
)

问题中的错误信息是
错误:空值违反字段“customer_id”中的非空约束

enter image description here

最佳答案

大概是想让customer_id自动分配。如果是这样,则将其定义为:

customer_id integer generated always as identity primary key,

或在较旧的Postgres版本中:
customer_id serial primary key,

附带说明一下,我通常将主键设为表的第一列,而不是最后一列。

关于sql - Postgresql-Insert值出现非空冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61054028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com