I'm running MySql Server 5.7.11 and this sentence:
updated datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
is not working. Giving the error:
ERROR 1067 (42000): Invalid default value for 'updated'
But the following:
updated datetime NOT NULL DEFAULT '1000-01-01 00:00:00'
just works.
The same case for DATE.
As a sidenote, it is mentioned in the mysql docs:
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
even if they also say:
Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').
Having also into account the second quote from mysql documentation, could anyone let me know why it is giving that error?
It works for 5.7.8:
You can create a SQLFiddle to recreate your issue.
http://sqlfiddle.com/
If it works for MySQL 5.6 and 5.7.8, but fails on 5.7.11. Then it probably be a regression bug for 5.7.11.
First select current session
sql_mode
:Then you will get something like that default value:
and then set
sql_mode
without'NO_ZERO_DATE'
:If you have grants, you can do it also for
GLOBAL
:To solve the problem with MySQL Workbench (After applying the solution on the server side) :
Remove SQL_MODE to TRADITIONAL in the preferences panel.
The error is because of the sql mode which can be strict mode as per latest MYSQL 5.7 documentation
MySQL Documentation 5.7 says:
To Check MYSQL mode
SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session
Disabling STRICT_TRANS_TABLES mode
However to allow the format
0000-00-00 00:00:00
you have to disable STRICT_TRANS_TABLES mode in mysql config file or by commandBy command
SET sql_mode = '';
or
SET GLOBAL sql_mode = '';
Using the keyword
GLOBAL
requires super previliges and it affects the operations all clients connect from that time onif above is not working than go to
/etc/mysql/my.cnf
(as per ubuntu) and comment outSTRICT_TRANS_TABLES
Also, if you want to permanently set the sql mode at server startup then include
SET sql_mode=''
inmy.cnf
on Linux or MacOS. For windows this has to be done inmy.ini
file.Note
However strict mode is not enabled by default in MYSQL 5.6. Hence it does not produce the error as per MYSQL 6 documentation which says
UPDATE
Regarding the bug matter as said by @Dylan-Su:
I don't think this is the bug it the way MYSQL is evolved over the time due to which some things are changed based on further improvement of the product.
However I have another related bug report regarding the
NOW()
functionDatetime field does not accept default NOW()
Another Useful note [see Automatic Initialization and Updating for TIMESTAMP and DATETIME]
Update Regarding NO_ZERO_DATE
As of MySQL as of 5.7.4 this mode is deprecated. For previous version you must comment out the respective line in the config file. Refer MySQL 5.7 documentation on NO_ZERO_DATE
Option combinations for
mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64)
.Doesn't throw:
STRICT_TRANS_TABLES
+NO_ZERO_DATE
Throws:
STRICT_TRANS_TABLES
+NO_ZERO_IN_DATE
My settings in
/etc/mysql/my.cnf
on Ubuntu:I had this error with WAMP 3.0.6 with MySql 5.7.14.
Solution:
change line 70 (if your ini file is untouched) in
c:\wamp\bin\mysql\mysql5.7.14\my.ini
file fromto
and restart all services.
This will disable strict mode. As per the documentation, “strict mode” means a mode with either or both
STRICT_TRANS_TABLES
orSTRICT_ALL_TABLES
enabled. The documentation says: