简体   繁体   中英

How to LOAD DATA setting default value NULL if column field is empty?

How can set missing csv field values to sql NULL during LOAD DATA ? Problem in the following example is, that mysql won't let me import the file if dob field offers only a blank:

create table persons (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    dob date DEFAULT NULL,
    lastname varchar(20) NOT NULL,
    //in real world having 100 columns!
    PRIMARY KEY (id)
);

import.csv with missing birthday:

1;;doe
2;;dane
...

Import million rows:

LOAD DATA INFILE 'import.csv' INTO TABLE persons FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n';

I could of course use:

 (@col1, @col2... @col100) 
        SET date = nullif(@col2,''),
            lastfield = nullif(@col100,'')
        ;

But that would imply I had to repeat all the columns explicit, which I try to avoid for maintenance reasons. What are my chances?

Or could I somehow only define the non-char fields and use nullif on them?

(@col2)
   SET date = nullif(@col2,'');

And leave the rest as is?

From the reference manual of MySQL 8.0

Handling of NULL values varies according to the FIELDS and LINES options in use:

  • For the default FIELDS and LINES values, NULL is written as a field value of \N for output, and a field value of \N is read as NULL for input (assuming that the ESCAPED BY character is \).

  • If FIELDS ENCLOSED BY is not empty, a field containing the literal word NULL as its value is read as a NULL value. This differs from the word NULL enclosed within FIELDS ENCLOSED BY characters, which is read as the string 'NULL'.

  • If FIELDS ESCAPED BY is empty, NULL is written as the word NULL.

  • With fixed-row format (which is used when FIELDS TERMINATED BY and FIELDS ENCLOSED BY are both empty), NULL is written as an empty string. This causes both NULL values and empty strings in the table to be indistinguishable when written to the file because both are written as empty strings. If you need to be able to tell the two apart when reading the file back in, you should not use fixed-row format.

So if you don't want to write the nullif functions for the nullable columns and but you have some control on the way the file is generated, you could adjust it to one of the options to differentiate between NULL and an empty string or the word 'NULL'.

So having your file like

1;\N;doe
2;\N;dane

or like

"1";NULL;"doe"
"2";NULL;"dane"

And using the right FIELDS and LINES options would do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM