Icon Frequently Asked Questions

How do I alter the starting value for an existing column that is defined as GENERATED AS IDENTITY ?

When you first define an IDENTITY column, you do so as follows:

CREATE TABLE MyTable
(
MyIdentityColumn INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH <SeedValue>, INCREMENT BY <IncrementValue>)
)

This will cause the <SeedValue> to be used as the starting value when the table is first created.

However, with an existing table, you cannot simply use the following statement to change the seed value:

ALTER TABLE MyTable
ALTER COLUMN MyIdentityColumn INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH <NewSeedValue>, INCREMENT BY <IncrementValue>)

Executing the above statement will only cause the database catalog to be updated so that any time the table is created it will have the new seed value. It will not change the next seed value used for an existing table. It is important to remember that the database catalog and the physical table files are two separate entities. It is possible to delete the table files from disk and leave the database catalog, and ElevateDB will simply create new, empty table files when the table is opened again.

In order to change the seed value on existing table files, one must use the ALTER COLUMN clause along with the RESTART WITH clause, like this:

ALTER TABLE MyTable
ALTER COLUMN MyIdentityColumn RESTART WITH <NewSeedValue>

This statement will properly reset the seed value for the IDENTITY column so that any newly-inserted rows will use the new seed value.
Image