load data infile
阿新 • • 發佈:2017-09-14
mina lds 文件的 斜杠 enc closed line txt ignore 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,...]
starting by指定每行開始的位置,跟字符有關。
terminated by 指定列的分隔符,默認是Tab符(\t)。
enclosed by 指定列的包括符,默認為空。
escaped by 轉義字符,默認的是反斜杠(‘\‘)。
IGNORE number {LINES | ROWS} :忽略數據文件的前N行。
如果你指定關鍵詞low_priority,那麽MySQL將會等到沒有其他人讀這個表的時候,才把插入數據。可以使用如下的命令:
load data low_priority infile "/tmp/data sql" into table Orders;
如果指定local關鍵詞,則表明從客戶主機讀文件。如果local沒指定,文件必須位於服務器上。
replace和ignore關鍵詞控制對現有的唯一鍵記錄的重復的處理。如果你指定replace,新行將代替有相同的唯一鍵值的現有行。
如果你指定ignore,跳過有唯一鍵的現有行的重復行的輸入。如果你不指定任何一個選項,當找到重復鍵時,出現一個錯誤,並且文本文件的余下部分被忽略。例如:
load data low_priority infile "/tmp/data sql" replace into table Orders;
關於字符集的處理,
範例一:
load data infile ‘/tmp/test.txt‘ into table test charset gbk fields terminated by ‘,‘;
範例二:
先設置字符集,再執行導入語句:
set character_set_database=gbk;
表中的列比數據文件的列多,處理範例:
$more /tmp/test.csv
10,"zhangsan","[email protected]",25
11,"lisi","[email protected]",26
12,"wangwu","[email protected]",27
load data infile ‘/tmp/test.csv‘
into table test
FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘"‘
(id,name,ename,age);
表中的列比數據文件的列少,並且最後兩列也不對應,處理範例:
$more /tmp/test.csv
10,"zhangsan",25,"[email protected]",13000000000,0
11,"lisi",26,"[email protected]",13000000001,5
12,"wangwu",27,"[email protected]",13000000002,2
load data infile ‘/tmp/test.csv‘
into table test
FIELDS TERMINATED BY ‘,‘ ENCLOSED BY ‘"‘
(id,name,age,ename,@tmp_sex,@tmp_tel,status)
set sex-if(@tmp_sex=‘男‘,0,1;
load data infile