在MariaDB的时态表中恢复到特定时间的先前版本

时间:2019-08-19 09:38:51

标签: mariadb

我有一个MariaDB时间表,如下所示。

架构

MariaDB [teamdb]> DESCRIBE t2;
+-------+---------+------+-----+---------+---------------------------+
| Field | Type    | Null | Key | Default | Extra                     |
+-------+---------+------+-----+---------+---------------------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment            |
| x     | int(11) | YES  |     | NULL    |                           |
| y     | int(11) | YES  |     | NULL    | WITHOUT SYSTEM VERSIONING |
+-------+---------+------+-----+---------+---------------------------+

数据(当前)

MariaDB [teamdb]> SELECT * FROM t2;
+----+------+------+
| id | x    | y    |
+----+------+------+
|  1 |    1 |    1 |
|  2 |    2 |    2 |
|  3 |    3 |    4 |
|  4 |    5 |    4 |
+----+------+------+

数据(带有历史记录)

MariaDB [teamdb]> select *, ROW_START, ROW_END from t2 FOR SYSTEM_TIME ALL;
+----+------+------+----------------------------+----------------------------+
| id | x    | y    | ROW_START                  | ROW_END                    |
+----+------+------+----------------------------+----------------------------+
|  1 |    1 |    1 | 2019-08-15 06:41:18.684508 | 2038-01-19 03:14:07.999999 |
|  2 |    1 |    2 | 2019-08-15 06:41:18.684508 | 2019-08-15 06:42:11.661167 |
|  2 |    2 |    2 | 2019-08-15 06:42:11.661167 | 2038-01-19 03:14:07.999999 |
|  3 |    3 |    4 | 2019-08-15 06:41:18.684508 | 2038-01-19 03:14:07.999999 |
|  4 |    5 |    4 | 2019-08-15 06:41:18.684508 | 2038-01-19 03:14:07.999999 |
+----+------+------+----------------------------+----------------------------+

问题: 我想将记录(id=2)还原为2019-08-15 06:42:10.661167时间戳中的版本。这意味着,更新后,记录#2应该再次具有x = 1

我尝试使用ON DUPLICATE进行插入,如下查询所示,

INSERT INTO t2 (id,x,y) 
  SELECT t.id,t.x,t.y 
  FROM t2 t 
  FOR SYSTEM_TIME AS OF TIMESTAMP '2019-08-15 06:42:10.661167' 
  WHERE t.id=2 
ON DUPLICATE KEY UPDATE x=t.x, y=t.y;

这给了我说语法错误的错误。

ERROR 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version 
for the right syntax to use near 'SYSTEM_TIME AS OF TIMESTAMP '2019-08-15 06:42:10.661167' WHERE t.id=2 ON DUPLICA' at line 1

根据该错误,似乎MariaDB不能在选择插入中标识FOR子句。

通过查询恢复到其他版本的任何其他方式。

1 个答案:

答案 0 :(得分:1)

这与标识FOR子句无关,而与表别名的位置有关。

INSERT INTO t2 (id,x,y) 
  SELECT t.id,t.x,t.y 
  FROM t2
  FOR SYSTEM_TIME AS OF TIMESTAMP '2019-08-15 06:42:10.661167' t
  WHERE t.id=2 
ON DUPLICATE KEY UPDATE x=t.x, y=t.y;

注意将t重定位到系统时间子句的末尾。