How to import data using the load data command in MySQL

Source: Internet
Author: User

The load data command is a self-contained data import and export command in mysql. It can quickly import tens of millions of data records without getting stuck. Next I will introduce the usage of the load data command.

Load data syntax description:

Load data infile syntax

Load data [LOW_PRIORITY] [LOCAL] INFILE 'file_name.txt '[REPLACE | IGNORE]
Into table tbl_name
[FIELDS
[Terminated by 'T']
[OPTIONALLY] enclosed by '']
[Escaped by '\']
[Lines terminated by 'n']
[IGNORE number LINES]
[(Col_name,...)]

The simplest method

The Code is as follows: Copy code

Mysql> load data local infile "D:/AB .txt" into table mytbl (name, age );


MySQL supports data import from the load data command. This method is more efficient than direct insert. According to the official statement, it is 20 times faster than insert statements. The usage is as follows:

The Code is as follows: Copy code
Mysql> load data local infile "D:/AB .txt" into table mytbl (name, age );

Using the preceding command, you can import the content of the D:/AB .txt file to mytbl. the nameand ageare the fields of mytbl, and correspond to the data in each row in the AB .txt file. If-enable-local-infile is not specified during mysql compilation and installation, the following error is reported when you use the preceding command:

ERROR 1148 (42000): The used command is not allowed with this MySQL version

There are two solutions: one is to re-compile and install the above parameters, but directly run the command line, as shown below:

The Code is as follows: Copy code

Mysql-uroot-proot mydb_name -- local-infile = 1-e 'Load data local infile "D:/AB .txt" into table mytbl (name, age )'

I tested it. It took about three minutes to use the load data command to import 3 million data records!

Some common errors during import

System Error:

Data truncated for column 'f3 'at row 1

It may be because the field is defined as double type, and the text file is null, the text file is updated:

Abc; cdef; 2;

Import again with the following error:

Row 1 doesn't contain data for all columns

The reason for analysis may be that there are five fields in the table, and there are four fields in the text data source, with one less field.

Data truncated for column 'f3 'at row 1,

I understand that the system should handle this by null value, that is, the default value of this field in the database is "-1 ",

Solution

The Code is as follows: Copy code

Load data infile 'd: \ my_program \ Tek_sig \ example_data1.txt'
Into table TABLE_NAME
Fields terminated ';'
Lines terminated by 'n'
(F1, f2, f3, f4)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.