Bonzay
Bonzay

Reputation: 750

alter table adding guid column in oracle

What is the statement to alter a table which holds about 10 million rows, adding a guid column which will hold a unique identifier for each row (without being part of the pk)

What datatype should the global unique identifier column be? Is there a procedure which creates it? How will it auto incremented or produced everytime a new record is inserted?

Upvotes: 0

Views: 2286

Answers (2)

SatishSK
SatishSK

Reputation: 56

  1. Use identity columns feature of oracle 12c to add a column to the table which auto increments upon adding new rows to the table.
  2. An ideal way to handle this task is to: a) CREATE a "new" table with structure similar to the source table using CREATE TABLE AS (CTAS statement) with a new "identity column" instead of adding identity column using ALTER statement on existing table. b) CTAS works faster compared to running ALTER on existing table. c) After confirming that the "new" table has all the data from the source table along with an column containing unique values and all the indexes and constraints then, you can drop the original table.

Another way to avoid creating constraints, indexes present on original table onto the new table is to create an empty table with all constraints, indexes and identity column. Let DBA extract data from the original table and import it into the "new" table.

Benefits: This approach will ensure that none of the objects dependent on the source table become INVALID which generally hampers some features of the application(s).

Upvotes: 1

JohnHC
JohnHC

Reputation: 11195

Break it down into the separate stages

First, we need a new column:

alter table MyTable
add guid_column raw(32) default sys_guid();

Then update the existing rows:

update MyTable
set guid_column = sys_guid();

Upvotes: 2

Related Questions