patternMajor
Can't default date to CURRENT_TIMESTAMP in MySQL 5.5
Viewed 0 times
candatedefaultmysqlcurrent_timestamp
Problem
I am not able to set
Query is
While it is working fine on my local DB with
Current_timestamp as default value. My Mysql version is 5.5.47.Query is
ALTER TABLE `downloads` ADD `date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ;While it is working fine on my local DB with
mysql V5.6.56.Solution
From the MySQL 5.5 manual:
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.
Therefore, what you want to achieve will work in MySQL 5.5 if you add a
The changes in 5.6.x that allow the functionality are documented here, and I'll quote the relevant summary for completeness:
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
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.
Therefore, what you want to achieve will work in MySQL 5.5 if you add a
TIMESTAMP column instead of a DATE column.The changes in 5.6.x that allow the functionality are documented here, and I'll quote the relevant summary for completeness:
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Context
StackExchange Database Administrators Q#132951, answer score: 31
Revisions (0)
No revisions yet.