The load command usage method in Mysql _mysql

Source: Internet
Author: User
Tags access database mysql command line

Use the load command in MySQL to load the contents of the TXT file into a database table, for example, to create a table, the name is User, a field username; there is currently a db.txt file with a space-separated username, for example: Xiaowang Xiaoliu Zhangsan

Load the file into datasheet user, using the command:

Copy Code code as follows:

Load data local infile "/home/beaver/db" to table user lines terminated by ';


Author Beaver ' s

LOAD DATA INFILE

This is the last way we're going to introduce the data into the MySQL database. This command is very similar to Mysqlimport, but this method can be used on the MySQL command line. That means you can use this command in all programs that use APIs. With this approach, you can import the data you want to import into your application. Before using this command, the MYSQLD process (service) must already be running.

To start the MySQL command line:

Bin/mysql–p

Press the prompt to enter the password, after successfully entered the MySQL command line, enter the following command:
Use Meet_a_geek;
LOAD DATA INFILE "/home/mark/data.sql" into TABLE Orders;

Simply put, this will import the contents of the file Data.sql into the table orders, like the Mysqlimport tool, and there are some parameters to choose from. For example, you need to import data from your computer to a remote database server, and you can use the following command:
LOAD DATA local INFILE "C:\MyDocs\SQL.txt" into TABLE Orders;

The local parameter above indicates that the file is a native file and the server is the server you are logging on to. This eliminates the use of FTP to upload files to the server, MySQL for you to complete. You can also set the priority of the INSERT statement, if you want to mark it as low priority (low_priority), then MySQL will wait until no one else reads the table to insert the data. You can use the following command:
LOAD DATA low_priority INFILE "/home/mark/data.sql" into TABLE Orders;

You can also specify whether to replace or ignore duplicate key values in the file and datasheet when inserting data. Syntax to override duplicate key values:
LOAD DATA low_priority INFILE "/home/mark/data.sql" REPLACE into TABLE Orders;
The above sentence looks a bit clumsy, but puts the keyword in the place where your profiler can understand it.

The following pair of options describes the file's record format, which is also available in the Mysqlimport tool. They look a little different here. First, to use the fields keyword, if you use this keyword, the MySQL profiler would like to see at least one of the following options:
Terminated by character
Enclosed by character
Escaped by character

These keywords are the same as their parameters and their usage in Mysqlimport. The terminated by describes the separator for the field, which is the tab character by default (\ t)
Enclosed by describes the enclosing character of the field. For example, enclose each field in quotation marks.
Escaped by description of the escape character. The default is the Counter bar (backslash:\).

The following example of the previous Mysqlimport command is still used to import the same file into the database using the load DATA infile statement:
The LOAD DATA INFILE "/home/mark/orders.txt" REPLACE into the TABLE Orders FIELDS terminated by ', ' enclosed by ' ";

There is no feature in a Mysqlimport tool in the LOAD DATA infile statement:

The LOAD DATA INFILE can import files into the database according to the specified columns. This feature is important when we want to import part of the data into the content. For example, when upgrading from an Access database to a MySQL database, we need to add some columns (column/field/field) to the MySQL database to accommodate some additional requirements.

At this point, the data in our Access database is still available, but because the column (field) of the data is no longer matched in MySQL, the Mysqlimport tool cannot be used again. Nevertheless, we can still use the load data INFILE, and the following example shows how to import data into a specified column (field):
LOAD DATA INFILE "/home/order.txt" into TABLE Orders (Order_number, order_date, customer_id);

As you can see, we could specify the required columns (fields). These specified fields are still enclosed in parentheses, separated by commas, and if you omit any of them, MySQL will remind you ^_^

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.