Adding a default now() to new entries in mySQL?

Discussion in 'MySQL' started by amaze, Aug 30, 2007.

  1. #1
    Hi,

    I would like to add something like "now()" as a default for new entries? Is this possible? So basically when a new record is entered in will time\date stamp.

    I tried adding "now()" but just returns null..

    THanks :)
     
    amaze, Aug 30, 2007 IP
  2. timothy247

    timothy247 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    What about something like this, I believe it's used when creating the table?

    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

    Also what happens when you try: SELECT Now()
    Do you get the current date/time?

    I am not sure if it will work or not, but there is some info on the following site that might help:

    http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

    Good luck!

    Tim
     
    timothy247, Aug 30, 2007 IP
  3. bluegrass special

    bluegrass special Peon

    Messages:
    790
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Are you adding Now() as the value in your query or seting it as a default for the field? From what I have read (but not tested as I use MSSQL) you cannot use a nonconstant expression as a default value. The exception to this is to use timestamp. (http://dev.mysql.com/doc/refman/5.0/en/timestamp.html)

    Be careful about using timestamp because it has some properties that may make it work in a manner you do not want it to if you do not set it up correctly (such as updating each time the record is updated).

    You could also try adding Now() to the query or pulling the datetime from your coding language.

    Looks like Tim got to it as I was writting.
     
    bluegrass special, Aug 30, 2007 IP
  4. amaze

    amaze Active Member

    Messages:
    594
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I had the data type as "datetime". When I changed to "timestamp" and changed the default value to "CURRENT_TIMESTAMP" it worked - thanks!
     
    amaze, Aug 31, 2007 IP
  5. _Eugene_

    _Eugene_ Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    you should remember that with works only with the first timestamp in the table
     
    _Eugene_, Sep 14, 2007 IP