Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I disable MySQL strict mode?

I have been working on a site that had MySQL strict mode enabled. One person had a long user agent string that was logged in our log table and unfortunately the user agent string exceeded the limit for the column and thus caused a warning. The data was not inserted at all.

To avoid such troubles, should I disable the MySQL strict mode or should I come up with something on my own (I'm using PHP)?

like image 243
Tower Avatar asked Apr 03 '11 08:04

Tower


People also ask

Is it safe to disable MySQL strict mode?

When Strict Mode is disabled, the same query would have its invalid or missing values adjusted and would produce a simple warning. This may seem like the preferred result; however, with Strict Mode disabled, specific actions may cause unexpected results.

What is MySQL strict mode?

Strict SQL Mode. 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.

How do I disable strict mode in MySQL?

You can change the value of sql_mode to NO_ENGINE_SUBSTITUTION to completely disable strict mode, but you may want to look up each mode that is configured before disabling it. If sql_mode isn't set, you can add it under the [mysqld] heading, then save the file, and restart MySQL.


2 Answers

Validate the data before inserting it into your database. If a string is too big to fit in your table, either your column is too narrow, or the data is invalid. You need to decide whether you truncate it before storing it, do some error reporting, or both.

Do not turn off the safety features of your DBMS, that's the completely wrong thing to do.

like image 89
Mat Avatar answered Sep 18 '22 01:09

Mat


Would you rather have your data silently truncated (possibly leading to broken data) or would you at least like to know that you have a problem?

I'd recommend leaving strict mode enabled and bounds checking your data in your PHP. Your PHP application knows, or at least should know, what to do with a string that's too long. If you turn off strict mode and leave that decision to MySQL then MySQL will silently truncate your strings and you will end up with a database full of garbage.

Changing and fixing code is easy, fixing broken data is often impossible.

If you disable strict mode, you'll end up with strange problems like this:

VARCHAR(4) storing more characters than four

like image 39
mu is too short Avatar answered Sep 22 '22 01:09

mu is too short