I would like to understand how to create a decent back-up file by using mysql
and mysqlimport
. I know that I can use mysqldump
and mysql
, but I'm learning to use the combination: mysql
and mysqlimport
.
I'm on mysql5.7.21 in Windows 10.
I have test table: aircrafts that looks like this:
I would like to export this table to a textfile trough the mysql
client tool.
So I do this:
mysql -h localhost -u root -D julian -p -e "SELECT * FROM `aircrafts` " > aircrafts.txt
This creates the back-up file, and content of it is:
id countryFactoryID name type price
1 1 Cessna 172 Skyhawk GA 60000
2 43 Diamond DA40 GA NULL
3 1 Cirrus SR20 GA NULL
4 1 Piper PA-28 Cherokee GA NULL
5 1 Beechcraft A23 Musketeer GA NULL
6 1 Grumman American AA-5 Traveller GA NULL
What I noticed about the back-up file is that it uses NULL
instead of \N
.
But that's ok I guess.
Next, I import the back-up file like this:
mysqlimport --local -l -v --ignore-lines=1 -p -u root myDB aircrafts.txt
The output in CLI is:
Connecting to localhost
Selecting database myDB
Locking tables for write
Loading data from LOCAL file: aircrafts.txt into aircrafts
myDB.aircrafts: Records: 6 Deleted: 0 Skipped: 0 Warnings: 6
Disconnecting from localhost
Ok, so far so good.
When I look in phpmyadmin, then I see this:
As you can see, the price is 0
, but I would like to have a NULL-value, like in the first image. It's strange because my default value for column price is NULL.
What am I doing wrong?
Best Answer
This issue is caused by the way MySQL LOAD DATA handles NULLs:
In your case NULL is considered as word 'NULL' and if you try to cast 'NULL' to number it will return 0. You need to change NULL to \N or enclose strings using some character.