Postgresql Current timestamp on Update

后端 未结 3 1374
感动是毒
感动是毒 2020-12-13 07:30

What is the postgres equivalent of the below mysql code

CREATE TABLE t1 (
  created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  modified TIMESTAMP DEFAULT CURRENT         


        
3条回答
  •  甜味超标
    2020-12-13 08:01

    Just make sure all tables have the same columnname:

    CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER 
    LANGUAGE plpgsql
    AS
    $$
    BEGIN
        NEW.modified = CURRENT_TIMESTAMP;
        RETURN NEW;
    END;
    $$;
    
    CREATE TRIGGER t_name
      BEFORE UPDATE
      ON tablename
      FOR EACH ROW
      EXECUTE PROCEDURE upd_timestamp();
    

提交回复
热议问题