部分インデックスが必要です。列nameに一意の制約を削除します 列に部分インデックスを作成します:
CREATE TABLE customers (
customer_id serial PRIMARY KEY,
name VARCHAR,
email VARCHAR NOT NULL,
active bool NOT NULL DEFAULT TRUE
);
CREATE UNIQUE INDEX ON customers (name) WHERE active;
INSERT INTO customers (NAME, email) VALUES
('IBM', 'example@sqldat.com'),
('Microsoft', 'example@sqldat.com'),
('Intel','example@sqldat.com');
クエリは次のようになります:
INSERT INTO customers (name, email)
VALUES
('Microsoft', 'example@sqldat.com')
ON CONFLICT (name) WHERE active
DO UPDATE SET email = excluded.email;
SELECT *
FROM customers;
customer_id | name | email | active
-------------+-----------+-----------------------+--------
1 | IBM | example@sqldat.com | t
3 | Intel | example@sqldat.com | t
2 | Microsoft | example@sqldat.com | t
(3 rows)
excluded.の特別なレコードが適切に使用されていることに注意してください。 ドキュメントごと: