J. Doe
J. Doe

Reputation: 55

Is there a way to overwrite a table with LOAD DATA LOCAL INFILE?

I have a CSV file that I am loading into my database. I want the previous data in the table to be overwritten and not appended every time I load my CSV file. Is it possible to do this within a single query?

Is the only solution to TRUNCATE the table and then utilize the LOAD DATA INFILE queries?

Upvotes: 0

Views: 1718

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269733

Assuming you have a primary key, you can use REPLACE. As the documentation states:

The REPLACE and IGNORE modifiers control handling of input rows that duplicate existing rows on unique key values:

  • If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row. See Section 13.2.9, “REPLACE Statement”.

However, if you want to replace the existing table, then truncate the table first and then load.

Upvotes: 2

Related Questions