Search code examples
mariadbmariadb-10.2

sync table from a csv?


I have a CSV which I'd like to sync with an existing table. The table should update with the values from the CSV or insert new rows if a key isn't found. Is this possible? I can't seem to find anything on it, other than simply importing the csv. Only thing I can think of is writing a script in another language to go line by line of the csv, and check each row that way. Is there a way to achieve this with just a single sql script?


Solution

  • If you use LOAD DATA INFILE on a table with a Unique index or Primary key, new records will be inserted.

    Duplicates can be handled in one of three ways:

    • Using the REPLACE keyword, will replace the entire row with the incoming data
    • Using the IGNORE keyword will discard the incoming row.
    • With neither keyword, a duplicate will throw an error and the LOAD will stop.

    If you want more control over how to handle duplicates (updating just one column in an existing row, for example) you'll need to write something to read the CSV file and insert the rows, one by one.

    Reference: https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-error-handling