I can duplicate rows from a single table using INSERT and SELECT whilst assigning a new auto increment value eg:
INSERT INTO tableA (tableA_ID,tableB_ID,col1,col2)
SELECT '0',tableB_ID,col1,col2 WHERE tableB_ID='1'
However, suppose I have 2 tables with autoincrement ids, but, whose data I wish to duplicate simultaneously.
Please consider these tables;
report
+----------------------+---------------+------+-----+
| Field | Type | Null | Key |
+----------------------+---------------+------+-----+
| report_id | int(11) | NO | PRI |
| report_title | varchar(255) | NO | |
+----------------------+---------------+------+-----+
document
+----------------------+---------------+------+-----+
| Field | Type | Null | Key |
+----------------------+---------------+------+-----+
| doc_id | int(11) | NO | PRI |
| report_id | int(11) | NO | |
| document_title | varchar(255) | NO | |
+----------------------+---------------+------+-----+
document_asset
+----------------------+---------------+------+-----+
| Field | Type | Null | Key |
+----------------------+---------------+------+-----+
| asset_id | int(11) | NO | PRI |
| doc_id | int(11) | NO | |
| report_id | int(11) | NO | |
| asset_name | varchar(255) | NO | |
+----------------------+---------------+------+-----+
It is necessary to duplicate a report, it's documents and their assets so that they may be edited whilst preserving the original. I can using the aforementioned method, duplicate tables 'report' and 'document' without undue stress. However, I am unable to create query that will allow me to duplicate the document and also the document_asset(s) whilst linking the duplicate assets to the duplicate parent document.
Here is an example of the initial data:
Report:
+-----------+--------------------------+
| report_id | report_title |
+-----------+--------------------------+
| 1 | Report A |
+-----------+--------------------------+
Document:
+-----------+--------------------------+
| doc_id | report_id | document_title |
+-----------+--------------------------+
| 1 | 1 | Doc 1 |
| 2 | 1 | Doc 2 |
+-----------+--------------------------+
Document Asset:
+-----------+--------------------------------+
| asset_id | doc_id | report_id | asset_name |
+-----------+--------------------------------+
| 1 | 1 | 1 | Asset 1 |
| 2 | 1 | 1 | Asset 2 |
| 3 | 2 | 1 | Asset 3 |
+-----------+--------------------------------+
This is the output I am working towards:
Report Output:
+-----------+--------------------------+
| report_id | report_title |
+-----------+--------------------------+
| 1 | Report A |
| 2 | Copy of Report A |
+-----------+--------------------------+
Document Output:
+-----------+--------------------------+
| doc_id | report_id | document_title |
+-----------+--------------------------+
| 1 | 1 | Doc 1 |
| 2 | 1 | Doc 2 |
| 3 | 2 | Doc 1 |
| 4 | 2 | Doc 2 |
+-----------+--------------------------+
Document Asset Output:
+-----------+--------------------------------+
| asset_id | doc_id | report_id | asset_name |
+-----------+--------------------------------+
| 1 | 1 | 1 | Asset 1 |
| 2 | 1 | 1 | Asset 2 |
| 3 | 2 | 1 | Asset 3 |
| 4 | 3 | 2 | Asset 1 |
| 5 | 3 | 2 | Asset 2 |
| 6 | 4 | 2 | Asset 3 |
+-----------+--------------------------------+
Note, duplicating the report is easy enough, it is duplicating documents and document assets that is the issue.