+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Simple SQL Problem

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Simple SQL Problem

    Why is SQL saying now() is an invalid default value for a datetime field?
    Code:
    CREATE TABLE Catagories (
    ID INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(100) NOT NULL UNIQUE,
    Created DATETIME DEFAULT NOW(),
    Modified DATETIME DEFAULT NOW(),
    Notes VARCHAR(255)
    )
    
    ~Error: Invalid default value for 'Created'
    Looks fine to me?
    Last edited by Twinkie; 06-12-2009 at 01:48 AM.

  2. #2
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: Simple SQL Problem

    im not sure but i dont think you can store functions inside of a sql database and have it run the function, however sql has a built in way to put timestamps in, try this:

    Code:
    CREATE TABLE IF NOT EXISTS `test` (
      `id` int(11) NOT NULL auto_increment,
      `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
      `created` timestamp NOT NULL default '0000-00-00 00:00:00',
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    you will need to add a way to put the timestamp for the created, such as the php function date()
    also, you can only have one field time be timestamped otherwise you will get this error
    Code:
    Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

  3. #3
    mrfish is offline x10Hosting Member mrfish is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Re: Simple SQL Problem

    Yah, I just got back into MySQL and I'm pretty sure your cannot store NOW() as a property. Correct me if I'm wrong though. The only way you should use the now() function is in your php script when the 'whatever' if created and/or modified. Then you can add/rewrite the current value to now().

  4. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Simple SQL Problem

    Ok, thanks for your help

  5. #5
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Simple SQL Problem

    The DEFAULT clause specifies 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”.
    Source: http://dev.mysql.com/doc/refman/5.0/...ate-table.html

  6. #6
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: Simple SQL Problem

    MySQL over PostgreSQL? tsk tsk
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  7. #7
    batman1's Avatar
    batman1 is offline x10Hosting Member batman1 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Jamaica
    Posts
    92

    Re: Simple SQL Problem

    default value cannot be a function. put some time. 0000-00-00 00:00:00

  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Simple SQL Problem

    I usually allow null values in my timestamp fields since some programs and scripts have trouble processing 0000-00-00 00:00:00.

    Or you could setup the database field to hold an integer and directly store the number of seconds since the epoch, as a lot of php scripts are doing.
    Last edited by xav0989; 06-13-2009 at 07:45 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  9. #9
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Simple SQL Problem

    I understand the reasoning for using a timestamp rather than a datetime field, but for the sake of maintainability for a small project, I chose the field that would be easier to read. If this was a bigger project I would be concerned of the speed of turning datetime into a timespamp value for processing.

    I am making a little Ebay for me and my mom to sell some of our old junk.

  10. #10
    cmstheme is offline x10Hosting Member cmstheme is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    1

    Cms theme

    :drool: I guess everyone is welcome here lol. :cool:

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. simple actionscript problem
    By stalkio in forum Programming Help
    Replies: 2
    Last Post: 08-19-2008, 06:18 PM
  2. Simple Image
    By jtaah in forum Free Hosting
    Replies: 0
    Last Post: 08-16-2008, 06:17 AM
  3. Replies: 2
    Last Post: 12-14-2007, 09:21 AM
  4. problem on creating sql database
    By joseph0829 in forum Free Hosting
    Replies: 1
    Last Post: 12-05-2007, 01:07 PM
  5. i am having my sql problem
    By xtone in forum Free Hosting
    Replies: 2
    Last Post: 10-27-2006, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers