Psql not null错误但值不为空

时间:2016-04-02 02:35:16

标签: sql postgresql null

我正在尝试将数据从一个表加载到另一个表中,从all_address加载到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;

然而,每当我尝试通过

获取街道地址,城市和邮编时
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”列中的空值违反了非空约束

由于用户名是主键,我不明白为什么我收到错误,因为我确保使用用户名创建客户,因此错误说明用户名中没有空值。

任何和所有帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您收到错误是因为您未在第二个查询中提供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;