由于 NULL 值,将 csv 保存到 MySQL table 时出错

Error in saving csv to MySQL table due to NULL values

我已将数据保存在我的 csv excel 中。数据在行和列中有一些空值。我想将此数据保存到 MySQL 中的数据库中。但是空值导致将数据保存到 MySQL 时出现问题。这是创建 table -

的查询
create table student (
  Std_ID int, 
  Roll_NO int, 
  First_Name varchar(10) NOT NULL, 
  Last_Name varchar(10), 
  Class int, 
  constraint test_student primary key (Std_ID)
); 

...它 运行 成功了。现在我想使用查询 -

将我的数据从 csv 保存到这个 table
load data infile 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\new.csv' into table student fields terminated by ',' lines terminated by '\n' ignore 1 lines; 

...并给我错误消息 -

ERROR 1366 (HY000): Incorrect integer value: '' for column 'XXX' at row X. 

您可以使用 this 数据作为参考。 同样可以在下面找到。

LOAD DATA INFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\new.csv' 
INTO TABLE student 
FIELDS TERMINATED BY ',' 
LINES TERMINATED by '\n' 
IGNORE 1 LINES
-- specify columns, use variables for the columns where incorrect value may occur
(Std_ID, @Roll_NO, First_Name, Last_Name, @Class)
-- use preprocessing, replace empty string with NULL but save any other value
SET Roll_NO = NULLIF(@Roll_NO, ''),
    Class = NULLIF(@Class, ''); 

如果 CSV 中的某些列为空字符串,则 NULL 值将插入到 table 的相应列中。

Std_ID没有预处理,因为定义为PRIMARY KEY,不能为NULL。


更新

OP提供源文件示例。在 HEX 模式下查看显示该文件是 Windows-style 文本文件,因此行终止符是 '\r\n'。修改后文件导入成功