How do I turn on strict mode in mysql

  • 2020-06-01 11:09:36
  • OfStack

Many integrated PHP environment (PHPnow WAMP Appserv, etc.) own MySQL have little open MySQL strict mode, what is a MySQL strict mode, is simply MySQL itself to strict calibration data (format, length, type, etc.), such as an integer field we write a string type of data, in the strict mode MySQL not an error, if the same defines char or varchar types of fields, when writing or update data more than the length of the definition is not an error.

I don't think this is any good for programming, although we try to do data validation in the code. MySQL opens the strict mode for 1 set program is the code 1 kind of test for us, if we don't have any open development environment strict mode in the development process and no encounters an error, so in the online or code transplantation will likely not compatible, so in the development process to do the best to open MySQL strict mode.

How do I turn it on?
1. It can be opened by executing the SQL statement, but only for the current connection. Here is the SQL statement:

set sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";

2. By modifying the configuration file of MySQL and looking for sql-mode in the configuration file, modify this line as:

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

If sql-mode = cannot be found, then add it under [mysqld]. The second method is recommended, which can be used once and for all.

Related articles: