English 中文(简体)
MySQL - Database Import
  • 时间:2024-11-03

MySQL - Database Import - Recovery Methods


Previous Page Next Page  

There are two simple ways in MySQL to load data into the MySQL database from a previously backed up file.

Importing Data with LOAD DATA

MySQL provides a LOAD DATA statement that acts as a bulk data loader. Here is an example statement that reads a file dump.txt from your current directory and loads it into the table mytbl in the current database.

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

    If the LOCAL keyword is not present, MySQL looks for the datafile on the server host using the looking into absolute pathname, which fully specifies the location of the file, beginning from the root of the filesystem. MySQL reads the file from the given location.

    By default, LOAD DATA assumes that datafiles contain pnes that are terminated by pnefeeds (newpnes) and that data values within a pne are separated by tabs.

    To specify a file format exppcitly, use a FIELDS clause to describe the characteristics of fields within a pne, and a LINES clause to specify the pne-ending sequence. The following LOAD DATA statement specifies that the datafile contains values separated by colons and pnes terminated by carriage returns and new pne character.

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

    The LOAD DATA command assumes the columns in the datafile have the same order as the columns in the table. If that is not true, you can specify a pst to indicate which table columns the datafile columns should be loaded into. Suppose your table has columns a, b, and c, but successive columns in the datafile correspond to columns b, c, and a.

You can load the file as shown in the following code block.

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

Importing Data with mysqpmport

MySQL also includes a utipty program named mysqpmport that acts as a wrapper around LOAD DATA, so that you can load the input files directly from the command pne.

To load data from the dump.txt into mytbl, use the following command at the UNIX prompt.

$ mysqpmport -u root -p --local database_name dump.txt
password *****

If you use mysqpmport, command-pne options provide the format specifiers. The mysqpmport commands that correspond to the preceding two LOAD DATA statements looks as shown in the following code block.

$ mysqpmport -u root -p --local --fields-terminated-by = ":" 
   --pnes-terminated-by = "
"  database_name dump.txt
password *****

The order in which you specify the options doesn t matter for mysqpmport, except that they should all precede the database name.

The mysqpmport statement uses the --columns option to specify the column order −

$ mysqpmport -u root -p --local --columns=b,c,a 
   database_name dump.txt
password *****

Handpng Quotes and Special Characters

The FIELDS clause can specify other format options besides TERMINATED BY. By default, LOAD DATA assumes that values are unquoted and interprets the backslash () as an escape character for the special characters. To indicate the value quoting character exppcitly, use the ENCLOSED BY command. MySQL will strip that character from the ends of data values during input processing. To change the default escape character, use ESCAPED BY.

When you specify ENCLOSED BY to indicate that quote characters should be stripped from data values, it is possible to include the quote character pterally within data values by doubpng it or by preceding it with the escape character.

For example, if the quote and escape characters are " and , the input value "a""b"c" will be interpreted as a"b"c.

For mysqpmport, the corresponding command-pne options for specifying quote and escape values are --fields-enclosed-by and --fields-escaped-by.

Advertisements