mysqljdbcload-data-infile

mysql LOAD DATA LOCAL INFILE with default timestamp column value


I've a table which has a column defined like this:

| Field       | Type          | Null | Key | Default           | Extra          |
+-------------+---------------+------+-----+-------------------+----------------+
| created_at  | timestamp     | NO   |     | CURRENT_TIMESTAMP |                |

and I insert data using this command:

LOAD DATA LOCAL INFILE '<path_to_the_file>' INTO TABLE my_table FIELDS TERMINATED BY '|' (<some_columns>,created_at,<more_columns>)

from a file where each line is like this:

822202|NETGEAR|||||1448ce8f-efc5-7b07-6982-1ff991bf967e||||||||1|||

Note: created_at is the 5th column, so the file has no value for it, therefore I expect CURRENT_TIMESTAMP

instead, this is what I see:

mysql> select distinct created_at from my_table;
+---------------------+
| created_at          |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

the code running the command is, supposedly, using a standard JDBC, it's a jdbc.clj Clojure library.

Note 2: if I run INSERT INTO ... query manually, it (obviously) works as expected.

Anyone can tell me what am I missing?


Solution

  • Table:

    DROP TABLE IF EXISTS `test_table`;
    
    CREATE TABLE `test_table` (
        `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        `column0` VARCHAR(6) DEFAULT NULL,
        `column1` VARCHAR(20) DEFAULT NULL,
        `column2` VARCHAR(20) DEFAULT NULL,
        `column3` VARCHAR(20) DEFAULT NULL,
        `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `column5` VARCHAR(20) DEFAULT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    

    File:

    /path/to/test_file.txt

    822202|NETGEAR||||1
    822203|NETGEAR1|||2000-01-01 00:00:00|2
    822204|NETGEAR2|||NULL|3
    822205|NETGEAR3|||\N|4
    

    MySQL Command-Line:

    mysql> LOAD DATA INFILE '/path/to/test_file.txt'
           INTO TABLE `test_table`
           FIELDS TERMINATED BY '|'
           (`column0`, `column1`, `column2`, `column3`, @`created_at`, `column5`)
           SET `created_at` = IF(CHAR_LENGTH(TRIM(@`created_at`)) = 0
                              OR
                              TRIM(@`created_at`) = 'NULL', NULL, @`created_at`);