mysql LOAD DATA LOCAL INFILE,带有默认的timestamp列值

时间:2015-10-05 00:56:45

标签: mysql jdbc load-data-infile

我的表格的列定义如下:

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

我使用这个命令插入数据:

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

来自文件,其中每一行都是这样的:

822202 |网件||||| 1448ce8f-efc5-7b07-6982-1ff991bf967e |||||||| ||| 1

注意: created_at 是第5列,因此该文件没有值,因此我期待CURRENT_TIMESTAMP

相反,这就是我所看到的:

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

运行该命令的代码据说是使用标准的JDBC,它是一个jdbc.clj Clojure库。

注意2:如果我手动运行INSERT INTO ...查询,它(显然)按预期工作。

任何人都可以告诉我我错过了什么?

1 个答案:

答案 0 :(得分:4)

表:

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;

文件:

/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命令行:

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`);