MySQL

MySQL IntroductionComment MySQLInstall MySQL container with Docker-ComposeMySQL AdminMySQL ALTER TABLEMySQL ArithmeticMySQL BackticksMySQL Backup using mysqldumpMySQL Change PasswordMySQL Character Sets and CollationsMySQL clientMySQL ClusteringMySQL Configuration and tuningMySQL Connecting with UTF-8 Using Various Programming language.MySQL Converting from MyISAM to InnoDBMySQL Create New UserMySQL Creating databasesMySQL Customize PS1MySQL Data TypesMySQL Date and Time OperationsMySQL Dealing with sparse or missing dataMySQL DELETEMySQL Drop TableMySQL Dynamic Un-Pivot Table using Prepared StatementMySQL ENUMMySQL Error 1055: ONLY FULL GROUP BY: something is not in GROUP by clause ...MySQL Error codesMySQL EventsMySQL Extract values from JSON typeMySQL Full-Text searchMySQL Group byMySQL Handling Time ZonesMySQL Indexes and KeysMySQL INSERTMySQL JoinsMySQL JOINS: Join 3 table with the same name of id.MySQL JSONMySQL Limit and OffsetMySQL LOAD DATA INFILEMySQL LOCK TABLEMySQL Log filesMySQL Many-to-many Mapping tableMySQL MyISAM EngineMySQL mysqlimportMySQL NULLMySQL One to ManyMySQL ORDER byMySQL PartitioningMySQL Performance TipsMySQL Performance TuningMySQL Pivot queriesMySQL PREPARE StatementsMySQL Recover from lost root passwordMySQL Regular ExpressionsMySQL ReplicationMySQL Reserved WordsMySQL Security via GRANTsMySQL SELECTMySQL Server InformationMySQL SSL Connection SetupMySQL Stored routines (procedures and functions)MySQL String operationsMySQL Table CreationMySQL Temporary TablesMySQL Time with subsecond precisionMySQL TransactionMySQL TRIGGERSMySQL UNIONMySQL UnionsMySQL UPDATEMySQL Using VariablesMySQL VIEWRecover and reset the default root password for MySQL 5.7+



MySQL mysqlimport

From WikiOD

Parameters[edit | edit source]

Parameter Description
--delete -D empty the table before importing the text file
--fields-optionally-enclosed-by define the character that quotes the fields
--fields-terminated-by field terminator
--ignore -i ignore the ingested row in case of duplicate-keys
--lines-terminated-by define row terminator
--password -p password
--port -P port
--replace -r overwrite the old entry row in case of duplicate-keys
--user -u username
--where -w specify a condition

Remarks[edit | edit source]

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

Basic usage[edit | edit source]

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[edit | edit source]

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[edit | edit source]

This example is useful for windows-like endings:

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

Handling duplicate keys[edit | edit source]

Given the table Employee

id Name
3 Yooden 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
id Name
1 Arthur Dent
2 Marvin
3 Yooden Vranx

The --replace option will overwrite the old entry

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

Conditional import[edit | edit source]

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

Import a standard csv[edit | edit source]

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

Credit:Stack_Overflow_Documentation