Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 01:36 AM
Prabu Subroto
Guest
 
Posts: n/a
Default the behaviour of timestamp on postgres.

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.

How is the behaviour of the timestamp on postgres? I
have define the datatype of the column with
"timestamp" but each time I inserted a new record into
the table than the timestamp column (with datatype
"timestamp") stays empty.

How can I make the postgres complete the value of the
timestamp field automatically?

Please let me know.

Thank you very much in advance.



__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #2  
Old November 23rd, 2005, 01:36 AM
Doug McNaught
Guest
 
Posts: n/a
Default Re: the behaviour of timestamp on postgres.

Prabu Subroto <prabu_subroto@yahoo.com> writes:
[color=blue]
> How can I make the postgres complete the value of the
> timestamp field automatically?[/color]

Add a DEFAULT NOW() clause to the column definition.

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
--T. J. Jackson, 1863

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

  #3  
Old November 23rd, 2005, 01:36 AM
Sebastian Böck
Guest
 
Posts: n/a
Default Re: the behaviour of timestamp on postgres.

Prabu Subroto wrote:[color=blue]
> 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.[/color]

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

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

  #4  
Old November 23rd, 2005, 01:36 AM
Prabu Subroto
Guest
 
Posts: n/a
Default Re: the behaviour of timestamp on postgres.

It's solved.

Thank you very much, Doug.

Thanks.
--- Doug McNaught <doug@mcnaught.org> wrote:
[color=blue]
> Prabu Subroto <prabu_subroto@yahoo.com> writes:
>[color=green]
> > How can I make the postgres complete the value of[/color]
> the[color=green]
> > timestamp field automatically?[/color]
>
> Add a DEFAULT NOW() clause to the column definition.
>
>
> -Doug
> --
> Let us cross over the river, and rest under the
> shade of the trees.
> --T. J. Jackson, 1863
>[/color]




__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

  #5  
Old November 23rd, 2005, 01:36 AM
Csaba Nagy
Guest
 
Posts: n/a
Default Re: the behaviour of timestamp on postgres.

Prabu,

Be aware that this will only work for inserts, and updating the row will
not automatically update the timestamp column. You will have to do it
yourself if you need that, either by using the right query (which is
actually a preferred solution).
Automatically you could do it through a rule - but I have never used the
Postgres rule system, so I don't know how to do that.
The update trigger posted in another reply will also work.

HTH,
Csaba.

On Wed, 2004-08-11 at 16:47, Prabu Subroto wrote:[color=blue]
> It's solved.
>
> Thank you very much, Doug.
>
> Thanks.
> --- Doug McNaught <doug@mcnaught.org> wrote:
>[color=green]
> > Prabu Subroto <prabu_subroto@yahoo.com> writes:
> >[color=darkred]
> > > How can I make the postgres complete the value of[/color]
> > the[color=darkred]
> > > timestamp field automatically?[/color]
> >
> > Add a DEFAULT NOW() clause to the column definition.
> >
> >
> > -Doug
> > --
> > Let us cross over the river, and rest under the
> > shade of the trees.
> > --T. J. Jackson, 1863
> >[/color]
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - 50x more storage than other providers!
> http://promotions.yahoo.com/new_mail
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster[/color]


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

  #6  
Old November 23rd, 2005, 01:36 AM
Stephan Szabo
Guest
 
Posts: n/a
Default Re: the behaviour of timestamp on postgres.


On Wed, 11 Aug 2004, Prabu Subroto wrote:
[color=blue]
> How is the behaviour of the timestamp on postgres? I[/color]

It's pretty much just a plain datatype.
[color=blue]
> have define the datatype of the column with
> "timestamp" but each time I inserted a new record into
> the table than the timestamp column (with datatype
> "timestamp") stays empty.
>
> How can I make the postgres complete the value of the
> timestamp field automatically?[/color]

If you want insert time setting, you can use a default
clause on the column.

If you want update time modification, you can write a
before trigger that updates the column.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles