22 September 2016

Error in mysql when setting default value for DATE or DATETIME


Problem :
I'm running MySql Server 5.7.15 and insert to my table with date value '0000-00-00'.
and got error in mysql when setting default value for DATE or DATETIME
[Err] 1292 - Incorrect date value: '0000-00-00' for column 'tanggal_lahir' at row 1 mysql

after any hour find the solution. this is the best solution for me :

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:
Strict mode affects whether the server permits '0000-00-00' as a valid date: If strict mode is not enabled, '0000-00-00' is permitted and inserts produce no warning. If strict mode is enabled, '0000-00-00' is not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, '0000-00-00' is permitted and inserts produce a warning.
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:00you have to disable STRICT_TRANS_TABLES mode in mysql config file or by command

By command

SET sql_mode = '';

if above is not working than go to /etc/mysql/my.cnf (as per ubuntu) and comment out STRICT_TRANS_TABLES


No comments: