Changing database structure by Adding Primary Key field SQL 11g

Posted on 2014-10-28
Medium Priority
Last Modified: 2014-10-31
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?

Question by:Prisoner362670
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 29

Expert Comment

ID: 40409500
Can you post a sample of the table definition?

Author Comment

ID: 40413112
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
LVL 29

Expert Comment

ID: 40413193
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?
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.


Author Comment

ID: 40413350
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?
LVL 29

Accepted Solution

MikeOM_DBA earned 2000 total points
ID: 40414239
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.

Author Comment

ID: 40415796
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.

Author Closing Comment

ID: 40415798
I have yet to test this solution but overall it seems sound and prudent.  I will write back after test.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question