SQL LOAD DATA INFILE问题

时间:2011-03-09 16:17:19

标签: mysql sql auto-increment load-data-infile

如何获取LOAD DATA INFILE命令以跳过某些字段。

假设我有下表:

CREATE TABLE stuff (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    name CHAR(64) NOT NULL,
    junk CHAR(64) NOT NULL,
    a ENUM('N','Y') NOT NULL DEFAULT 'N',
    PRIMARY KEY (id)
);

我的档案:

,name1,junk1,
,name2,junk2,
,name3,junk3,Y

声明:

LOAD DATA INFILE 'myfile' INTO TABLE stuff FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

自动跳过自动增量并正确处理?它似乎正确处理所有事情,但这会产生以下警告:

Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 1
Warning (Code 1265): Data truncated for column 'a' at row 1
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 2
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 3
Warning (Code 1265): Data truncated for column 'a' at row 3
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 4

删除开头的逗号不会正确输入数据。

1 个答案:

答案 0 :(得分:3)

您可以将id显式设置为NULL,以便使用自动增量值。

LOAD DATA INFILE 'myfile' 
INTO TABLE stuff 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n'
SET id = NULL;

我还建议删除第一个逗号并明确列出文件中的列:

LOAD DATA INFILE 'myfile' 
INTO TABLE stuff 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n'
(name,junk,a)
SET id = NULL;