Skip to content
Advertisement

ORACLE keep identity columns always as one (1)

I found some questions similar to mine, but I couldn’t find the exact solution. In the Data Warehouse we have, we sometimes “Delete” or “Truncate” the table when we fix something or some other similar problem. However, in Oracle, the identity column always starts with the next string, how do you make the string always start with 1? This can disrupt the other dimensions and facts.

Is it possible to put this configuration forever or at least in the insert procedure?

I always reset the identity column manually by SQL Developer, since I don’t know how to do it by PL/SQL, as shown below (software is in Portuguese, sorry).

Example of identity column created:

enter image description here

Thanks!

Advertisement

Answer


EDIT: Given what I knew at the time I was right, but as it turns out I’m incorrect. In a comment and an associated fiddle @micklesh shows how this can be accomplished using ALTER TABLE MODIFY.... I’ve asked him to post an answer so it can be accepted – in the meantime I’ve leaving this answer here so that others can at least follow the link to his dbfiddle. But really – this is incorrect.


Sorry, but you cannot do this.

I had a really nice answer prepared about how to get the name of the sequence the system creates for a GENERATED ALWAYS AS IDENTITY column, and how to translate that LONG value to a character string, and how to reset the start value of a sequence – all good stuff, and it made a really nice db<>fiddle – and then when I got it all wrapped up I made one last pass through it – and got

So – Oracle will not let you alter the sequence it generates for a GENERATED ALWAYS AS IDENTITY column. I think this means you’re stuck and you’re going to have to live with the fact that those numbers cannot be reset to start at one. Your other options would be to

  1. Drop and recreate the table, which would also require you to recreate any associated triggers, and recompile any procedures/functions/packages which use this table, and probably other things I haven’t thought of; or

  2. Don’t use GENERATE ALWAYS AS IDENTITY, create your own sequence, use a trigger to set the identity column from your sequence, and then you should be able to use the following procedure to reset your sequence:

And here’s a db<>fiddle showing a general test of the RESET_SEQUENCE function.

Pax.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement