What is an Identity Column?
An identity column in Oracle is used to automatically generate sequential values, typically for a primary key. When you define a column as "GENERATED AS IDENTITY", Oracle creates and manages an internal sequence behind the scenes. This feature removes the need to manually create and manage a separate sequence and trigger.
Why Reset the Identity Sequence?
You may need to reset the identity column sequence in situations such as:
- You inserted test data and now want to move the table to production.
- You deleted or truncated the data and want the sequence to start from 1 again.
- You want the identity sequence to continue from the current maximum ID.
The method depends on the Oracle version you are using.
In Oracle 12c (12.1 and 12.2):
You can reset the identity sequence using the START WITH clause.
Example:
Code: Select all
ALTER TABLE JS_INVOICE_HEADER MODIFY ID GENERATED AS IDENTITY (START WITH 1);
In Oracle 18c and Higher (including 21c and 23c):
Use START WITH LIMIT VALUE to automatically resume from the maximum existing ID plus the increment.
Example:
Code: Select all
ALTER TABLE JS_INVOICE_HEADER MODIFY ID GENERATED BY DEFAULT AS IDENTITY (START WITH LIMIT VALUE);
After Truncating the Table:
If the table has been truncated (i.e., all rows deleted), use:
Code: Select all
TRUNCATE TABLE JS_INVOICE_HEADER;
ALTER TABLE JS_INVOICE_HEADER MODIFY ID GENERATED AS IDENTITY (START WITH 1);
Verifying Identity Column Details:
To check the identity column and its system-managed sequence:
SELECT COLUMN_NAME, GENERATION_TYPE, SEQUENCE_NAME FROM USER_TAB_IDENTITY_COLS WHERE TABLE_NAME = 'JS_INVOICE_HEADER';
Summary:
Reset to 1 after truncation (12c+):
ALTER TABLE JS_INVOICE_HEADER MODIFY ID GENERATED AS IDENTITY (START WITH 1);
Automatically resume from max value (18c+):
ALTER TABLE JS_INVOICE_HEADER MODIFY ID GENERATED BY DEFAULT AS IDENTITY (START WITH LIMIT VALUE);