仅在MariaDB中插入时的时间戳字段,与“LOAD DATA LOCAL INFILE'数据加载

时间:2018-03-22 15:23:25

标签: mysql file-upload mariadb sql-timestamp

我想在MySQL表中设置一个时间戳字段,只在插入时设置,而不是在更新时设置。该表创建如下:

CREATE TABLE `test_insert_timestamp` (
  `key` integer NOT NULL,
  `value` integer NOT NULL,
  `insert_timestamp` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`key`)
);

数据加载了这句话(需要使用LOAD DATA LOCAL INFILE):

LOAD DATA LOCAL INFILE 
   "inserts_test_timestamp1.txt"
REPLACE
INTO TABLE 
    `test_insert_timestamp`
FIELDS TERMINATED BY ';'

注意:我需要使用REPLACE选项,不管为什么。 inserts_test_timestamp**1**.txt的内容为:

1;2
3;4

我有另一个文件inserts_test_timestamp**2**.txt

3;4
5;6

我不会:

  1. 如果我加载文件inserts_test_timestamp**1**.txt,则设置字段insert_timestamp(代码正常)

  2. 如果我加载inserts_test_timestamp**2**.txt,则记录(3; 4)不会更改已设置的字段insert_timestamp,但记录(5; 6)设置新insert_timestamp

  3. 但没办法。两个记录都使用相同的值加上时间戳,而不是使用旧时间戳留下(3; 4)。

    我正在MariaDB 5.5.52数据库上发布CentOS 7.3个数据库。认为MariaDB版本很重要,但我无法改变它。

2 个答案:

答案 0 :(得分:1)

您可以分两个步骤划分流程:

MariaDB [_]> DROP TABLE IF EXISTS
    ->   `temp_test_insert_timestamp`,
    ->   `test_insert_timestamp`;
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> CREATE TABLE IF NOT EXISTS `test_insert_timestamp` (
    ->   `key` integer NOT NULL,
    ->   `value` integer NOT NULL,
    ->   `insert_timestamp` timestamp DEFAULT CURRENT_TIMESTAMP,
    ->   PRIMARY KEY (`key`)
    -> );
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> CREATE TABLE IF NOT EXISTS `temp_test_insert_timestamp` (
    ->   `key` integer NOT NULL,
    ->   `value` integer NOT NULL,
    ->   `insert_timestamp` timestamp DEFAULT CURRENT_TIMESTAMP,
    ->   PRIMARY KEY (`key`)
    -> );
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> LOAD DATA LOCAL INFILE '/path/to/file/inserts_test_timestamp1.txt'
    -> INTO TABLE `test_insert_timestamp`
    -> FIELDS TERMINATED BY ';'
    -> (`key`, `value`);
Query OK, 2 rows affected (0.00 sec)                 
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0

MariaDB [_]> SELECT
    ->   `key`,
    ->   `value`,
    ->   `insert_timestamp`
    -> FROM
    ->   `test_insert_timestamp`;
+-----+-------+---------------------+
| key | value | insert_timestamp    |
+-----+-------+---------------------+
|   1 |     2 | 2018-03-20 00:49:38 |
|   3 |     4 | 2018-03-20 00:49:38 |
+-----+-------+---------------------+
2 rows in set (0.00 sec)

MariaDB [_]> DO SLEEP(5);
Query OK, 0 rows affected (5.00 sec)

MariaDB [_]> LOAD DATA LOCAL INFILE '/path/to/file/inserts_test_timestamp2.txt'
    -> INTO TABLE `temp_test_insert_timestamp`
    -> FIELDS TERMINATED BY ';'
    -> (`key`, `value`);
Query OK, 2 rows affected (0.00 sec)                 
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0

MariaDB [_]> SELECT
    ->   `key`,
    ->   `value`,
    ->   `insert_timestamp`
    -> FROM
    ->   `temp_test_insert_timestamp`;
+-----+-------+---------------------+
| key | value | insert_timestamp    |
+-----+-------+---------------------+
|   3 |     4 | 2018-03-20 00:49:43 |
|   5 |     6 | 2018-03-20 00:49:43 |
+-----+-------+---------------------+
2 rows in set (0.00 sec)

MariaDB [_]> REPLACE INTO `test_insert_timestamp`
    -> SELECT
    ->   `ttit`.`key`,
    ->   `ttit`.`value`,
    ->   `tit`.`insert_timestamp`
    -> FROM
    ->   `temp_test_insert_timestamp` `ttit`
    ->   LEFT JOIN `test_insert_timestamp` `tit`
    ->   ON `ttit`.`key` = `tit`.`key`;
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [_]> SELECT
    ->   `key`,
    ->   `value`,
    ->   `insert_timestamp`
    -> FROM
    ->   `test_insert_timestamp`;
+-----+-------+---------------------+
| key | value | insert_timestamp    |
+-----+-------+---------------------+
|   1 |     2 | 2018-03-20 00:49:38 |
|   3 |     4 | 2018-03-20 00:49:38 |
|   5 |     6 | 2018-03-20 00:49:43 |
+-----+-------+---------------------+
3 rows in set (0.00 sec)

MariaDB [_]> TRUNCATE TABLE `temp_test_insert_timestamp`;
Query OK, 0 rows affected (0.00 sec)

答案 1 :(得分:0)

我在这篇文章中实现了解决方案:MySQL LOAD DATA INFILE with ON DUPLICATE KEY UPDATE

此解决方案不仅允许我获取insert_timestamp,还允许我获取update_timestamp的字段:

# --- Create temporary table ---
CREATE TEMPORARY TABLE temporary_table LIKE test_insert_timestamp;

# --- Delete index to speed up
DROP INDEX `PRIMARY` ON temporary_table;
DROP INDEX `STAMP_INDEX` ON temporary_table;

# --- Load data in temporary table
LOAD DATA LOCAL INFILE "./inserts_test_timestamp1.txt"
INTO TABLE temporary_table
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
IGNORE 1 LINES
SET
        insert_timestamp = CURRENT_TIMESTAMP(),
        update_timestamp = NULL
;

# --- Insert data in temporary table ---
INSERT INTO test_insert_timestamp
SELECT * FROM temporary_table
ON DUPLICATE KEY UPDATE
   update_timestamp = CURRENT_TIMESTAMP();

# --- Drop temporary
DROP TEMPORARY TABLE temporary_table;

感谢您的帮助!