Upgrading to MySQL 5.7? Beware of the new STRICT mode

STRICT modeThis blog post discusses the ramifications of STRICT mode in MySQL 5.7.

In short

By default, MySQL 5.7 is much “stricter” than older versions of MySQL. That can make your application fail. To temporarily fix this, change the SQL_MODE to NO_ENGINE_SUBSTITUTION (same as in MySQL 5.6):

MySQL 5.7, dates and default values

The default SQL_MODE in MySQL 5.7 is:

That makes MySQL operate in “strict” mode for transactional tables.

“Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.”


That also brings up an interesting problem with the default value for the date/datetime column. Let’s say we have the following table in MySQL 5.7, and want to insert a row into it:

The event_date does not have a default value, and we are inserting a row without a value for event_date. That causes an error in MySQL 5.7. If we can’t use NULL, we will have to create a default value. In strict mod,e we can’t use “0000-00-00” either:

We have to use a real date:

Or, a most likely much better approach is to change the application logic to:

  • allow NULLs, or
  • always insert the real dates (i.e. use NOW() function), or
  • change the table field to timestamp and update it automatically if no value has been assigned
Further reading

Read the Morgan Tocker’s article on how to transition to MySQL 5.7, and check the full sql_mode documentation

Share this post

Comments (10)

  • Paul

    Are there any tools to help migrate a codebase to start using mysql’s STRICT mode?

    Ideally a permissive approach, but one which would log strict mode warnings to disk so they could be reviewed?

    December 31, 2016 at 12:12 am
  • aniketkushwaha

    The problem is due to microseconds added in default value in mysql new versions. See http://tekina.info/default-datetime-timestamp-issue-mysql-upgrading-5-6/ for solutions.

    November 9, 2017 at 2:36 am
  • Georgi Todorov

    Thank you Alexander!
    Helpful article.

    January 22, 2018 at 6:00 pm
  • Napoleon

    Alex you saved my day!!!

    April 10, 2018 at 3:02 pm
  • Joerg

    Many thanks!

    July 12, 2018 at 4:25 pm
  • Tongpoon

    I see and microseconds are important to us, regular humans. I fail to see how but then that’s just me, a normal human being. The boys at MySQL are obviously not from this galaxy and are superbeings. I’m trying to change the field to VARCHAR but guess what PHPMyAdmin (another bright light in the chandeleer) gives error on the other date field. So if you have 2 date fields you cannot even change one of them. Great guys. Keep up the good work!

    February 3, 2019 at 6:02 am
    • Alejandro de la Barra

      You need to eliminate NO_ZERO_DATE, and other options reletive to zeros.

      October 1, 2019 at 7:17 am
  • Jozsef

    Mysql 5.7 causes only problems. The strict mode sucks. It must be an option to be choosed.

    July 30, 2019 at 4:09 pm
    • Alejandro de la Barra


      October 1, 2019 at 7:18 am

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.