Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgresql主键中生成更新幂等元?_Sql_Postgresql - Fatal编程技术网

如何在postgresql主键中生成更新幂等元?

如何在postgresql主键中生成更新幂等元?,sql,postgresql,Sql,Postgresql,我有一个错误: SQL错误[23505]:错误:重复的键值违反唯一约束“name_pkey” 详细信息:键(状态id,类型)=(0,1)已存在 执行此更新脚本时: update schema.table_name set type = '1' where status_id in ('AT', '0', '1',); 主键位于(status\u id,type),这意味着您已经有了以下一对或多对的行: AT 1 0 1 1 1 所以,更新不起作用。您可以

我有一个错误:

SQL错误[23505]:错误:重复的键值违反唯一约束“name_pkey” 详细信息:键(状态id,类型)=(0,1)已存在

执行此更新脚本时:

update schema.table_name
    set type = '1'
    where status_id in ('AT', '0', '1',);

主键位于
(status\u id,type)
,这意味着您已经有了以下一对或多对的行:

AT    1
0     1
1     1
所以,更新不起作用。您可以通过过滤掉潜在冲突来忽略密钥:

update schema.table_name t
    set type = '1'
    from (values ('AT'), ('0'), ('1')) v(status_id)
    where t.status_id = v.status_id and
          not exists (select 1
                      from schema.table_name t2
                      where t2.status_id = t.status_id and t2.type = '1'
                     );

这只会忽略可能发生的更新。

表中已经有一行状态为0且类型为1,并且您有一个唯一的键,禁止重复。您想如何处理此问题?
UPDATE
不支持冲突的
@a_horse\u with_no\u name。谢谢,谢谢。我知道——没有数据库支持它。但这真的很有用!非常感谢你!