gpt4 book ai didi

postgresql - 如何用postgresql模拟 "insert ignore"和 "on duplicate key update"(sql合并)?

转载 作者:太空狗 更新时间:2023-10-30 01:37:01 31 4
gpt4 key购买 nike

某些 SQL 服务器具有一项功能,如果 INSERT 违反主键/唯一键约束,则跳过该功能。例如,MySQL 有 INSERT IGNORE

用 PostgreSQL 模拟 INSERT IGNOREON DUPLICATE KEY UPDATE 的最佳方法是什么?

最佳答案

对于 PostgreSQL 9.5,现在是 native functionality (像 MySQL has had 好几年了):

INSERT ... ON CONFLICT DO NOTHING/UPDATE ("UPSERT")

9.5 brings support for "UPSERT" operations. INSERT is extended to accept an ON CONFLICT DO UPDATE/IGNORE clause. This clause specifies an alternative action to take in the event of a would-be duplicate violation.

...

Further example of new syntax:

INSERT INTO user_logins (username, logins)
VALUES ('Naomi',1),('James',1)
ON CONFLICT (username)
DO UPDATE SET logins = user_logins.logins + EXCLUDED.logins;

关于postgresql - 如何用postgresql模拟 "insert ignore"和 "on duplicate key update"(sql合并)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1009584/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com