MySQL CURRENT_TIMESTAMP Error Parsing DDL for microseconds - mysql

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.

Related

Laravel 7 - weird timestamp migration behavior

I'm creating a table via a migration file.
I have multiple timestamp columns which one of is not supposed to be nullable.
If i set that one column first in order the migration goes through just fine, if I place any nullable timestamp column before it, the migration fails with an exception:
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'credit_date' (SQL: create table `feedback_survey_records` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `feedback_survey_id` bigint unsigned not null, `reward` smallint not null, `credit_status` varchar(45) null, `answer_date` timestamp null, `open_date` timestamp null, `credit_date` timestamp not null, `decline_date` timestamp null, `decline_reason` varchar(255) null, `response_id` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = innodb)
Successful migration
$table->timestamp('credit_date');
$table->timestamp('answer_date')->nullable();
$table->timestamp('open_date')->nullable();
$table->timestamp('decline_date')->nullable();
Failing migration
$table->timestamp('answer_date')->nullable();
$table->timestamp('open_date')->nullable();
$table->timestamp('credit_date');
$table->timestamp('decline_date')->nullable();
I looked at the database and noticed that laravel automatically added a default value and extra instructions on the successful migration
Database structure Image
Can someone please shed some light on why this might be happening?

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.

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 Community 5.7 - Invalid default value (datetime field type)

I've just installed MySQL Community Server 5.7 and I am trying to create the following table:
CREATE TABLE IF NOT EXISTS `atcommandlog` (
`atcommand_id` mediumint(9) unsigned NOT NULL auto_increment,
`atcommand_date` datetime NOT NULL default '0000-00-00 00:00:00',
`account_id` int(11) unsigned NOT NULL default '0',
`char_id` int(11) unsigned NOT NULL default '0',
`char_name` varchar(25) NOT NULL default '',
`map` varchar(11) NOT NULL default '',
`command` varchar(255) NOT NULL default '',
PRIMARY KEY (`atcommand_id`),
INDEX (`account_id`),
INDEX (`char_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1;
This gives me the following error:
#1067 - Invalid default value for 'atcommand_date'
This same query works fine on MySQL 5.6. Has the default datetime value been changed in MySQL 5.7?
This looks like more of a SQL mode issue than the char set. Strict
mode (and more specifically NO_ZERO_DATE which is part of strict mode)
usually sets off that error if the table was created with an all zero
default date before turning on strict.
What we use (for similar modified date columns), is the '1970-01-01
00:00:01'.
and on a slightly related note, we use timestamp for these columns
(and for created time too). Takes half the storage space, and is
faster access.
https://dba.stackexchange.com/questions/6171/invalid-default-value-for-datetime-when-changing-to-utf8-general-ci

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,