mysqlimport NULL values when importing from delimited file

懵懂的女人 提交于 2019-12-11 10:44:29

问题


I am running mysqlimport to load data and am having an issue with empty values not being loaded as null. I am using mysql 5.7.23

Issue can be recreated by creating a file named mytable.psv, containing:

code|date_a|date_b
1|2018-11-27|2018-11-27
2|2018-11-27|

Then run the commands:

mysql -u root -e "CREATE DATABASE mydb"
mysql -u root -e "USE mydb; CREATE TABLE mytable (code varchar(15) NOT NULL, date_a date NOT NULL, date_b date);"
mysqlimport --ignore-lines=1 --fields-terminated-by='|' --columns="code,date_a,date_b" --local -u root mydb mytable.psv
mysql -u root -e "USE mydb; SELECT * FROM mytable;"

The output is:

mydb.mytable: Records: 2  Deleted: 0  Skipped: 0  Warnings: 1
+------+------------+------------+
| code | date_a     | date_b     |
+------+------------+------------+
| 1    | 2018-11-27 | 2018-11-27 |
| 2    | 2018-11-27 | 0000-00-00 |
+------+------------+------------+

You can see that date_b for the second row is empty and I need that to be a null but instead it transforms into 0000-00-00. This is breaking constraints that I need further along. Is there a way to ensure empty values are null when importing data from a delimited file?


回答1:


I was able to find answers here: https://dev.mysql.com/doc/refman/8.0/en/mysqlimport.html#option_mysqlimport_lines-terminated-by https://dev.mysql.com/doc/refman/8.0/en/load-data.html You need the characters '\N' to import a null so in the above example it would be:

code|date_a|date_b
1|2018-11-27|2018-11-27
2|2018-11-27|\N


来源:https://stackoverflow.com/questions/53490398/mysqlimport-null-values-when-importing-from-delimited-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!