PDA

View Full Version : trigger to create timestamp


catch
03-22-02, 19:38
<rookie question>

i have a table with a column;

joined timestamp not null,

so... how would i build a trigger that automatically generates a timestamp for column joined?

something like..

CREATE TRIGGER trig_person_tstamp
BEFORE INSERT ON person
FOR EACH ???????

what postgres function should i use to get the current timestamp in trig_person_tstamp?

</rookie question>

thanks.

uhexo
03-22-02, 20:15
Why don't you define field joined with DEFAULT CURRENT_TIMESTAMP?

I thing that's you want to do.

Saludos.


Originally posted by catch
<rookie question>

i have a table with a column;

joined timestamp not null,

so... how would i build a trigger that automatically generates a timestamp for column joined?

something like..

CREATE TRIGGER trig_person_tstamp
BEFORE INSERT ON person
FOR EACH ???????

what postgres function should i use to get the current timestamp in trig_person_tstamp?

</rookie question>

thanks.

catch
03-22-02, 20:26
thanks.
what's the syntax for that?

i found this link (http://www.postgresql.org/idocs/index.php?plpgsql-trigger.html) on triggers (using a timestamp example).
which method is more practical?

uhexo
03-22-02, 21:00
Originally posted by catch
[B]thanks.
what's the syntax for that?
create table xxx
(
joined timestamp not null
defaul current_timestamp
)

You can find more information about on "/B]create table (http://www.postgresql.org/idocs/index.php?sql-createtable.html)" syntax


i found this link (http://www.postgresql.org/idocs/index.php?plpgsql-trigger.html) on triggers (using a timestamp example).
which method is more practical?

the default one, but if it's you want to force the timestamp, i mean, if I wish that the users can't alter the joined's value, you have to use view with roles or table with triggers. And between them, I'd rather use the first methods.

saludos

catch
03-23-02, 00:22
thanks uhexo. the default method is what i was loooking for. it works.