Results 1 to 5 of 5

Thread: default NOW()

  1. #1
    Join Date
    Jun 2002

    Unanswered: default NOW()


    I have a table with a datetime field in it. I want the default value to be the current time, i.e. NOW(). But I don;t seem to be able to set that as the default:

    date datetime NOT NULL default NOW(),

    have also tried:

    date datetime NOT NULL default 'NOW()',

    But neither of them work. What am I doing wrong?

  2. #2
    Join Date
    May 2003
    If I understand your question well, you have a datetime field and you only want the time as a default value?? Why not do a time field and when you insert a record in your table you just put now() as the data instead of NULL or something else. Here's what i would do :

    create table tablename(
    fieldname TIME NOT NULL,

    and when you insert :

    insert into tablename values(now(), data, data, data...);

    If you are using VB you could just do a Sub and leave the now() always there since it never changes

    Hope this helps

  3. #3
    Join Date
    Jun 2002
    Did I say time? oh yeah, oops, sorry i meant date and time. Doesn't NOW() return 00.00.0000 00:00:00 with the current date and time?

    What I'm getting at is how do I set the defualt value to the current datetime? So as I don't have have insert blah blah NOW() blah blah. so as it just whacks it in by itself???

  4. #4
    Join Date
    Apr 2002
    Toronto, Canada
    mythix, you're not doing anything wrong, mysql doesn't support what you want to do:

    "A DEFAULT value has to be a constant,
    it cannot be a function or an expression."

    you could use a TIMESTAMP instead:

    "A TIMESTAMP column is useful for recording the
    date and time of an INSERT or UPDATE operation
    because it is automatically set to the date and time
    of the most recent operation if you don't give it a
    value yourself. You can also set it to the current date
    and time by assigning it a NULL value. "


  5. #5
    Join Date
    Jun 2002
    ahhh, that woiuld explain it, thanks. Will go and look into timestamp in more detail!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts