inserting date

Discussion in 'MySQL' started by webboy, Apr 23, 2008.

  1. #1
    Hi all is it possible to insert the date automatically on a record creation or update in mysql , instead of passing the values on a insert
    would this some sort of server type thing eg stored procedure ?
     
    webboy, Apr 23, 2008 IP
  2. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I didn't get exectly. can you expalin it. yes you can insert data automatically.

    plz clear give exect info...
    Thank
    Sam
     
    ksamir2004, Apr 24, 2008 IP
  3. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    for example
    could a record have a default entry as date()

    every time the recored was updated the date would change

    or if a new row was created the date would be todays date by default
    instead of me passing the value from a form with a date string
     
    webboy, Apr 24, 2008 IP
  4. jitendraag

    jitendraag Notable Member

    Messages:
    3,982
    Likes Received:
    324
    Best Answers:
    1
    Trophy Points:
    270
    #4
    Well.. I have only see people using 'now()'. I don't know if you can use functions as defaults :>
     
    jitendraag, Apr 24, 2008 IP
  5. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so i would put that in the default box on the table with out the ;
     
    webboy, Apr 24, 2008 IP
  6. jitendraag

    jitendraag Notable Member

    Messages:
    3,982
    Likes Received:
    324
    Best Answers:
    1
    Trophy Points:
    270
    #6
    I am not sure you can use that :)

    I use it in following manner:

    
    insert into table (col1, updated_date) values ('value1', now());
    
    Code (markup):
     
    jitendraag, Apr 24, 2008 IP
  7. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    oh well here we go its a no no
    10.1.4. Data Type Default Values

    The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column. See Section 10.3.1.1, “TIMESTAMP Properties”.
     
    webboy, Apr 24, 2008 IP
  8. eopeter

    eopeter Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    in MSSQL, set the date column to NOT NULL and default value to getdate()

    NOt sure if their is a translation in MySQL for something like that.
     
    eopeter, Apr 25, 2008 IP
  9. sukosari

    sukosari Active Member

    Messages:
    63
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    if you use mysql5 you can place a code into trigger, in your case assign it at before insert and before update
     
    sukosari, Apr 27, 2008 IP