LightDB支持WITH UPDATE CURRENT_TIMESTAMP
语法。具体的为在建表时可以指定CREATE TABLE t1(a int) WITH UPDATE CURRENT_TIMESTAMP;
表示增加一个隐藏列ltaut
,为自动更新时间戳属性。与ON UPDATE CURRENT_TIMESTAMP
含义相同。
使用示例如下:
-- 建表
postgres@postgres=# create table t3(a int) with update current_timestamp;
CREATE TABLE
--隐藏列ltaut
postgres@postgres=# select * from t3;
a
---
(0 rows)
postgres@postgres=# insert into t3 values (1);
INSERT 0 1
postgres@postgres=# insert into t3 values (2);
INSERT 0 1
-- 显示指定查找ltaut隐藏列,自动更新时间戳
postgres@postgres=# select a, ltaut from t3;
a | ltaut
---+----------------------------
1 | 2022-12-15 08:51:36.13198
2 | 2022-12-15 08:51:39.077884
(2 rows)
postgres@postgres=# update t3 set a = 0 where a = 1;
UPDATE 1
postgres@postgres=# select a, ltaut from t3;
a | ltaut
---+----------------------------
2 | 2022-12-15 08:51:39.077884
0 | 2022-12-15 08:52:58.89909 -- 可以看到时间戳被自动更新
(2 rows)
需要注意的是,WITH UPDATE CURRENT_TIMESTAMP
与ON UPDATE CURRENT_TIMESTAMP
不能同时使用,很容易理解,同时使用对业务没有什么实际意义。
postgres@postgres=# create table t4(a int, b timestamp on update current_timestamp) with update current_timestamp;
ERROR: specifying multi ltaut column is not supported on a table, and only use on update current_timestamp or with update current_timestamp, both is not support.
另外一点: ON UPDATE CURRENT_TIMESTAMP
为MySQL语法,Oralce与PostgreSQL都不支持,LightDB对此功能做了兼容。详细见LightDB支持ON UPDATE CURRENT_TIMESTAMP。