【问题标题】:Psql not null error but value isn't nullPsql 不为空错误但值不为空
【发布时间】:2016-04-02 02:35:15
【问题描述】:

我正在尝试将数据从一个表加载到另一个表,从 all_address 加载到客户。

我这样成功地插入到客户表中

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: 错误:“用户名”列中的空值违反非空约束

由于用户名是主键,我不明白为什么会出现错误,因为我已确保使用用户名创建客户,因此用户名中没有错误所述的空值。

感谢您的任何帮助!

【问题讨论】:

    标签: sql postgresql null


    【解决方案1】:

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

    【讨论】:

    • psql 的语法有点不对劲,需要做 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; 但这行得通,谢谢!
    • @AnnaGoldberg 。 . .将列名留在第一行是我的一个编辑错误。我很高兴你很容易抓住它。
    猜你喜欢
    • 1970-01-01
    • 2015-11-07
    • 2014-01-30
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2020-08-12
    • 1970-01-01
    相关资源
    最近更新 更多