1

An Oracle Identity column can be reset by simply changing its start value with an alter table... modify statement. Given that identity column internally uses a sequence, why is it not possible to do the same with a sequence i.e. change its start value to a specific number with a single alter statement? So far the only way I've seen to do this is through a procedure involving multiple steps.

Or, to put it in another way, how does Oracle internally handle the altering of start value of an identity column? Does it drop and recreate the backing implicit sequence? Or is there a way to reset a sequence which is just not accessible to users?

atokpas
8,6901 gold badge18 silver badges27 bronze badges
asked Nov 20, 2018 at 22:40

1 Answer 1

3

Given that identity column internally uses a sequence, why is it not possible to do the same with a sequence i.e. change its start value to a specific number with a single alter statement?

Oracle Official way of resetting a sequence is dropping and recreating the sequence, of course, you need to take care about the privileges granted.

You can also use this statement to restart a sequence by dropping and then re-creating it. For example, if you have a sequence with a current value of 150 and you would like to restart the sequence with a value of 27, then you can drop the sequence and then re-create it with the same name and a START WITH value of 27.

Oracle 12c Docs

However, there is an undocumented way to reset the sequence which works for Oracle 12c.

SQL> select serial.nextval from dual;
 NEXTVAL
----------
 4
SQL> alter sequence serial restart start with 1;
Sequence altered.
SQL> select serial.nextval from dual;
 NEXTVAL
----------
 1
answered Nov 21, 2018 at 4:34
3
  • 1
    But since it is undocumented, one would be well advised to not rely on it. I've been in this business over 35 years, first as a cobol programmer, then as a DBA on various platforms, and believe me the landscape is littered with people who came to rely on some undocumented 'feature' or behavior only to come to grief when said behavior changed. Since the behavior is undocumented the vendor is under no obligation to guarantee it and some internal change could break it at any patch. Commented Nov 22, 2018 at 12:20
  • 1
    It is a documented "new" feature in 18c: docs.oracle.com/en/database/oracle/oracle-database/18/admin/… docs.oracle.com/en/database/oracle/oracle-database/18/admin/… The SQL Language Reference still does not have this clause... Commented Nov 23, 2018 at 10:02
  • 1
    The restart clause is now documented in the 19c docs docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/… Commented Oct 14, 2020 at 12:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.