103

I got a stupid problem with SQL that I can't fix.

ALTER TABLE  `news` 
 ADD  `dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AUTO_INCREMENT ,
 ADD PRIMARY KEY (  `dateAdded` )

Error:

(#1067)Invalid default value for 'dateAdded'

Can somebody help me?

1
  • 11
    This might be a stupid question, but why do you AUTO_INCREMENT DATETIME ?
    – jave.web
    Jun 10, 2016 at 10:50

8 Answers 8

149

CURRENT_TIMESTAMP is only acceptable on TIMESTAMP fields. DATETIME fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.

relevant docs: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.

5
  • 71
    It looks like as of mysql 5.6.5, you can use CURRENT_TIMESTAMP with DATETIME fields. See dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html Jul 25, 2013 at 0:39
  • 2
    Thanks Frank, found this problem when trying to import an exported db from the latest xampp to an earlier one.
    – The HCD
    Aug 8, 2014 at 23:03
  • The HCD , Frank ... found same issue exporting from MariaDb 10.x to MariaDB 5.5 ... thanks
    – Aukhan
    Jul 1, 2015 at 19:57
  • 1
    This error happens in mariadb 5.5 also. Upgrade to mariadb 10 and its solved. Thanks Frank Sep 17, 2015 at 0:48
  • MySQL query browser seems to have a bug setting the default value for TIMESTAMPS. Use this query instead ALTER TABLE tablename MODIFY COLUMN timestamp_colname TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; Apr 29, 2022 at 9:29
54

CURRENT_TIMESTAMP is version specific and is now allowed for DATETIME columns as of version 5.6.

See MySQL docs.

2
  • 10
    are you sure ? I get the above error in version 5.7.x Jul 12, 2017 at 15:23
  • MySQL query browser seems to have a bug setting the default value for TIMESTAMPS. Use this query instead ALTER TABLE tablename MODIFY COLUMN timestamp_colname TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; Apr 29, 2022 at 9:31
22

Also do note when specifying DATETIME as DATETIME(3) or like on MySQL 5.7.x, you also have to add the same value for CURRENT_TIMESTAMP(3). If not it will keep throwing 'Invalid default value'.

2
  • 2
    This solved the problem for me on 5.7.x. My datetime fields were defined as datetime(6) and using CURRENT_TIMESTAMP(6) solved the problem
    – Brad
    May 10, 2018 at 2:37
  • 1
    This is highly worthy noting. May 23, 2018 at 17:54
2

I had the same issue, following fix solved my problem.

  • Select Type as 'TIMESTAMP'

  • DON'T ENTER ANYTHING IN LENGTH/VALUES FIELD. KEEP IT BLANK

  • Select CURRENT_TIMESTAMP as Default value.

I am using MySQL ver 5.5.56

0

I have mysql version 5.6.27 on my LEMP and CURRENT_TIMESTAMP as default value works fine.

0

mysql version 5.5 set datetime default value as CURRENT_TIMESTAMP will be report error you can update to version 5.6 , it set datetime default value as CURRENT_TIMESTAMP

0

Change the type from datetime to timestamp and it will work! I had the same issue for mysql 5.5.56-MariaDB - MariaDB Server Hope it can help... sorry if depricated

0

I solved mine by changing DATE to DATETIME

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.