Inserting unique rows without updating old ones
-
22-06-2021 - |
Domanda
I have two similar tables in different databases, I want to insert into one from the other one. At the same time I want to ensure that every time mySql encounters a 'duplicate id' error it inserts the row at the bottom of the new table and assigns a new unique id. I have tried:INSERT...SELECT...ON DUPLICATE KEY UPDATE
But I can't find a way to get it to insert into a new row if it finds 'duplicate keys' instead of updating the previous row.
Soluzione
If you can assign new id
to every record being copied to the destination table, irrespective of whether the id
in the source table existed previously in the destination or not, you can simply not provide the id
and allow MySQL to resort to the default NULL
value that causes an auto_increment
value to be assigned:
INSERT INTO destdb.tbl (colA, colB, colC)
SELECT colA, colB, colC
FROM srcdb.tbl
Otherwise, you will need to use something like NULLIF()
to set id
explicitly to NULL
where it already exists, based on joining the tables together in the SELECT
:
INSERT INTO destdb.tbl (id, colA, colB, colC)
SELECT NULLIF(src.id, dst.id), src.colA, src.colB, src.colC
FROM srcdb.tbl src LEFT JOIN destdb.tbl dst USING (id)
Altri suggerimenti
According to the documentation at http://dev.mysql.com/doc/refman/5.1/de/insert-select.html, this may be achieved with the query:
INSERT INTO `table2` SELECT (NULL, col1, col2, col3) FROM `table1`
This way, the autoincrement-value is left with 'NULL', causing the engine to give it a new AI-value instead of trying to force the existing one in there.
Please don't stone me for the syntax, I haven't tested this.
If the id is autoincrement:
Create an additional column in the second table with a reference to the id value in the first table
Sample Select Query:
SELECT unique_id, data FROM table_1 WHERE id='$id'
Sample Insert Query:
INSERT INTO table_2 (table_1_unique_id, data) VALUES ($unique_id_from_first_table, $data)
This will solve any duplicate id problems and allow referencing between the two tables