I have to modify an existing table in a Oracle 10g DB with a few thousand records to add a surrogate autonumber key. One way that comes to my mind is to
- Create a new sequence
- Create the id column, allowing null values
- Updating the id column with the sequence
- Alter table to add “not null” and “primary key” for the new id column
Is there an easier or more efficient way of doing this (or is there some reason why this wouldn’t work)?
I’d do it the following way:
Create the
idcolumn, allowing null valuesIssue this query:
Alter table to add
NOT NULLandPRIMARY KEYfor the new id columnCreate the sequence, seeding it to
MAX(id) + 1and use it for the further inserts.