Rahul
Rahul

Reputation: 893

Postgres UPSERT on any constraint

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

Answers (1)

Robins Tharakan
Robins Tharakan

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

Related Questions