Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Short Answer:</strong></p> <p>MERGE INTO AUDIT_TRANSACTION_IDS (uuid, time) KEY (uuid, time) VALUES (TRANSACTION_ID(), NOW()); </p> <p><sub>little performance tip: make sure uuid is indexed</sub></p> <p><strong>Long Answer:</strong></p> <p><code>MERGE</code> is basically an <code>UPDATE</code> which <code>INSERT</code>s when no record found to be updated. </p> <p>Wikipedia gives a more concise, standardized syntax of <a href="http://en.wikipedia.org/wiki/Merge_%28SQL%29" rel="nofollow">MERGE</a> but you have to supply your own update and insert. (Whether this will be supported in H2 or not is not mine to answer) </p> <p>So how do you update a record using <code>MERGE</code> in H2? You define a key to be looked up for, if it is found you update the row (with column names you supply, and you can define <code>DEFAULT</code> here, to reset your columns to its defaults), otherwise you insert the row. </p> <p>Now what is <code>Null</code>? <code>Null</code> means unknown, not found, undefined, anything which is not what you're looking for. </p> <p>That is why <code>Null</code> works as key to be looked up for. Because it means the record is not found. </p> <blockquote> <p>MERGE INTO table1 (id, col1, col2) KEY(id) VALUES (Null, 1, 2) </p> </blockquote> <p><code>Null</code> has a value. it IS a value.</p> <p>Now let's see your SQL. </p> <blockquote> <p>MERGE INTO table1 (id, col1, col2) KEY(id) VALUES (DEFAULT, 1, 2)</p> </blockquote> <p>What is that implying? To me, it says I have this [DEFAULT, 1, 2], <strong><em>find me a <code>DEFAULT</code> in column <code>id</code></em></strong>, then update <code>col1</code> to 1, <code>col2</code> to 2, if found. otherwise, insert default to <code>id</code>, 1 to <code>col1</code>, 2 to <code>col2</code>. </p> <p>See what I emphasized there? What does that even mean? What is <code>DEFAULT</code>? How do you compare <code>DEFAULT</code> to <code>id</code>? </p> <p><code>DEFAULT</code> is just a keyword. </p> <p>You can do stuff like, </p> <blockquote> <p>MERGE INTO table1 (id, col1, timeStampCol) KEY(id) VALUES (Null, 1, DEFAULT)</p> </blockquote> <p>but don't put DEFAULT in the key column.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload