mysqlimport

Other topics

Remarks:

mysqlimport will use the name of the imported file, after stripping the extension, to determine the destination table.

Basic usage

Given the tab-separated file employee.txt

1 \t Arthur Dent
2 \t Marvin
3 \t Zaphod Beeblebrox

$ mysql --user=user --password=password mycompany -e 'CREATE TABLE employee(id INT, name VARCHAR(100), PRIMARY KEY (id))'

$ mysqlimport --user=user --password=password mycompany employee.txt

Using a custom field-delimiter

Given the text file employee.txt

1|Arthur Dent
2|Marvin
3|Zaphod Beeblebrox

$ mysqlimport --fields-terminated-by='|' mycompany employee.txt

Using a custom row-delimiter

This example is useful for windows-like endings:

$ mysqlimport --lines-terminated-by='\r\n' mycompany employee.txt

Handling duplicate keys

Given the table Employee

idName
3Yooden Vranx

And the file employee.txt

1 \t Arthur Dent
2 \t Marvin
3 \t Zaphod Beeblebrox

The --ignore option will ignore the entry on duplicate keys

$ mysqlimport --ignore mycompany employee.txt
idName
1Arthur Dent
2Marvin
3Yooden Vranx

The --replace option will overwrite the old entry

$ mysqlimport --replace mycompany employee.txt
idName
1Arthur Dent
2Marvin
3Zaphod Beeblebrox

Conditional import

$ mysqlimport --where="id>2" mycompany employee.txt

Import a standard csv

$ mysqlimport
    --fields-optionally-enclosed-by='"'
    --fields-terminated-by=,
    --lines-terminated-by="\r\n"
    mycompany employee.csv

Parameters:

ParameterDescription
--delete -Dempty the table before importing the text file
--fields-optionally-enclosed-bydefine the character that quotes the fields
--fields-terminated-byfield terminator
--ignore -iignore the ingested row in case of duplicate-keys
--lines-terminated-bydefine row terminator
--password -ppassword
--port -Pport
--replace -roverwrite the old entry row in case of duplicate-keys
--user -uusername
--where -wspecify a condition

Contributors

Topic Id: 5215

Example Ids: 18463,18464,18465,18466,18467,18468

This site is not affiliated with any of the contributors.