【发布时间】:2018-05-26 23:27:11
【问题描述】:
我正在尝试将 Oracle 合并查询迁移到 PostgreSql。如this 文章中所述,Postgres UPSERT 语法支持“where 子句”来识别冲突条件。
很遗憾,该网页没有提供带有“where 子句”的示例。我尝试在其他地方搜索它,但找不到它。因此提出了这个问题。
按照上面给出的网页中的相同示例,这里是一个示例设置:
CREATE TABLE customers (
customer_id serial PRIMARY KEY,
name VARCHAR UNIQUE,
email VARCHAR NOT NULL,
active bool NOT NULL DEFAULT TRUE
);
INSERT INTO customers (NAME, email) VALUES
('IBM', 'contact@ibm.com'),
('Microsoft', 'contact@microsoft.com'),
('Intel','contact@intel.com');
SELECT * FROM customers;
customer_id | name | email | active
-------------+-----------+-----------------------+--------
1 | IBM | contact@ibm.com | t
2 | Microsoft | contact@microsoft.com | t
3 | Intel | contact@intel.com | t
(3 rows)
我希望我的 UPSERT 语句看起来像这样:
INSERT INTO customers (NAME, email)
VALUES
('Microsoft', 'hotline@microsoft.com')
ON CONFLICT where (name = 'Microsoft' and active = TRUE)
DO UPDATE SET email = 'hotline@microsoft.com';
这个例子有点做作,但我希望我能够在这里传达要点。
【问题讨论】:
标签: sql postgresql indexing unique upsert