mysqlimport NULL values when importing from delimi

2019-07-27 14:50发布

问题:

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