【问题标题】:PostgreSQL: for duplicating values for another user insert record from same table which has unique idPostgreSQL:用于从具有唯一 ID 的同一表中复制另一个用户插入记录的值
【发布时间】:2018-01-05 12:26:09
【问题描述】:

我有一个名为 customer_config 的表。我有四条记录。

id  customer_name   cust_id currency_code
1      test            1         (null)
2      alpha           1         (null)
3      beta            1         (null)
4      clone           1         (null)

id 列不应为空且唯一。 我想将所有这些记录克隆给另一个客户,即:cust_id =2; 我正在尝试以下查询,例如

INSERT INTO customer_config(cust_id, customer_name, currency_code)
SELECT 2, customer_name, currency_code
FROM customer_config
WHERE cust_id = 1;

这里我不能通过添加序列来引入/更新 customer_config 表。

我可以通过任何逻辑插入记录。

您的帮助将不胜感激

注意:

错误就像 17:55:38 [插入 - 0 行,0.583 秒] [代码:0,SQL 状态:23502] 错误:“id”列中的空值违反非空约束 详细信息:失败行包含 (4, null, sslProtocol, 1, SSL, null)。 ... 1 条语句执行,0 行受影响,执行/获取时间:0.583/0.000 秒 [0 成功,1 错误]

【问题讨论】:

    标签: sql database postgresql sql-insert


    【解决方案1】:

    您的id 列显然没有默认值。因此,您必须显式插入其值。如果您有此列的序列,您可以尝试:

    INSERT INTO customer_config(id, cust_id, customer_name, currency_code)
    SELECT NEXTVAL('id_sequence'), 2, customer_name, currency_code
    FROM customer_config
    WHERE cust_id = 1;
    

    其中id_sequence 是该序列的名称。

    【讨论】:

    • "2" 参数是多余的,因为您使用 NEXTVAL
    • @MaratSafin:不,这是给cust_id的。请参阅INTO 子句中的列列表。
    • Оh,对不起,我的错
    • 我不能为这个表引入新的序列。
    • @parasuramans:但是您需要为您的 id 列设置一个值。此表中的 id 来自哪里?
    猜你喜欢
    • 2015-11-26
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多