TIMESTAMP : created & modified fields ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reddog

    #1

    TIMESTAMP : created & modified fields ?

    Hi all,

    I have a number of tables that I want 'timestamped' with both a
    modified and created 'timestamp' field.

    Ideally both fields should 'work if ignored' in insert and updates,
    thus the following is fine for a modified timestamp field tsm:

    tsm timestamp default current_timesta mp on upate current_timesta mp

    But is there a way to create a field that in effect is 'write once'
    and contains the creation timestamp ? In a sense I want some thing
    like (psudo sql create line):

    tsc timestamp default current_timesta mp 'on_update_do_n othing'

    I think a second timestamp with 'default current_time' would help but
    mysql does not allow this - (I have no idea why !?)

    There are quite a number of tables and I want to avoid 'table
    specific' sql code like the following (psudo sql):

    insert record ...
    get last update auto increment key
    select modifed timestamp value from key record
    update key record create timestamp field with timestamp

    Any ideas (for a 'write once' creation timestamp)?

  • Bill Karwin

    #2
    Re: TIMESTAMP : created & modified fields ?

    "Reddog" <_no_spam_d-hilton@blueyond er.co.uk> wrote in message
    news:i1mnr1d70q p8ta4cd0i5cbvcm 3ros380hj@4ax.c om...[color=blue]
    > I have a number of tables that I want 'timestamped' with both a
    > modified and created 'timestamp' field.
    >
    > Any ideas (for a 'write once' creation timestamp)?[/color]

    One method to do this is with triggers (requires MySQL 5.0). Define a
    trigger on INSERT that forces the creation timestamp field to be the current
    time, overriding any other value that the client may have attempted to
    insert.

    CREATE TRIGGER timestamp_ins_c heck BEFORE INSERT ON myTable
    FOR EACH ROW
    BEGIN
    SET NEW.created = CURRENT_TIMESTA MP();
    SET NEW.updated = CURRENT_TIMESTA MP();
    END;

    Also define a trigger on UPDATE that forces the creation timestamp field to
    be unaltered from its pre-update value.

    CREATE TRIGGER timestamp_upd_c heck BEFORE UPDATE ON myTable
    FOR EACH ROW
    BEGIN
    SET NEW.created = OLD.created;
    SET NEW.updated = CURRENT_TIMESTA MP();
    END;

    If you don't use MySQL 5.0, you may have to resort to application-level code
    to enforce your rules.

    Regards,
    Bill K.


    Comment

    • Reddog

      #3
      Re: TIMESTAMP : created &amp; modified fields ?

      On Wed, 4 Jan 2006 11:40:52 -0800, "Bill Karwin" <bill@karwin.co m>
      wrote:
      [color=blue]
      >
      >One method to do this is with triggers (requires MySQL 5.0). Define a
      >trigger on INSERT that forces the creation timestamp field to be the current
      >time, overriding any other value that the client may have attempted to
      >insert.
      >....[/color]
      .....>[color=blue]
      >If you don't use MySQL 5.0, you may have to resort to application-level code
      >to enforce your rules.
      >
      >Regards,
      >Bill K.
      >[/color]

      Thanks for the info Bill,
      I using MySql 4.1.7 NT so I have do not triggers yet.
      Reddog.

      Comment

      Working...