How can we copy data from one table into another table which doesn’t exist in the first one. In table one of the column is primary key.
INSERT INTO table SELECT * FROM db2.table;
ERROR 1062 (23000): Duplicate entry '100001' for key 'id_UNIQUE'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the predicate
NOT INto do so like this:This will checks whether this
table1ReferenceIDfound in the first table or nor. Therefore, theSELECTclause will select all the rows from the second tables except those that is already presented in the first table table1.Note that: the column
table1ReferenceIDis the reference of theid_UNIQUEin the second table.Other alternatives for this, is to
LEFT JOINas suggested by @HamletHakobyan‘s answer andNOT EXISTS.