Update timestamp when row is updated in PostgreSQL

后端 未结 1 985
感动是毒
感动是毒 2020-12-04 06:18

In MySQL, we can execute this where it updates the column changetimestamp every time the row is changed:

create table ab (
  id int, 
  changeti         


        
相关标签:
1条回答
  • 2020-12-04 06:58

    Create a function that updates the changetimestamp column of a table like so:

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

    Create a trigger on the table that calls the update_changetimestamp_column() function whenever an update occurs like so:

        CREATE TRIGGER update_ab_changetimestamp BEFORE UPDATE
        ON ab FOR EACH ROW EXECUTE PROCEDURE 
        update_changetimestamp_column();
    
    0 讨论(0)
提交回复
热议问题