Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset Identity column in Oracle

Tags:

oracle

I have a column with identity, which count is 19546542, and i want reset it after deleting all data .I need anything like 'dbcc checkident' in ms sql but in Oracle

like image 677
Tigran Simonyan Avatar asked Sep 02 '16 14:09

Tigran Simonyan


People also ask

How do I reset my identity column?

Here, to reset the Identity column in SQL Server you can use DBCC CHECKIDENT method. Syntax : DBCC CHECKIDENT ('table_name', RESEED, new_value); Note : If we reset the existing records in the table and insert new records, then it will show an error.

How do I change an existing column to an identity in Oracle?

Sadly you can't alter an existing column to become an identity. This assigns a value from the sequence to all existing rows.

What is Oracle Identity column?

An INTEGER, LONG, or NUMBER column in a table can be defined as an identity column. The system can automatically generate values for the identity column using a sequence generator. See Sequence Generator section. A value for an identity column is generated during an INSERT, UPSERT, or UPDATE statement.


1 Answers

From Oracle 12c and above version, you can do it in 3 ways:

  1. Manually reset next value of the sequence to specific value:

ALTER TABLE [TableName] MODIFY(ID GENERATED AS IDENTITY (START WITH 1));

  1. Automatically reset the next value of the sequence to the maximum ID value:

ALTER TABLE [TableName] MODIFY ID GENERATED BY DEFAULT AS IDENTITY (START WITH LIMIT VALUE);

Both the above case it will allow you to insert data with values in the identity column

insert into [TableName] (ID, Name) VALUES (1, 'Name1');
insert into [TableName] (ID, Name) VALUES (2, 'Name2');
  1. Automatically reset the next value of the sequence to the maximum ID:

ALTER TABLE [TableName] MODIFY ID GENERATED ALWAYS AS IDENTITY (START WITH LIMIT VALUE);

However, in this case, it will restrict you insert with identity column values

insert into [TableName] (Name) VALUES ('Name1');
insert into [TableName] (Name) VALUES ('Name2');

You will following error if you pass values to the identity column

ORA-32795: cannot insert into a generated always identity column

like image 114
mijaved Avatar answered Oct 04 '22 21:10

mijaved