Reputation: 458
I am trying to copy data from one column to another column in a different table using MySQL but the table that I am importing into has a foreign key restraint that is preventing me from doing this;
Here is the table that I would like to import from (product_code) column
Table1
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
| id | product_code | distributor | brand | productname | wheelsize | pcd_1 | pcd_2 | pcd_3 |
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
| 1 | F7050MHS20A2 | ******* | MAK | MOHAVE | 7 x 15 | 5x139.7 | | |
| 2 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 3 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 4 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 5 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 6 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 7 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 8 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 9 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 10 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
I would like to copy the product_code
column into the sku
column
Table2
+----------+----------+-------+--------------+
| id | value_id | pid | sku |
+----------+----------+-------+--------------+
| 20315857 | 369781 | 41257 | 001 |
| 20315858 | 369782 | 41256 | Config - ST5 |
+----------+----------+-------+--------------+
The problem is that the value_id
column in Table2 is referencing value_id
Table3 so I either get a Foreign key restraint error
or lock wait timeout
a foreign key constraint fails (`gravytra_topgear`.`am_finder_map`, CONSTRAINT `FK_MAP_VALUE` FOREIGN KEY (`value_id`) REFERENCES `am_finder_value` (`value_id`) ON D
Table 3
+----------+-----------+-------------+----------------+
| value_id | parent_id | dropdown_id | name |
+----------+-----------+-------------+----------------+
| 6771 | 0 | 4 | AC |
| 6749 | 0 | 4 | Acura USA |
| 6895 | 0 | 4 | Aixam |
| 6872 | 0 | 4 | Alfa Romeo |
| 6853 | 0 | 4 | Alfa Romeo USA |
| 6772 | 0 | 4 | Alpina |
| 6815 | 0 | 4 | AMC USA |
| 6854 | 0 | 4 | Anhui Anchi |
| 6928 | 0 | 4 | Ariel |
| 6783 | 0 | 4 | ARO |
+----------+-----------+-------------+----------------+
Here is my Query
INSERT INTO table2 (sku) SELECT product_code FROM table1;
The table1 product_code
column has over 2million records inside it and has caused my server to crash during the query.
I know that there must be a better way to do this but I can't figure out how to so would like some assistance if possible please...?
Upvotes: 1
Views: 1270
Reputation: 458
The fix was simple,
Just wrap the query inside:
SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS = 0;
SET UNIQUE_CHECKS = 0;
# YOUR QUERIES HERE...
SET AUTOCOMMIT = 1;
SET FOREIGN_KEY_CHECKS = 1;
SET UNIQUE_CHECKS = 1;
The the data goes in, only difficulty is the large size of the dataset but breaking it into chunks helped alot.
Upvotes: 0
Reputation: 371
To me your query looks OK.
try breaking the "insert - select" into parts either using some id in the where clause or using limit in the select.
insert 100 records. See how it goes. check your time_out variables. increase accordingly if needed.
Upvotes: 1
Reputation: 1384
You are trying to insert lots of different columns into table_1
but you aren't selecting those out of the first table. So you're inserting one column when you should be inserting 4. I think what you're looking for is:
INSERT INTO table_1(name) SELECT firstname FROM table_2;
If you want to have default values for the other 3 columns, you could easily put them in the select query as well:
INSERT INTO table_1(value_id, parent_id, dropdown_id, name)
SELECT default_id, default_parent_id, default_dropdown_id, firstname
FROM table_2;
Simply replace the default_
ids with your own defaults, whatever they may be.
Upvotes: 0