Hello,
I'm on DB2 UDB for IBM iSeries V5R4. I have a table which contains TIMESTAMP field and want to declare the field so, that it should be automatically updated by UPDATE and INSERT operations.
I created the table as follows
When I try to INSERT some rows using
then the TIMESTAMP field will be automatically correctly inserted.
But when I try to UPDATE a row with
then the TIMESTAMP field tmstp will not be automatically updated. To achieve this I need to create a trigger
Isn't it possible to declare the TIMESTAMP field tmstp so it be automatic updated without need to add a trigger ?
Or is there a better way to do what I have done?
I'm on DB2 UDB for IBM iSeries V5R4. I have a table which contains TIMESTAMP field and want to declare the field so, that it should be automatically updated by UPDATE and INSERT operations.
I created the table as follows
Code:
[COLOR=#804040][b]CREATE[/b][/color] [COLOR=#6a5acd]TABLE[/color] ex_tmstp (
[COLOR=#0000ff]-- automatically incremented key field[/color]
id INT [COLOR=#804040][b]NOT[/b][/color] [COLOR=#6a5acd]NULL[/color] GENERATED ALWAYS [COLOR=#6a5acd]AS[/color] IDENTITY (
[COLOR=#6a5acd]START[/color] [COLOR=#6a5acd]WITH[/color] [COLOR=#ff00ff]1[/color] [COLOR=#6a5acd]INCREMENT[/color] [COLOR=#6a5acd]BY[/color] [COLOR=#ff00ff]1[/color]
NO MINVALUE NO MAXVALUE
CYCLE NO [COLOR=#6a5acd]ORDER[/color]
CACHE [COLOR=#ff00ff]20[/color] ) PRIMARY KEY,
[COLOR=#0000ff]--[/color]
data [COLOR=#2e8b57][b]VARCHAR[/b][/color]([COLOR=#ff00ff]50[/color]),
[COLOR=#0000ff]-- Warning: [/color]
[COLOR=#0000ff]-- This creates TIMESTAMP field which will be automatically generated [/color]
[COLOR=#0000ff]-- only by INSERT but not by UPDATE operation[/color]
tmstp TIMESTAMP [COLOR=#6a5acd]WITH[/color] [COLOR=#6a5acd]DEFAULT[/color] [COLOR=#6a5acd]CURRENT[/color] TIMESTAMP
)
;
When I try to INSERT some rows using
Code:
[COLOR=#804040][b]INSERT[/b][/color] [COLOR=#6a5acd]INTO[/color] ex_tmstp (data)
[COLOR=#6a5acd]VALUES[/color] ([COLOR=#ff00ff]'The time of creation is:'[/color])
;
But when I try to UPDATE a row with
Code:
[COLOR=#804040][b]UPDATE[/b][/color] ex_tmstp
[COLOR=#804040][b]SET[/b][/color] data=[COLOR=#ff00ff]'The current timestamp of last update is: '[/color]
[COLOR=#6a5acd]WHERE[/color] id=[COLOR=#ff00ff]1[/color]
;
Code:
[COLOR=#0000ff]-- for UPDATE this trigger is necessary[/color]
[COLOR=#804040][b]CREATE[/b][/color] [COLOR=#6a5acd]TRIGGER[/color] update_cur_tmstp
BEFORE [COLOR=#804040][b]UPDATE[/b][/color]
[COLOR=#6a5acd]ON[/color] ex_tmstp
REFERENCING NEW [COLOR=#6a5acd]AS[/color] n
[COLOR=#6a5acd]FOR[/color] EACH [COLOR=#6a5acd]ROW[/color]
[COLOR=#804040][b]SET[/b][/color] tmstp = [COLOR=#6a5acd]CURRENT[/color] TIMESTAMP
;
Isn't it possible to declare the TIMESTAMP field tmstp so it be automatic updated without need to add a trigger ?
Or is there a better way to do what I have done?