Re: the behaviour of timestamp on postgres. - Mailing list pgsql-general

From Sebastian Böck
Subject Re: the behaviour of timestamp on postgres.
Date
Msg-id 411A317F.9080904@freenet.de
Whole thread Raw
In response to the behaviour of timestamp on postgres.  (Prabu Subroto <prabu_subroto@yahoo.com>)
List pgsql-general
Prabu Subroto wrote:
> Dear my friends...
>
> I created some tables with field timestamp (datatype
> also timestamp). I mean, I want to have the data when
> each record inserted or modified in the tables.
>
> on MysQL, I just need to define the column (field)
> with datatype "timestamp" and that's all. each time
> new record inserted than the timestamp value will be
> inserted automaticall.  also for the data modification,
> each time the data in the record modified than the
> value of timestamp column will be modified
> automatically.

You can use triggers for that.

Try something like:

CREATE FUNCTION set_timestamp() RETURNS TRIGGER AS '
   BEGIN
     NEW.timestamp := now();
     RETURN NEW;
   END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER insert_timestamp BEFORE INSERT ON table
   FOR EACH ROW EXECUTE PROCEDURE set_timestamp();
CREATE TRIGGER update_timestamp BEFORE UPDATE ON table
   FOR EACH ROW EXECUTE PROCEDURE set_timestamp();

HTH

Sebastian

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: POSIX RE starting with a (
Next
From: Prabu Subroto
Date:
Subject: Re: the behaviour of timestamp on postgres.