postgresql数据创建时间&更新时间
2020-02-10 本文已影响0人
SodaCrush
建表语句
CREATE table if not exists "test_table" (
"name" TEXT,
"age" TEXT,
"create_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(0),
"update_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP(0)
);
- 实现:
- 创建带create_at和update_at字段的表,字段默认值为精度为0的CURRENT_TIMESTAMP,保证数据在插入时默认会同时记录当前时间为创建时间和更新时间
- 在插入语句的时候判断当数据重复时,更新update_at字段即可