pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedbest way to auto-update a field when row is updated
3+ messages / 3 participants
[nested] [flat]
* best way to auto-update a field when row is updated
@ 2005-03-10 20:09 Henry Ortega <juandelacruz@gmail.com>
0 siblings, 2 replies; 3+ messages in thread
From: Henry Ortega @ 2005-03-10 20:09 UTC (permalink / raw)
To: pgsql-sql
I have the following table
FIELD_A | FIELD_B | TSTAMP
--------------------------------------------------------------------------------------------------------
x y 2005-03-10
14:56:47.456431
TSTAMP = not null default now()
What's the best way to always auto-update TSTAMP to it's
default value whenever the row gets updated?
(e.g. update TABLENAME set FIELD_A='zzz' where FIELD_A='x';
should automatically set TSTAMP to now)
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: best way to auto-update a field when row is updated
@ 2005-03-10 20:21 Larry Rosenman <ler@lerctr.org>
parent: Henry Ortega <juandelacruz@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Larry Rosenman @ 2005-03-10 20:21 UTC (permalink / raw)
To: Henry Ortega <juandelacruz@gmail.com>; +Cc: pgsql-sql
On Thursday 10 March 2005 02:09 pm, Henry Ortega wrote:
> I have the following table
>
> FIELD_A | FIELD_B | TSTAMP
> ---------------------------------------------------------------------------
>----------------------------- x y
> 2005-03-10 14:56:47.456431
>
> TSTAMP = not null default now()
>
> What's the best way to always auto-update TSTAMP to it's
> default value whenever the row gets updated?
> (e.g. update TABLENAME set FIELD_A='zzz' where FIELD_A='x';
> should automatically set TSTAMP to now)
>
trigger on update/insert.
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
--
Larry Rosenman http://www.lerctr.org/~ler
Phone: +1 214-351-4152 E-Mail: ler@lerctr.org
US Mail: 3535 Gaspar Drive, Dallas, TX 75220-3611
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: best way to auto-update a field when row is updated
@ 2005-03-10 20:35 Scott Marlowe <smarlowe@g2switchworks.com>
parent: Henry Ortega <juandelacruz@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Scott Marlowe @ 2005-03-10 20:35 UTC (permalink / raw)
To: Henry Ortega <juandelacruz@gmail.com>; +Cc: pgsql-sql
On Thu, 2005-03-10 at 14:09, Henry Ortega wrote:
> I have the following table
>
> FIELD_A | FIELD_B | TSTAMP
> --------------------------------------------------------------------------------------------------------
> x y 2005-03-10
> 14:56:47.456431
>
> TSTAMP = not null default now()
>
> What's the best way to always auto-update TSTAMP to it's
> default value whenever the row gets updated?
> (e.g. update TABLENAME set FIELD_A='zzz' where FIELD_A='x';
> should automatically set TSTAMP to now)
Here's a simple trigger to do that for ya.
-- 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
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2005-03-10 20:35 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-03-10 20:09 best way to auto-update a field when row is updated Henry Ortega <juandelacruz@gmail.com>
2005-03-10 20:21 ` Larry Rosenman <ler@lerctr.org>
2005-03-10 20:35 ` Scott Marlowe <smarlowe@g2switchworks.com>
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