MySQL 4 methods to import data

MySQL 4 methods to import data

1. Import mysql command

The mysql command import syntax is:

mysql -u username -p password < database data to be imported (runoob.sql)

Examples:

# mysql -uroot -p123456 < runoob.sql

The above command will import the entire backed up database runoob.sql.

2. Import with source command

To import a database using the source command, you need to log in to the database terminal first:

mysql> create database abc; # Create a databasemysql> use abc; # Use the created databasemysql> set names utf8; # Set the encodingmysql> source /home/abc/abc.sql # Import the backup database

3. Import data using LOAD DATA

MySQL provides the LOAD DATA INFILE statement to insert data. The following example reads the dump.txt file from the current directory and inserts the data in the file into the mytbl table of the current database.

mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl;

If the LOCAL keyword is specified, it indicates that the file is read from the client host according to the path. If not specified, the file is read from the server at path.

You can explicitly specify column value delimiters and end-of-row markers in the LOAD DATA statement, but the default markers are tabs and newlines.

The syntax of the FIELDS and LINES clauses is the same for both commands. Both clauses are optional, but if both are specified, the FIELDS clause must appear before the LINES clause.

If the user specifies a FIELDS clause, its clauses (TERMINATED BY, [OPTIONALLY] ENCLOSED BY, and ESCAPED BY) are also optional, however, the user must specify at least one of them.

mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl
 -> FIELDS TERMINATED BY ':'
 -> LINES TERMINATED BY '\r\n';

By default, LOAD DATA inserts data in the order of the columns in the data file. If the columns in the data file are inconsistent with the columns in the table being inserted, you need to specify the order of the columns.

For example, the column order in the data file is a, b, c, but the column order in the insert table is b, c, a. The data import syntax is as follows:

mysql> LOAD DATA LOCAL INFILE 'dump.txt' 
  -> INTO TABLE mytbl (b, c, a);

4. Import data using mysqlimport

The mysqlimport client provides a command-line interface to the LOAD DATA INFILEQL statement. Most options for mysqlimport correspond directly to the LOAD DATA INFILE clause.

To import data from the dump.txt file into the mytbl table, use the following command:

$ mysqlimport -u root -p --local mytbl dump.txt
password *****

The mysqlimport command can specify options to set the specified format. The command statement format is as follows:

$ mysqlimport -u root -p --local --fields-terminated-by=":" \
  --lines-terminated-by="\r\n" mytbl dump.txt
password *****

Use the --columns option in the mysqlimport statement to set the order of the columns:

$ mysqlimport -u root -p --local --columns=b,c,a \
  mytbl dump.txt
password *****

Introduction to common options of mysqlimport

Options Function
-d or --delete Delete all information in the data table before importing new data into the data table
-f or --force Regardless of whether errors are encountered, mysqlimport will continue to insert data.
-i or --ignore mysqlimport skips or ignores rows that have the same unique key; the data in the import file is ignored.
-l or -lock-tables Locking the table before data is inserted prevents user queries and updates from being affected while you are updating the database.
-r or -replace This option has the opposite effect to the -i option; it replaces records in the table that have the same unique key.
--fields-enclosed-by= char What is used to enclose the data records in the specified text file? In many cases, the data is enclosed in double quotes. By default, data is not enclosed in characters.
--fields-terminated-by=char Specifies the separator between each data value. In a period-delimited file, the separator is a period. You can use this option to specify the separator between data. The default separator is the tab character.
--lines-terminated- by=str This option specifies the string or character that separates data between lines in a text file. By default, mysqlimport uses newline as the line separator. You can choose to replace a single character with a string: a newline or a carriage return.

The above is the detailed content of the method of importing data into MySQL. For more information about importing data into MySQL, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Several different ways to import data into MYSQL
  • Summary of six ways to optimize MySQL database
  • Several ways to backup MySql database
  • Summary of several ways to overwrite and import databases in MySQL

<<:  A useful mobile scrolling plugin BetterScroll

>>:  Detailed explanation of nginx configuration file interpretation

Recommend

MySQL 5.6 binary installation process under Linux

1.1 Download the binary installation package wget...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

Detailed steps to implement the Excel import function in Vue

1. Front-end-led implementation steps The first s...

Use Docker to create a distributed lnmp image

Table of contents 1. Docker distributed lnmp imag...

CSS navigation bar menu with small triangle implementation code

Many web pages have small triangles in their navi...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

Summary of various postures of MySQL privilege escalation

Table of contents 1. Write Webshell into outfile ...

Detailed explanation of the construction and use of docker private warehouse

1. Download the repository image docker pull regi...

Introduction to JWT Verification Using Nginx and Lua

Table of contents Preface Lua Script nignx.conf c...

foreman ubuntu16 quick installation

Quickstart Guide The Foreman installer is a colle...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157 Pref...

How to implement an array lazy evaluation library in JavaScript

Table of contents Overview How to achieve it Spec...