Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset IDENTITY column in oracle to a new value

I am using IDENTITY feature of oracle 12 c to increment record id of a table.
As part of initial setup we have to migrate some records from another system to this table. these records are non sequential records(Partial records are deleted).

How to make identity to always create highest value + 1 based on the table records.

After googling for different options found keyword of RESTART WITH value option. For this we need to create a new store procedure and alter all the tables with highest possible values to restart with.

Is there any direct keyword which can be used along with IDENTITY which can force it to always regenerate higher values. ?

like image 630
sims Avatar asked Aug 03 '15 09:08

sims


1 Answers

This has become super easy in 12c

alter table your_table modify (id generated by default on null as identity 
                               start with limit value);

Then the next insert will safely insert using a sequence that has been automatically reset to what is essentially max(id)+1 https://docs.oracle.com/database/121/SQLRF/statements_3001.htm#SQLRF01001

like image 197
Scott Avatar answered Sep 20 '22 18:09

Scott