LOAD DATA导入数据语法:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE ‘file_name‘ [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partition_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 | ROWS}] [(col_name_or_user_var,...)] [SET col_name = expr,...]
FIELDS和LINES都是可选的,但是如果都指定了,FIELDS必须在LINES之前。
本地用一个pet表,结构如下:
+---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+
要导入的数据:pet.txt
Fluffy,Harold,cat,f,1993-02-04 Claws,Gwen,cat,m,1994-03-17 Buffy,Harold,dog,f,1989-05-13 Fang,Benny,dog,m,1990-08-27 Bowser,Diane,dog,m,1979-08-31,1995-07-29 Chirpy,Gwen,bird,f,1998-09-11 Whistler,Gwen,bird, ,1997-12-09 Slim,Benny,snake,m,1996-04-29
导入命令:
LOAD DATA LOCAL INFILE ‘/home/scott/MySql/pet.txt‘ IGNORE INTO TABLE pet fields TERMINATED BY ‘,‘ lines TERMINATED BY ‘\n‘;
导入结果:
mysql> select * from pet; +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1990-08-27 | NULL | | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+------------+
时间: 2024-10-31 04:00:41