Wednesday, 15 August 2012

php - Copy records and related records over in a MySQL database (stored procedure or function) -


I'm thinking that a stored procedure / function would be the best solution for this, but I'm open to suggestions.

I want to duplicate a master record, and want to duplicate all related records at the same time. Consider the following example setup:

  Table 1 Table 2 Table 3 ------- ---------- ---------- ID (PK) ID (PK) ID ID 1 T1_ID (FK) T2_ID (FK) Data 2 Data 1 Data 1 Data 3 Data 2 Data 2  

Looking at the schema, you can see that the Table 1 is the master table; I can put it in the tab2 with your child's record and table 3 Want to copy with his granddaughter's record I received.

When this is done, it will be necessary to update the PK (some of which are auto_ature, and some are dynamically generated). When this happens, the foreign keys will also need to be updated in related tables.

Is there any processed process / task that can do this with ease, which I am now trying to do? [Provide more information for everything in a multi-dimensional array, and then inserting, proceeding, moving forward and inserting more information].


No comments:

Post a Comment