The MySQL MySQL sql_mode pattern Explanation and set the pattern Explanation and setting sql_mode

MySQL's sql_mode pattern Explanation and settings

 

MySQL reasonable set of sql_mode

    sql_mode is easily overlooked variable, the default value is a null value, in this arrangement is to allow some illegal operations, such as allowing some invalid data is inserted. This value must be set to strict mode in a production environment, the development, test environment database must be set so that the development and testing phases can find the problem.

    

sql model used to solve the following types of problems

  (1) By setting sql mode, you can perform different stringency data check, data preparation of effective protection.

  (2) by setting the sql model is loose mode to ensure the most sql sql-standard syntax, such applications when migrating between different databases, you do not need to make huge changes to business sql.

  (3) prior to data migration between different databases, so that the data can be provided by the SQL Mode MySQL more easily migrate to the target database.

    

sql_mode common values ​​are as follows: 

    ONLY_FULL_GROUP_BY:

    For GROUP BY aggregate operations, if a column in the SELECT, does not appear in a GROUP BY, then the SQL is not legitimate, because the column is not in the GROUP BY clause

 

    NO_AUTO_VALUE_ON_ZERO:

    The value is inserted from the impact of the growth column. In the default setting, the insert 0 or NULL on behalf of a self-generated growth value. If you want to insert a value of 0, and that column is self-growth, this option comes in handy.

 

    STRICT_TRANS_TABLES:

    In this mode, if a value can not be inserted into a transaction table, interrupting the current operation, no limitation on the non-transactional table

    NO_ZERO_IN_DATE:

    In strict mode, the date and month of zero is not allowed

 

    NO_ZERO_DATE:

    This value is set, mysql database does not allow zero-insertion date, insert zero dates will throw an error instead of a warning.

 

    ERROR_FOR_DIVISION_BY_ZERO:

    INSERT or UPDATE in the process, if the data is zero, an error instead of a warning is generated. If the pattern is not given, then return NULL when data MySQL zero

 

    NO_AUTO_CREATE_USER:

    GRANT prohibit users create a blank password

 

    NO_ENGINE_SUBSTITUTION:

    If you need a storage engine is disabled or not compiled, then throw an error. When this value is not set, the default storage engine replacement, and throws an exception

 

    PIPES_AS_CONCAT:

    The "||" operator not regarded as connection strings or operator, and that the Oracle database is the same, and also blending functions similar string Concat

 

    ANSI_QUOTES:

    ANSI_QUOTES then enabled, can not be used to refer to a string double quotation marks, because it is interpreted as an identifier

 

    ORACLE的sql_mode设置等同:PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, NO_KEY_OPTIONS, NO_TABLE_OPTIONS, NO_FIELD_OPTIONS, NO_AUTO_CREATE_USER.

     

    If you are using mysql, in order to retain the habit of people to use oracle, mysql can sql_mode set as follows:

     

    Adding follows my.cnf

    [mysqld]

    sql_mode='ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,

    ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,PIPES_AS_CONCAT,ANSI_QUOTES'

      

      

Note: MySQL5.6 and MySQL5.7 sql_mode default mode parameter is not the same, mode 5.6 is NO_ENGINE_SUBSTITUTION, in fact, represent a null value is equivalent to no mode setting, it can be understood as easing mode. Mode 5.7 is STRICT_TRANS_TABLES, which is strict mode.

 

    If the setting is relaxed mode, when we insert data, even gave a wrong data may also be accepted, and not being given, for example: When I create a table that has a field name, a char (10), when inserted data if I, wherein the name of this field has a length of data field 10 exceeds the setting of the type name, e.g. '1234567890abc', 10 exceeds the set field length , it does not complain, and to deposit the first 10 characters, which means that data is stored for you '1234567890', but 'abc' would not, but we know that we give this data is wrong, because more than the length of the field, but did not error, and mysql discretion and accepted, and this is the effect of easing mode, in fact, in the development, testing, production environment, we should adopt a strict mode, this error, it should be an error fishes, so MySQL5.7 version will sql_mode default value to the strict mode, and we even use MySQL5.6, it should be self Strict mode, but you remember, MySQL databases, etc. These are all trying to operate on the data are taking down their own, including the parity data, in fact, a lot of time, we should project the program will develop their own level these check to do, though, when the trouble to write the project a number of steps, but after doing so, we are carrying out database migration or migration project, it will be a lot easier, you see this on their own to measure. mysql In addition to data verification, you slowly learning process will find that it can do much, much, your program will do a lot of things are swept.

 

There may be a problem later changed to strict mode:

    If the setting mode contains NO_ZERO_DATE, then MySQL database does not allow zero-insertion date, insert zero dates will throw an error instead of a warning. TIMESTAMP field for example a table containing a column (or NULL if the display is not declared DEFAULT clause) is automatically assigned DEFAULT '0000-00-00 00:00:00' (zero time stamp), this test is also a table or column day the default date permit insertion of zero '0000-00-00' COMMENT 'date'; these are clearly not satisfied in NO_ZERO_DATE sql_mode be given.

 

Mode setting and modification (to solve the above problems, for example):

    A mode : performing first select @@ sql_mode, check out the copy of the value and wherein NO_ZERO_IN_DATE, NO_ZERO_DATE deleted, and then performs set sql_mode = 'modified values' or set session sql_mode = 'modified value'; for example: set session sql_mode = 'STRICT_TRANS_TABLES'; strict mode to

        This method only applies to the current session, close the current session will not enter into force.

    Second way : first implementation of select @@ global.sql_mode, check out the value and copy them NO_ZERO_IN_DATE, NO_ZERO_DATE delete, and then do set global sql_mode = 'modified values'.

        This method is effective on the current service, re MySQL service failed

 

    Method three : mysql installation directory or file my.cnf (windows my.ini file system), the new sql_mode = ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION,

        My.cnf add the following:

        [mysqld]

        sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER

 

        Then restart the mysql.

    This method is permanent. Of course, the production environment is prohibited to restart the MySQL service, so by way of two plus Three ways to solve the problem line, then even the day really restart the MySQL service will permanently into effect.

Guess you like

Origin www.cnblogs.com/taosiyu/p/11229946.html