简体   繁体   中英

How to execute SQL queries skipping error?

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?

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.

  • 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 ...".

How to inmport all data skipping this error?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM