MySQL – Fixing current_timestamp Default Column Value Issue on Windows and Linux

datetimeMySQLtimestampUbuntuwindows

This MySql query runs fine in Windows:

CREATE TABLE `posts`.`table` (
  `idTable` INT NOT NULL AUTO_INCREMENT,
  `Column1` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`idTable`));

In Ubuntu, it is giving the following error:

ERROR 1067: Invalid default value for 'Column1'

How do I give CURRENT_TIMESTAMP as the default value for 'Column1' in Ubuntu?

The version of MySql is this:

mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3

Best Answer

The reason is the different versions installed in the two environments.

The option for DEFAULT CURRENT_TIMESTAMP for datetime columns was added in 5.6 version (specifically in 5.6.5). See the doumentation about the DATE, DATETIME, and TIMESTAMP Types:

The TIMESTAMP and (as of MySQL 5.6.5) DATETIME data types offer automatic initialization and updating to the current date and time. For more information, see Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME.

Most probably your Linux installation has an older version while the Windows has a newer. The solution would be to upgrade to the newer version (or have it installed besides the old).

General info about upgrading from 5.5 to 5.6 here: Upgrading from MySQL 5.5 to 5.6 and specific about upgrading in Ubuntu 14.04 LTS.

Don't forget to backup your data before you upgrade and run mysql_upgrade immediately after.