简体   繁体   English

如何自动更新 PostgreSQL 中的时间戳

[英]How do I automatically update a timestamp in PostgreSQL

I want the code to be able to automatically update the time stamp when a new row is inserted as I can do in MySQL using CURRENT_TIMESTAMP.我希望代码能够在插入新行时自动更新时间戳,就像我在 MySQL 中使用 CURRENT_TIMESTAMP 所做的那样。

How will I be able to achieve this in PostgreSQL?我将如何在 PostgreSQL 中实现这一目标?

CREATE TABLE users (
    id serial not null,
    firstname varchar(100),
    middlename varchar(100),
    lastname varchar(100),
    email varchar(200),
    timestamp timestamp
)

To populate the column during insert, use a DEFAULT value:要在插入期间填充列,请使用DEFAULT值:

CREATE TABLE users (
  id serial not null,
  firstname varchar(100),
  middlename varchar(100),
  lastname varchar(100),
  email varchar(200),
  timestamp timestamp default current_timestamp
)

Note that the value for that column can explicitly be overwritten by supplying a value in the INSERT statement.请注意,可以通过在INSERT语句中提供值来显式覆盖该列的值。 If you want to prevent that you do need a trigger .如果你想防止你确实需要一个trigger

You also need a trigger if you need to update that column whenever the row is updated (as mentioned by EJ Brennan )如果您需要在更新行时更新该列,则还需要一个触发器(如EJ Brennan 所述

Note that using reserved words for column names is usually not a good idea.请注意,对列名使用保留字通常不是一个好主意。 You should find a different name than timestamp您应该找到与timestamp不同的名称

You'll need to write an insert trigger, and possible an update trigger if you want it to change when the record is changed.您需要编写一个插入触发器,如果​​您希望它在记录更改时更改,则可能需要编写一个更新触发器。 This article explains it quite nicely:这篇文章很好地解释了它:

http://www.revsys.com/blog/2006/aug/04/automatically-updating-a-timestamp-column-in-postgresql/ http://www.revsys.com/blog/2006/aug/04/automatically-updating-a-timestamp-column-in-postgresql/

 CREATE OR REPLACE FUNCTION update_modified_column() RETURNS TRIGGER AS $$ BEGIN NEW.modified = now(); RETURN NEW; END; $$ language 'plpgsql';

Apply the trigger like this:像这样应用触发器:

 CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON customer FOR EACH ROW EXECUTE PROCEDURE update_modified_column();

Updating timestamp, only if the values changed更新时间戳,仅当值更改时

Based on EJ's link and add a if statement from this link ( https://stackoverflow.com/a/3084254/1526023 )基于 EJ 的链接并从此链接添加 if 语句( https://stackoverflow.com/a/3084254/1526023

CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
   IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN
      NEW.modified = now(); 
      RETURN NEW;
   ELSE
      RETURN OLD;
   END IF;
END;
$$ language 'plpgsql';

使用 'now()' 作为默认值会自动生成时间戳。

To automatically update the timestamp field in PostgresSQL whenever a new row is inserted, you can set the current_timestamp as its default value:要在插入新行时自动更新 PostgresSQL 中的时间戳字段,您可以将current_timestamp设置为其default值:

CREATE TABLE users (
    id serial not null,
    firstname varchar(100),
    middlename varchar(100),
    lastname varchar(100),
    email varchar(200),
    timestamp timestamp default current_timestamp
)

In addition to this, you might want to prevent anyone from updating this field in the future, and this can be done by creating an update trigger and applying it:除此之外,您可能希望防止将来任何人更新此字段,这可以通过创建更新触发器并应用它来完成:

CREATE OR REPLACE FUNCTION stop_change_on_timestamp()
  RETURNS trigger AS
$BODY$
BEGIN
  -- always reset the timestamp to the old value ("actual creation time")
  NEW.timestamp := OLD.timestamp;
  RETURN NEW;
END;
$BODY$
CREATE TRIGGER prevent_timestamp_changes
  BEFORE UPDATE
  ON users
  FOR EACH ROW
  EXECUTE PROCEDURE stop_change_on_timestamp();

For update and Liquibase YAML:对于更新和 Liquibase YAML:

databaseChangeLog:
  - changeSet:
      id: CREATE FUNCTION set_now_to_timestamp
      author: Konstantin Chvilyov
      changes:
        - sql:
            sql: CREATE OR REPLACE FUNCTION set_now_to_timestamp() RETURNS TRIGGER LANGUAGE plpgsql AS 'BEGIN NEW.timestamp = NOW(); RETURN NEW; END;'

  - changeSet:
      id: CREATE TRIGGER update_users_set_now_to_timestamp
      author: Konstantin Chvilyov
      changes:
        - sql:
            sql: CREATE TRIGGER update_users_set_now_to_timestamp BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE set_now_to_timestamp();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM