我正在尝试将数据从一个表加载到另一个表,从 all_address 加载到 customer。
我这样成功插入到customer表中
INSERT INTO Customer(username, password, customer_email, first_name,
last_name, middle_initial)
SELECT username, password, customer_email, first_name,
last_name, middle_initial
FROM all_user;
但是每当我尝试通过以下方式获取街道地址、城市和 zip 时
INSERT INTO Customer(street_address, city, zip_code)
SELECT temp.street_address, temp.city,
temp.zip_code
FROM all_address as temp, Customer as final
WHERE temp.customer_email = final.customer_email;
我收到一个错误:
psql:manip.sql:82: 错误:“用户名”列中的空值违反了非空约束
由于用户名是主键,我不明白为什么会出现错误,因为我已确保使用用户名创建客户,因此用户名中没有空值,如错误所述。
感谢所有帮助!
最佳答案
您收到错误是因为您没有在第二个查询中提供 username
值。可能没有默认值,也没有设置该值的机制,因此它默认为 NULL
。
您需要提供一个值。您可能打算进行 update
而不是 insert
:
update Customer c
set street_address = aa.street_address,
city = aa.city,
zip_code = aa.zip_code
from all_address aa
where aa.customer_email = c.customer_email;
关于sql - Psql not null 错误但值不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368540/