Changing database structure by Adding Primary Key field SQL 11g

Greetings Experts -

Today a general discussion question.

I have inherited a database an Oracle 11g database with NO primary key field.  
Instead an external identifier with a not null constraint was used to relate all the tables, run the forms and update the records.  

As is the fate of all external identifiers, it is now being superseded and will no longer be available to my inherited database.  Even better--not every record in the inherited database will be assigned a new external identifier.

Ideally, I would like to migrate the old external identifier into a new field, and modify its former field to a primary key field with an autonumber sequence.  By reusing the field I hope to avoid having to rewrite all of the tables/queries and forms.

I am unclear as to the method and timing of updating the related tables.  Should I create a new temporary field in the 'base' table so that I maintain integrity while I update the related child tables?  Other pitfalls?  Is this an accepted approach?  Are there others?

CongoGrey
Prisoner362670Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:
Can you post a sample of the table definition?
0
Prisoner362670Author Commented:
Sure Mike - Sorry for the delayed response as I have been ill.

Table1: Demography
ColName             IDNO                                    FName  
DataType            VARCHAR2 (7 Byte)            VARCHAR2(20 Byte)
Nullable              NO                                           Yes
DataDefault        (null)                                          (null)
PrimaryKey          (null)                                         (null)                                            
ConstraintType    Check (idno is not null)
Status                    Enabled                                  
Deferrable            Not Deferrable
IndexName            (null)      

Table2: RelatedTable1

ColName                    IDNO
DataType                    VARCHAR2 (7 Byte)          
Nullable                       No
DataDefault                (null)
PrimaryKey                 (null)

Let me know if you need additional information
0
MikeOM_DBACommented:
Sorry, but I am still not sure what you mean by "an external identifier"?
What is the source and how is the local table populated with this value?

Why not preserve the current values of said identifier converting it to primary key and just code a function to generate the future values for that key?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Prisoner362670Author Commented:
The 'external identifier' is an id number generated by another database.  Certain demographic information from that database is imported into the Oracle 11g database using the SQL loader utility.  Imports may be new records or updates to existing records.

The 'external identifier' will change both size and datatype in the external database within a few months.  
There are several tacks I could take which is why this is a discussion question.

Your suggestion is novel to me.  Let me restate it to make sure that I understand:  modify the idno field to primary key, retain the current values in idno and set a sequence function to assign values to all new records.
Create a new field to hold the new external identifier and use this value to locate records for update.

Another twist on the same suggestion: If I updated the field/datatype in the Oracle 11g demographic table with a primary key constraint and updated the old identifier to the new identifier (because I need to perform updates using the external database), how would I change the old identifier written in the related tables in Oracle 11g to the new identifier?
0
MikeOM_DBACommented:
Maybe we are on the same page:

1) Add column for the new identifier
2) Copy old identifier to new identifier column in order to change to the new identifier when available
3) Create unique index on IDN0 (old identifier)
4) Add primary key constraint  on (IDN0) using index  from (3)
5) Create a function that will generate PK values when new row is inserted
6) Code trigger on insert to populate PK using the function

. . . And no need to modify related tables.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Prisoner362670Author Commented:
Hi Mike - sounds like a plan!  I'll try it in my test environment between this afternoon and Monday afternoon and let you know how it turns out.
However, in the meantime I'm going to accept your solution.  It was the 'no need to modify related tables' that did it for me.
0
Prisoner362670Author Commented:
I have yet to test this solution but overall it seems sound and prudent.  I will write back after test.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.