pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Marlowe <smarlowe@qwest.net>
To: azah azah <izza76@gmail.com>
Cc: pgsql-php@postgresql.org
Subject: Re: default timestamp in postresql
Date: Thu, 29 Jul 2004 04:01:52 -0600
Message-ID: <1091095312.27159.25.camel@localhost.localdomain> (raw)
In-Reply-To: <235b1a280407290016621836b4@mail.gmail.com>
References: <235b1a280407290016621836b4@mail.gmail.com>

On Thu, 2004-07-29 at 01:16, azah azah wrote:
> Hi, 
> I need some help to solve a problem regarding transfer the db from
> mysql to postresql.
> Previously in mysql table field as below , will be add automatic in
> database when execute insert statement.
> 
>              = submittime timestamp NOT NULL
> 
> But in postresql the timestamp cannot insert automatic when execute
> insert statement.
> How can I solve this problem instead of change the php code?

If you need that field to always be updated whenever the row is changed,
you can use a trigger.

-- FUNCTION --

CREATE FUNCTION modtime () RETURNS opaque AS '
    BEGIN
        new.lm :=''now'';
        RETURN new;
    END;
' LANGUAGE 'plpgsql';

-- TABLE --

CREATE TABLE dtest (
    id int primary key,
    fluff text,
    lm timestamp without time zone
);


--TRIGGER --

CREATE TRIGGER dtest 
  BEFORE UPDATE or INSERT ON dtest FOR EACH ROW EXECUTE PROCEDURE
    modtime(lm);

-- SQL TESTS --

INSERT INTO dtest (id, fluff) VALUES (1,'this is a test');
INSERT INTO dtest (id, fluff) VALUES (2,'this is another test');
SELECT * FROM dtest;
  1 | this is a test       | 2003-04-02 10:33:12.577089
  2 | this is another test | 2003-04-02 10:33:18.591148
UPDATE dtest SET id=3 WHERE id=1;
  3 | this is a test | 2003-04-02 10:34:52.219963  [1]
UPDATE dtest SET fluff='now is the time' WHERE id=2;
SELECT * FROM dtest WHERE id=2;
  2 | now is the time | 2003-04-02 10:38:06.259443 [2]
UPDATE dtest SET lm='2003-04-02 08:30:00' WHERE id=3;
SELECT * FROM dtest WHERE id=3;
  3 | this is a test | 2003-04-02 10:36:15.45687 [3]

[1] The timestamp has changed for this record when we changed the id field.
[2] The timestamp also changes for the fluff field.
[3] We tried to set lm, but the trigger on that field in dtest intercepted the change and forced it 





view thread (5+ messages)

Message-ID: <1091095312.27159.25.camel@localhost.localdomain>
Permalink:  ../1091095312.27159.25.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1091095312.27159.25.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: smarlowe@qwest.net, izza76@gmail.com
  Subject: Re: default timestamp in postresql
  In-Reply-To: <1091095312.27159.25.camel@localhost.localdomain>
  References: <235b1a280407290016621836b4@mail.gmail.com> <1091095312.27159.25.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox