Thread: Create a function that updates the record with and timestamps
I would like to have postgres update the last_modified column with the current_date on an update of the record.
I am not sure if there is a very simple way of doing this?
Or, do I need to create a function and a trigger to call the row and update with new data and set the last_modified to current_date?
Here is the table.
CREATE TABLE price.price_table (
PRICE_DATE DATE,
ID VARCHAR(13),
OPENING NUMERIC(18,6),
CLOSING NUMERIC(18,6),
HIGHEST NUMERIC(18,6),
LOWEST NUMERIC(18,6),
VOLUME BIGINT,
LAST_MODIFIED TIMESTAMP(6) WITHOUT TIME ZONE DEFAULT current_date,
CONSTRAINT PK_PRICE PRIMARY KEY (PRICE_DATE,ID));
Any help would be appreciated.
Cheers,
Chris
Stay in touch. Get Messenger on your phone now.
I am not sure if there is a very simple way of doing this?
Or, do I need to create a function and a trigger to call the row and update with new data and set the last_modified to current_date?
Here is the table.
CREATE TABLE price.price_table (
PRICE_DATE DATE,
ID VARCHAR(13),
OPENING NUMERIC(18,6),
CLOSING NUMERIC(18,6),
HIGHEST NUMERIC(18,6),
LOWEST NUMERIC(18,6),
VOLUME BIGINT,
LAST_MODIFIED TIMESTAMP(6) WITHOUT TIME ZONE DEFAULT current_date,
CONSTRAINT PK_PRICE PRIMARY KEY (PRICE_DATE,ID));
Any help would be appreciated.
Cheers,
Chris
Stay in touch. Get Messenger on your phone now.
In response to Chris Barnes : > I would like to have postgres update the last_modified column with the > current_date on an update of the record. > > I am not sure if there is a very simple way of doing this? > > Or, do I need to create a function and a trigger to call the row and update > with new data and set the last_modified to current_date? Yes, that's the way, a TRIGGER on UPDATE for each row. I think, the doc contains an example. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
I see examples for updating tables using a function, but I would like to pull the row requested and modify the last_modified column with current_date and push the modified data back into the same row.
I did see an example of how to use old and new at this at this link, but it is vague.
http://www.faqs.org/docs/ppbook/x20655.htm#TRIGGERFUNCTIONVARIABLES
From: compuguruchrisbarnes@hotmail.com
To: pgsql-general@postgresql.org
Subject: [GENERAL] Create a function that updates the record with and timestamps
Date: Mon, 22 Mar 2010 12:58:49 -0400
I am not sure if there is a very simple way of doing this?
Or, do I need to create a function and a trigger to call the row and update with new data and set the last_modified to current_date?
Here is the table.
CREATE TABLE price.price_table (
PRICE_DATE DATE,
ID VARCHAR(13),
OPENING NUMERIC(18,6),
CLOSING NUMERIC(18,6),
HIGHEST NUMERIC(18,6),
LOWEST NUMERIC(18,6),
VOLUME BIGINT,
LAST_MODIFIED TIMESTAMP(6) WITHOUT TIME ZONE DEFAULT current_date,
CONSTRAINT PK_PRICE PRIMARY KEY (PRICE_DATE,ID));
Any help would be appreciated.
Cheers,
Chris
> Date: Mon, 22 Mar 2010 18:14:00 +0100
> From: andreas.kretschmer@schollglas.com
> To: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Create a function that updates the record with and timestamps
>
> In response to Chris Barnes :
> > I would like to have postgres update the last_modified column with the
> > current_date on an update of the record.
> >
> > I am not sure if there is a very simple way of doing this?
> >
> > Or, do I need to create a function and a trigger to call the row and update
> > with new data and set the last_modified to current_date?
>
> Yes, that's the way, a TRIGGER on UPDATE for each row. I think, the doc
> contains an example.
>
>
> Andreas
> --
> Andreas Kretschmer
> Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
> GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
Don't miss a beat Get Messenger on your phone
On Monday 22 March 2010 10:55:36 am Chris Barnes wrote: > I see examples for updating tables using a function, but I would like to > pull the row requested and modify the last_modified column with > current_date and push the modified data back into the same row. > > > > I did see an example of how to use old and new at this at this link, but it > is vague. > > > > http://www.faqs.org/docs/ppbook/x20655.htm#TRIGGERFUNCTIONVARIABLES > Something like this: CREATE OR REPLACE FUNCTION public.ts_update() RETURNS trigger AS $Body$ BEGIN NEW.ts_update:=timeofday(); RETURN NEW; END; $Body$ LANGUAGE 'plpgsql'; -- Adrian Klaver adrian.klaver@gmail.com
In response to Richard Sickler : > > I am not sure if there is a very simple way of doing this? > > > > Or, do I need to create a function and a trigger to call the row and > update > > with new data and set the last_modified to current_date? > > Yes, that's the way, a TRIGGER on UPDATE for each row. I think, the doc > contains an example. > > > Andreas > > > From a novice: I use last_updated_at timestamp without time zone NOT NULL > DEFAULT now() This works only for INSERT, but not for UPDATE. test=# create table richard (id int, last_updated_at timestamp without time zone NOT NULL DEFAULT NOW()); CREATE TABLE test=# insert into richard (id) values (1); INSERT 0 1 test=# select * from richard ; id | last_updated_at ----+---------------------------- 1 | 2010-03-23 06:54:28.656668 (1 row) test=# select now(); now ------------------------------- 2010-03-23 06:54:42.443224+01 (1 row) test=# UPDATE richard set id=2 where id=1; UPDATE 1 test=# select * from richard ; id | last_updated_at ----+---------------------------- 2 | 2010-03-23 06:54:28.656668 (1 row) As you can see, the last_updated_at isn't up-to-date ;-) Regards, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99