Expressing MERGE statements
The MERGE
statement is quite a powerful tool; it allows us to perform INSERT
/UPDATE
and even DELETE
on a table known as the target table from a table known as the source table. I strongly suggest you read this article, especially if you need a quick reminder of the MERGE
statement: https://blog.jooq.org/2020/04/10/the-many-flavours-of-the-arcane-sql-merge-statement/.
MySQL and PostgreSQL support a MERGE
flavor known as UPSERT
(INSERT
or UPDATE
) via ON DUPLICATE KEY UPDATE
, respectively via ON CONFLICT DO UPDATE
clauses. You can find examples of these statements next to the well-known INSERT IGNORE INTO
(MySQL) and ON CONFLICT DO NOTHING
(PostgreSQL) clauses in the code bundled with this book. By the way, we can use all these statements interchangeably (for example, we can use onConflictDoNothing()
with MySQL and onDuplicateKeyIgnore()
with PostgreSQL), since jOOQ will always emulate the correct syntax. We can even use them with SQL Server and Oracle,...