Skip site navigation (1) Skip section navigation (2)

Re: timestamp data type?

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: psql novice <psql_novice(at)operamail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: timestamp data type?
Date: 2003-05-22 15:03:54
Message-ID: 1053615834.25709.2174.camel@linda.lfix.co.uk (view raw or flat)
Thread:
Lists: pgsql-novice
On Thu, 2003-05-22 at 07:51, psql novice wrote:
> Is there a timestamp data type that will automatically update the date
> and time when a record gets inserted/updated. I know MySQL has this
> feature, i was wondering if postgresql had a equivalent data type
> 
> MySQL Doc
> "The TIMESTAMP column type provides a type that you can use to
> automatically mark INSERT or UPDATE operations with the current date
> and time."

There is no type that does this.

The way to do it on INSERT is:

CREATE TABLE xxx (
   ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   ...);

This can be overridden if the insert provides a value for the field.

If you need it not to be overrideable or to be changed on update as
well, you need a trigger to fire BEFORE UPDATE OR INSERT

-- 
Oliver Elphick                                Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "We are troubled on every side, yet not distressed; we 
      are perplexed, but not in despair; persecuted, but not
      forsaken; cast down, but not destroyed; Always bearing
      about in the body the dying of the Lord Jesus, that 
      the life also of Jesus might be made manifest in our 
      body."        II Corinthians 4:8-10 


In response to

Responses

pgsql-novice by date

Next:From: Nicholas AllenDate: 2003-05-22 15:41:43
Subject: How to send an email when data is inserted into a table
Previous:From: Molenda, Mark PDate: 2003-05-22 14:33:30
Subject: Problem on Linux

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group