9

I try to import to table data from sql files using command line. This data contents duplicates in filed url.

But field url in table is unique. So when I try to insert data I get error "Dublicate entry"

How to inmport all data skipping this error?

7
  • You should probably import to a new table, and insert only unique records. Commented Dec 6, 2016 at 13:15
  • 2
    Show the code that you are using to bring the data in. Commented Dec 6, 2016 at 13:16
  • How to do this? Create one table and how insert only unique rows? Commented Dec 6, 2016 at 13:16
  • look at this post stackoverflow.com/a/812462/3956479 Commented Dec 6, 2016 at 13:17
  • You should create query from the table you have created with DISTINCT Or GROUP BY if is possible Commented Dec 6, 2016 at 13:19

4 Answers 4

6

You can to use the --force (-f) flag.

mysql -u userName -p -f -D dbName < script.sql

From man mysql:

· --force, -f

Continue even if an SQL error occurs.

Sign up to request clarification or add additional context in comments.

Comments

2
  • Create a staging table with the same structure as your destination table but without the constraints (unique index included).
  • Manually check the duplicates and decide on the way you want to choose between duplicates rows / merge rows.
  • Write the appropriate query and use "insert into ... select ...".

Comments

0

How to inmport all data skipping this error?

Drop the index for time being -> run your batch insert -> recreate the index back

1 Comment

recreate the index back will be failed
0

If you are using insert, the you can ignore errors using ignore error or on duplicate key update (preferable because it only ignores duplicate key errors).

If you are using load data infile, then you can use the ignore key word. As described in the documentation:

If you specify IGNORE, rows that duplicate an existing row on a unique key value are discarded. For more information, see Comparison of the IGNORE Keyword and Strict SQL Mode.

Or, do as I would normally do:

  • Load the data into a staging table.
  • Validate the staging table and only load the appropriate data into the final table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.