ali
ali

Reputation: 11045

MySQL INSERT if a identical entire row doesn't exist

I have a table with 10 columns and I have to add many, many rows from a CSV file. Of course, I must not add two identical rows, so I need a SQL statement that ignore the command if the entire row does exists. The INSERT must be ignored only if all fields are identical. Two rows may have identical field1 or field2, but not all fields identical.

I tried INSERT IGNORE but it doesn't work. No column is set as UNIQUE, as the INSERT must be ignored onyl only if the entire row is identical.

What solution do you have for this? Thanks!

Upvotes: 3

Views: 702

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65334

Create a combined index on all columns, then INSERT IGNORE or REPLACE INTO according to your needs.

From the docs:

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.

Upvotes: 2

Related Questions