MySQL database import and export methods and tool introduction _ MySQL

Source: Internet
Author: User
1. mysqlimport syntax introduction: mysqlimport is located in the mysqlbin directory and is a very effective tool for mysql to load (or import) data. This is a command line tool. There are two parameters and a large number of options to choose from. This tool imports a text file to your specified database and table. For example, we want to use the mysql management tool.

1. mysqlimport syntax introduction:

Mysqlimport is located in the mysql/bin directory and is a very effective tool for mysql to load (or import) data. This is a command line tool. There are two parameters and a large number of options to choose from. This tool imports a text file into your specified database and table. In other words, we want to import data from the Customers.txt file to the Custermers table in the Meet_A_Geek database: mysqlimport Meet_A_Geek Customers.txt.

Note: customers.txt is the text file for data import, while Meet_A_Geek is the database to be operated, and the table name in the database is Customers, the data format of the text file must be the same as that of the record in the MERS table. Otherwise, the mysqlimport command will fail.

The table name is the file string before the first period (.) of the imported file. Another example is as follows:

Mysqlimport Meet_A_Geek Cus.to.mers.txt.

Then we will import the content in the file to the Cus table in the database Meet_A_Geek.

In the above example, only two parameters are used and no more options are used. The following describes the options of mysqlimport.

2. Introduction to common mysqlimport options:

Option

-D or -- delete all information in the data table before the new data is imported into the data table.

-F or -- force: no matter whether an error occurs or not, mysqlimport forcibly inserts data.

-I or -- ignore mysqlimport skips or ignores rows with the same unique keyword. data in the import file will be ignored.

-L or-lock-tables: the table is locked before data is inserted. This prevents you from affecting your queries and updates when updating the database.

-R or-replace is opposite to-I. This option replaces records with the same unique keywords in the table.

-- Fields-enclosed-by = char specifies the data record in a text file. in many cases, the data is enclosed by double quotation marks. By default, data is not enclosed by characters.

-- Fields-terminated-by = char specifies the delimiter between values of each data. in a file separated by periods, the delimiter is a period. You can use this option to specify the delimiter between data. The default delimiter is a Tab)

-- Lines-terminated-by = str this option specifies the data separator string or character between the row and row in a text file. By default, mysqlimport uses newline as the line separator.

You can use a string to replace a single character:

A new line or press Enter.

Common options of the mysqlimport command include-v display version and-p prompt for password.

3. example: import a file with a comma as the separator

The record format of the file row is as follows: "1", "ORD89876", "1 Dozen Roses ", "19991226" Our task is to import the data in this file to the table Orders in the database Meet_A_Geek,

We use this command:

     bin/mysqlimport –prl –fields-enclosed-by=" –fields-terminated-by=, Meet_A_Geek Orders.txt

This command may seem uncomfortable, but it is very simple when you are familiar with it.

The first part is bin/mysqlimport, which tells the operating system that the command you want to run is mysqlimport in the mysql/bin directory. option p requires a password, this requires you to enter the password before changing the database, which makes the operation safer. We use the r option because we want to replace the record with a unique keyword that already exists in the file record with the data in the file. The data in our form is not up-to-date and needs to be updated using the data in the file. Therefore, we use the r option to replace the existing records in the database. L option is used to lock the table when we insert data, which prevents users from querying or changing the table when we update the table.

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.