Note that there are some explanatory texts on larger screens.

plurals
  1. POAS400 DB2 Duplicate Key Error during Insert in Table with PK Identity Column
    primarykey
    data
    text
    <p>I got a Table with an Auto Increment Column which looks like: </p> <pre class="lang-sql prettyprint-override"><code>ALTER TABLE SOMESCHEMA.SOMETABLE ALTER COLUMN ID SET DATA TYPE INTEGER GENERATED BY DEFAULT SET INCREMENT BY 1 SET NO ORDER SET NO CYCLE SET MINVALUE 1 SET MAXVALUE 2147483647 SET NO CACHE; </code></pre> <p>As long as i let the DBMS generate the Ids everything works fine and I can get the generated Id via: </p> <pre class="lang-sql prettyprint-override"><code>SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1 </code></pre> <p>But sometimes i need to insert a row with an ID of my choice and there i get into trouble.</p> <p>Lets say we got a single row in the table with ID 1.<br>Now i insert a new row with a manually assigned id of 2.<br> The next time i try to insert a new row without a preset ID i get an error SQL0803 "DUPLICATE KEY".</p> <p>I assume the internal "NextId" field for that Auto-Increment Column doesnt update itself if the Id of a row is manually set.</p> <p>So I tried reseting this field with:</p> <pre class="lang-sql prettyprint-override"><code>ALTER TABLE SOMESCHEMA.SOMETABLE ALTER COLUMN ID RESTART WITH 3 </code></pre> <p>But this causes a permanent Table lock, which i dont know how to unlock.</p> <p>How can i get this "Mixed-Mode" ID-Column working? Is it possible to get it to work like MySQL where the DBMS automatically updates the "NextID" upon a manually-Id Insert? If not, how can I release that {insert swear-word here} lock that pops up if i try to reset the NextId?</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.
 

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