Reputation: 893
In MySQL we can do the following on any constraint violation
INSERT INTO table {setters} ON DUPLICATE KEY UPDATE {setters}
Is there anyway to do this in Postgres ?
INSERT INTO table {setters} ON CONFLICT(*) DO UPDATE {setters}
Note: * = Any Possible Key
Why is this an important feature?
Upvotes: 5
Views: 2837
Reputation: 2473
Sure. PostgreSQL 9.5+ allows you to do this, in this manner:
The MySQL
query:
INSERT INTO tablename (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
In PostgreSQL
becomes:
INSERT INTO tablename (a, b, c) values (1, 2, 10)
ON CONFLICT (a) DO UPDATE SET c = tablename.c + 1;
Upvotes: 1