6

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?

Shadow
  • 33,525
  • 10
  • 51
  • 64
MisterPi
  • 1,471
  • 5
  • 17
  • 23

4 Answers4

4

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.

vp_arth
  • 14,461
  • 4
  • 37
  • 66
1
  • 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 ...".
David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88
0

How to inmport all data skipping this error?

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

Rahul
  • 76,197
  • 13
  • 71
  • 125
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.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786