0

I want to copy data from TABLE A to TABLE B based on date range and update the foreign key value in the TABLE A . How to achieve this using sql? i.e

TABLE A

A_id : 11,12,13

A_name : abc,def,efg

A_item: item1, item2, item3

A_date :

B_id : null, null ,null

TABLE B:

B_id : 1, 2, 3...

B_name: abc,def,efg

B_item: item1, item2, item3

After copying data, TABLE A should look like:

TABLE A

A_id : 11,12,13

A_name : abc,def,efg

A_item: item1, item2, item3

A_date :

B_id : 1, 2, 3

Could you please let me know how to update the foreign key value after copying data?

Shadow
  • 33,525
  • 10
  • 51
  • 64
Veda
  • 577
  • 1
  • 6
  • 22
  • It looks like `Table B` is the child table. Shouldn't you have the foreign key on `Table B` referencing `Table A` `A_id`? And it would be easier to insert rows into `Table B` with `A_id` column on `Table B`. – SS_DBA Aug 20 '20 at 00:36
  • Actually, once new record is added, first Table B entry will be made and then Table A entry will be added. But for the older data, we are copying from TABle A to Table B and facing this issue since Table A reference id is blank in the older data – Veda Aug 20 '20 at 02:50
  • 1
    Where does the "copy data" and "date range" come into play here? Your example reads like: "Table A has missing values for B_Id at some rows, we want to update those missing values from table B". And this would actually be done with an update, see e.g. [mysql update column with value from another table](https://stackoverflow.com/q/11709043) – Solarflare Aug 20 '20 at 06:29

0 Answers0