Invalid Default value in MySQL when using datetime with CURRENT_TIMESTAMP - mysql

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.

Related

Invalid default value for current timestamp field in live server

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.

mysql 5.6.3 issue with CURRENT_TIMESTAMP default value on TIMESTAMP field

I've been through many of the questions that are similar to mine, but all seem to relate to trying to use a default CURRENT_TIMESTAMP with a DATETIME field.
I am having a problem using a CURRENT_TIMESTAMP default with a TIMESTAMP field which I understand is supposed to be supported.
I am using MySQL 5.6.3 community edition. Here is a partial extract of my table creation code:
CREATE TABLE IF NOT EXISTS `my_table` (
`master_id` INT(10) NOT NULL AUTO_INCREMENT ,
`template_id` INT(10) NULL,
`def_id` INT(10) NOT NULL,
'override` TINYINT(1) NULL,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ...
This errors out:
Invalid default value for 'last_update'
Any ideas why?
Found the answer.
Someone (not me) had set the following on the MySQL my.cnf:
explicit_default_for_timestamp = 1
Removing this setting resolved the issue.

Create table with default values giving error

Using MySql Workbench 6.3 Build version 6.3.6.
I am trying to create a table with Default constraint but its giving me error.
Here is the script
Create Table `Migration_Log2` (
`Id` Int NOT NULL AUTO_INCREMENT,
`FilePath` varchar(1000) NOT NULL,
`FileName` varchar(100) NOT NULL,
`IsSent` bool NOT NULL DEFAULT '0',
`CreatedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ModifiedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`SendAttemptMade` int NOT NULL DEFAULT '0',
`Message` Text DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `migration_log_Id_UNIQUE` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Error Message
Error Code: 1067. Invalid default value for 'CreatedDate' 0.000 sec
This may be due to some strict constraint on the data type check on database server.
I would suggest to change type of field CreatedDate from datetime to timestamp.
I had faced similar issue in a VPS for my website.
Your CURRENT_TIMESTAMP might have been appending the microseconds in the output.
Try to use: CURRENT_TIMESTAMP(0) as the default value.
SELECT CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(0), CURRENT_TIMESTAMP(1), CURRENT_TIMESTAMP(2);
The microseconds mattered, possibly. See the differences.

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,

MySQL CURRENT_TIMESTAMP Error Parsing DDL for microseconds

This is the MySQL table that I want, but focus on datum_en_tijd:
CREATE TABLE `navigatie` (
`navigatie_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`navigatie_id`),
`huidige_vraaggroep` varchar(255) NOT NULL,
`vorige_vraaggroep` varchar(255) DEFAULT NULL,
`richting` varchar(255) NOT NULL,
`datum_en_tijd` timestamp(3) NOT NULL,
`schadegeval_id` bigint(20) UNSIGNED DEFAULT NULL,
`claim_id` bigint(20) UNSIGNED DEFAULT NULL,
`gebruiker_id` bigint(20) NOT NULL,
`soort_gebruiker` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As you can see TIMESTAMP is with (3) for milliseconds
Whenever I try to Alter Table... in MySQL Workbench I get this error:
When I do View DDL, I get a new tab with this query:
delimiter $$
CREATE TABLE `navigatie` (
`navigatie_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`huidige_vraaggroep` varchar(255) NOT NULL,
`vorige_vraaggroep` varchar(255) DEFAULT NULL,
`richting` varchar(255) NOT NULL,
`datum_en_tijd` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`schadegeval_id` bigint(20) unsigned DEFAULT NULL,
`claim_id` bigint(20) unsigned DEFAULT NULL,
`gebruiker_id` bigint(20) NOT NULL,
`soort_gebruiker` varchar(255) NOT NULL,
PRIMARY KEY (`navigatie_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Note the change on
`datum_en_tijd` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
Is this a bug or what?
Also note the SYNTAX ERROR on line 8 MySQL WorkBench gives us:
I'm running MySQL 5.6.16
Incompatible change: In very old versions of MySQL (prior to 4.1), the
TIMESTAMP data type supported a display width, which was silently
ignored beginning with MySQL 4.1. This is deprecated in MySQL 5.1, and
removed altogether in MySQL 5.5. These changes in behavior can lead to
two problem scenarios when trying to use TIMESTAMP(N) columns with a
MySQL 5.5 or later server:
When importing a dump file (for example, one created using mysqldump) created in a MySQL 5.0 or earlier server into a server from
a newer release series, a CREATE TABLE or ALTER TABLE statement
containing TIMESTAMP(N) causes the import to fail with a syntax error.
To fix this problem, edit the dump file in a text editor to replace any instances of TIMESTAMP(N) with TIMESTAMP prior to
importing the file. Be sure to use a plain text editor for this, and
not a word processor; otherwise, the result is almost certain to be
unusable for importing into the MySQL server.
http://dev.mysql.com/doc/refman/5.1/en/timestamp-initialization.html
So you cant have
`datum_en_tijd` timestamp(3)
instead you need to use
`datum_en_tijd` timestamp
or
`datum_en_tijd` datetime(3)
Yes timestamp does not require this any more, i usually just use it with either DEAFAULT or ON UPDATE CURRENT_TIMESTAMP, you may find either useful.
Some variants to pick from:
created_at timestamp(3) NOT NULL DEFAULT '1970-01-01 12:34:56'
mysql_row_created_at timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
created_at timestamp(3) NULL DEFAULT NULL
Each has its own pluses and minuses.
Don't (ever) use datetime, as that has no related timezone, it is mostly just liike a (packed) string.
The MySQL timestamp type is physically stored in UTC (it is a UNIX-epoch delta). It is rendered in the active timezone of the session.