Reputation: 12902
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
Is there an easier or more efficient way of doing this (or is there some reason why this wouldn't work)?
Upvotes: 8
Views: 6137
Reputation: 425371
I'd do it the following way:
Create the id
column, allowing null values
Issue this query:
UPDATE mytable
SET id = rownum
Alter table to add NOT NULL
and PRIMARY KEY
for the new id column
Create the sequence, seeding it to MAX(id) + 1
and use it for the further inserts.
Upvotes: 8