Invalid default value for current timestamp field in live server - mysql

Below query works fine in my localhost but When i try to execute this query in the live server it gives sql error
CREATE TABLE IF NOT EXISTS `tbl_claims` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`claimed_profile_id` int(11) NOT NULL,
`claimed_by_profile_id` int(11) NOT NULL COMMENT 'this will be only doctor''s profile id',
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`phone_no` varchar(100) NOT NULL,
`status` enum('approved','disapproved','pending') NOT NULL DEFAULT 'pending',
`added_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
It gives below error:
Invalid default value for 'added_on'
I do not know why the above query works fine in loalhost and why it does not work in live server.
The server where the query does not work has PHP Version 5.6.33
Any suggestions would be greatly appreciated.

I suspect it is because you are trying to use CURRENT_TIMESTAMP as the default value for a datetime field.
For MySQL >= 5.6:
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 TIMESTAMP and DATETIME columns.
For MySQL <= 5.5:
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.
As you can see, for versions 5.5 and below, you can only use CURRENT_TIMESTAMP as the default value for a TIMESTAMP field.

Related

Invalid Default value in MySQL when using datetime with CURRENT_TIMESTAMP

I'm trying to restore a database someone gave me for reference. I'm using MAMP as of the moment with PHP 5.6.10. I tried importing the SQL file given to me, however, it gives an error at this block:
CREATE TABLE `itg_civil_status` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`civilStatusName` varchar(16) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
PRIMARY KEY (`id`)
)
the line in question is:
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
And the error I get is
Invalid default value for 'date_created'
I already looked it up and there are answers saying that datetime should either be null or has a constant value by default. However, I've also read that as of PHP 5.6, CURRENT_TIMESTAMP() is allowed for the datetime field. I also tried CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), andNOW()`, but all to no avail.
Kindly note that I can't simply just go and change my friend from datetime to timestamp because for some reason, MySQL didn't allow me to do so.
I also tried using PHP 7 just to test but to no avail as well.

MySql table have two or more columns have default date vaule CURRENT_TIMESTAMP,any Solutions?

if i have a table with two columns create_time and update_time,the data type is timestamp,then have default value CURRENT_TIMESTAMP,the sql code of created table is:
CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);
but it prompt error:1293,there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or UPDATE clause.
I am not sure what you're trying to accomplish having both the STARTDATE and ENDDATE populated with the CURRENT_TIMESTAMP. However to fix your code try changing your data types to DATETIME like this:
**CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);**
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html
This limitation in Mysql:
Previously, at most one TIMESTAMP column per table could be
automatically initialized or updated to the current date and time.
This restriction has been lifted. Any TIMESTAMP column definition can
have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used
with DATETIME column definitions. For more information, see Automatic
Initialization and Updating for TIMESTAMP and DATETIME.
Check This for more detail
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html

Invalid default value for 'timestamp'

i am getting error in my database. i am encountering invalid default value for timestamp.
here's my database:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`color` varchar(10) DEFAULT '#00bcd4',
`icon` varchar(100) NOT NULL DEFAULT 'https://mymonas.com/forum/category_icon/ic_question.png'
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
I was having the same problem, I changed type from "datetime" to "timestamp" and It worked. I have mysql 5.5.52.
Mysql_error
I have the same issue in sql_mode.
Make query:
show variables like 'sql_mode' ;
You need to remove the "NO_ZERO_IN_DATE,NO_ZERO_DATE" from sql_mode.
SET sql_mode = '';
Use CURRENT_TIMESTAMP() instead CURRENT_TIMESTAMP
i.e.
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
Now() works as well
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."
The changes in MYSQL 5.6.x that allow the functionality are documented here:
"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."
So, this means you are using an older version of mysql, either you can use datetime data type of upgrade your mysql version
Answered by #max Sherbakov worked but I think its risky,
if you execute SET sql_mode = ''; query.
Because if you or other users SET any different variables in sql_mode
like NO_ENGINE_SUBSTITUTION check other SQL MODES
by changing sql_mode values in my.ini file
OR
using SET sql_mode = 'YOUR_VARIABLE_LIST'; query
it worked for you current situation
but create problem in other projects.
To view current sql mode use following query
show variables like 'sql_mode' ;

Difference of datetime type in mysql 5.5.46 and 5.6.28

Have table in mysql 5.5.46 and same table in 5.6.28. I backup database on mysql 5.5.46 and then restore it on 5.6.28. So table have same structure and same data on both servers.
CREATE TABLE `test` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Path` varchar(250) NOT NULL,
`Link` varchar(250) NOT NULL,
`Company` varchar(250) NOT NULL,
`Width` int(11) NOT NULL,
`Height` int(11) NOT NULL,
`View_Count` int(11) NOT NULL DEFAULT '0',
`create_date` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
Have query like this :
insert into test(path,link,company,width,height,view_count)
values('test','test','test',10,10,10)
On mysql 5.5.46 this query execute successfully and create_date value is '0000-00-00 00:00:00'
On mysql 5.6.28 have an error Field 'Create_Date' doesn't have a default value and it's logically because create_date is not null so it does not have any default value. But how it works on 5.5.46 ?
Based on the information you gave us, it appears that you were running MySQL 5.5.46 in non strict mode, but running MySQL 5.6.28 in strict mode. From the MySQL documentation:
TIMESTAMP columns declared as NOT NULL and without an explicit DEFAULT clause are treated as having no default value. For inserted rows that specify no explicit value for such a column, the result depends on the SQL mode. If strict SQL mode is enabled, an error occurs. If strict SQL mode is not enabled, the column is assigned the implicit default of '0000-00-00 00:00:00' and a warning occurs. This is similar to how MySQL treats other temporal types such as DATETIME.
Your create_date column is of type DATETIME, and it appears that you were trying to do the INSERT in the 5.6.28 database in strict mode, and hence getting an error.

MySQL: CURRENT_TIMESTAMP for date field works locally but not on server

I have a date column whose default value is CURRENT_TIMESTAMP. It works fine locally, but when I did an export and tried to create the database on my hosting I get this:
`Invalid default value for 'created'`
...from this code:
CREATE TABLE IF NOT EXISTS `bookings` (
`id` varchar(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`cloned` int(11) DEFAULT NULL,
`event_id` varchar(11) NOT NULL,
`amount_due` smallint(6) NOT NULL,
`vat` tinyint(2) NOT NULL,
`discount` tinyint(4) DEFAULT NULL,
`date_paid` date DEFAULT NULL,
`notes` mediumtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The answer to this question says that, apparently, CURRENT_TIMESTAMP is an acceptable default only for datetime, not date, columns. But like I say, it works locally, just not remotely.
Could it be to do with the difference in MySQL versions?
Local MySQL: protocol v10 / server v5.6.16
Remote MySQL: protocol v10 / server v5.5.35
Am I getting away with this locally because of the higher server version?
This won't work for MySQL 5.5; before mysql 5.6.5 CURRENT_TIMESTAMP works only for TIMESTAMP.
http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
It's working for you locally, because you are using MySQL 5.6.16 there.
In the MySQL manual a Timestamp datatype is recommended for use with "DEFAULT CURRENT_TIMESTAMP".
Also consider this (taken from the MySQL manual):
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
try using
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,