Monday 26 October 2015

Load Data in mysql

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed.

LOAD DATA INFILE Syntax

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]


Example 

LOAD DATA INFILE '<Filename>' INTO TABLE <Table Name>;

LOAD DATA LOCAL INFILE '<Filename>' INTO TABLE <Table Name> FIELDS TERMINATED BY '<Column Delimiter>' IGNORE 1 LINES;

About the Command Keywords 

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed.

LOCAL : When using LOCAL with LOAD DATA, a copy of the file is created in the server's temporary directory.Lack of sufficient space for the copy in this directory can cause the LOAD DATA LOCAL statement to fail.

FIELDS : If you specify a FIELDS clause, each of its subclauses (TERMINATED BY, [OPTIONALLY] ENCLOSED BY, and ESCAPED BY) is also optional, except that you must specify at least one of them.

If you specify no FIELDS or LINES clause, the defaults are the same as if you had written this:

FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''


IGNORE : The IGNORE number LINES option can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip over an initial header line containing column names

When the LOAD DATA INFILE statement finishes, it returns an information string in the following format:

Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

You can use SHOW WARNINGS to get a list of the first max_error_count warnings as information about what went wrong


These is about the load data command that we used widely.

For in-depth knowledge of this command please refer to below url : click here


No comments:

Post a Comment