Skip to main content
Version: V1.4.1

LOAD DATA

Description​

LOAD DATA imports data from a text file into a table. The statement supports two file locations:

  • LOAD DATA INFILE: Reads a file from a filesystem accessible to the seekdb server.
  • LOAD DATA LOCAL INFILE: Reads a local file through the client, which sends its contents to seekdb.

Syntax​

LOAD DATA
[LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE table_name
[PARTITION (partition_name [, partition_name] ...)]
[COMPRESSION [=] {AUTO | NONE | GZIP | DEFLATE | ZSTD}]
[CHARACTER SET {binary | utf8mb4}]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number {LINES | ROWS}]
[(column_name_or_user_variable
[, column_name_or_user_variable] ...)]
[SET column_name = {expression | DEFAULT}
[, column_name = {expression | DEFAULT}] ...]
[LOGFILE [=] 'log_file_name']
[REJECT LIMIT [=] integer]
[BADFILE [=] 'bad_file_name'];

Parameters​

ParameterDescription
LOCALReads a file from the client's local filesystem. If omitted, seekdb reads from the server filesystem. Specifying LOCAL has the same duplicate-key behavior as specifying IGNORE.
file_nameThe input file path and name. Server-side imports are restricted by the secure_file_priv system variable.
REPLACEWhen a primary-key or unique-key conflict occurs, replaces the existing row with the row from the input file.
IGNOREWhen a primary-key or unique-key conflict occurs, ignores the conflicting row from the input file.
table_nameThe target table. It cannot have an INSERT or UPDATE trigger.
PARTITIONImports data only into the listed partitions or subpartitions. Separate multiple names with commas.
COMPRESSIONThe compression format of the input file. AUTO detects the format from the filename extension. You can also specify NONE, GZIP, DEFLATE, or ZSTD.
CHARACTER SETThe input file character set. Valid values are binary and utf8mb4.
FIELDS, COLUMNSSpecifies field delimiters, enclosure characters, and escape characters. FIELDS and COLUMNS are equivalent.
LINESSpecifies the line prefix and line terminator.
IGNORE number LINESSkips a number of lines at the beginning of the file, commonly to skip a header row. ROWS and LINES are equivalent.
Column or user-variable listMaps input fields, in order, to target columns or user variables.
SETAssigns an expression, user variable, or DEFAULT to a target column.
LOGFILESpecifies the import log file.
REJECT LIMITSpecifies the number of data-error rows allowed.
BADFILESpecifies a file in which to record rejected rows.

Considerations​

  • A server-side import requires the FILE privilege and the INSERT privilege on the target table. The file path must satisfy secure_file_priv.
  • A client-side import requires the INSERT privilege on the target table, and the client connection must enable local file loading. For example, start the MySQL client with --local-infile=1.
  • LOAD DATA LOCAL INFILE handles duplicate keys with IGNORE semantics.
  • \N represents NULL.
  • Field and line delimiters currently support only single-byte ASCII characters.

Examples​

Import a server-side file​

The following statement imports /data/student.csv on the server into student and skips its header row:

LOAD DATA INFILE '/data/student.csv'
INTO TABLE student
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Import a client-side file​

Start a client that supports local file loading:

mysql --local-infile=1 -h127.0.0.1 -P2881 -uroot -p

Run the import:

LOAD DATA LOCAL INFILE '/home/user/student.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

Transform data with a user variable​

LOAD DATA LOCAL INFILE '/home/user/student.csv'
INTO TABLE student
FIELDS TERMINATED BY ','
(id, name, @score)
SET score = CAST(@score AS SIGNED);