Update Foreign Keys

I have table Orders with an ID autonumber field

ID
CustomerID
OrderDate
etc...

I have table Detail with a field 'OrderID' which points to ID field in table Orders.

ID
OrderID
Qty
PartNum
Cost
etc...

One or Many records in table Detail point to a single Order record.

The ID's in table orders have to be changed/resequenced which in turn means that the 'OrderID' fields in table Detail must now point to the new 'OrderID'.

I have created a new table Order_Reseq and appended all of the order records to it.  The append query appends the ID field in the original Order table to a field called OldID in the table Order_Reseq.  The fields in Order_Reseq are:

ID                 which is an autonumber
OldID           which is the ID from the original Orders table
CustomerID
OrderDate
etc...

Now I must change all of the 'OrderID' fields in table Detail to point to the new ID in 'Orders_Reseq'.

One idea I have is to loop thru table Orders_Reseq record by record.  For each record I would execute and update/set query on table Detail revising 'OrderID' to the value of 'ID' in table Orders_Reseq' for every record

Read thru Order_Reseq record by record

updateString = " Update tblDetail " & _
                       " Set [OrderID]      = " & rsIn!ID & _
                       " Where [OrderID] = " & rsIn!OldID
DoCmd.RunSQL updateString


I am thinking there must be a better way, so that is the question.   Is there an easier more efficient way of revising all of the 'OrderID' fields in to the revised ID in the orders table?

Efficienty is important because their are approximatley 3 million records in the order table and 8  millionn in the Detail table.
LVL 1
mlcktmguyAsked:
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.

chaauCommented:
The easiest way to do the updates is to enforce cascade updates/deletes. In this case you would only need to update/delete in the master table. The dependant tables will be updated automatically. To do so, create the relationship and check the cascade checkboxes, as per below:
 cascade update
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
Dale FyeCommented:
However, your comment:

"The ID's in table orders have to be changed/resequenced"

has me somewhat concerned.  Autonumber fields are just that (auto); you should rarely present these values to your customers or users as they can be confusing, and rarely is there a good reason to "change/resequence" them.  

If you need a number that you can present to your users (Case #, Invoice #, ...) then I would recommend creating another field and computing that value based on your internal rule set (generally via a function).  It gives you more flexibility to make changes later on, and still allows you to use the autonumber as the "behind the scenes" link between your various tables.
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
Microsoft Access

From novice to tech pro — start learning today.