I have a set of rows in Table A (with AI PK) that I'd like to clone based on certain criteria, ie:
INSERT INTO A (field1, field2, field3) SELECT field1,field2,field3 FROM A WHERE (criteria)
But, these rows have a 1-1 relation with Table B which has an AI PK in B and FK in A. I would like to also clone the related rows in table B and have the cloned rows in A point to the cloned rows in B not the original rows.
I'm looking for the simplest way to do this--preferably just in SQL.
Here's an example of what I'm trying to do:
BEFORE CLONE:
Table A
ID B_FK Other Data Meets Clone Criteria
1 101 Data 1 true
2 102 Data 2 false
3 103 Data 3 true
4 104 Data 4 true
Table B
ID Other Data
101 Data A
102 Data B
103 Data C
104 Data D
AFTER ROW CLONING:
Table A
ID B_FK Other Data
1 101 Data 1
2 102 Data 2
3 103 Data 3
4 104 Data 4
5 105 Data 1
6 106 Data 3
7 107 Data 4
Table B
ID Other Data
101 Data A
102 Data B
103 Data C
104 Data D
105 Data A
106 Data C
107 Data D