sql_mode MySQL and set of analytical sql_mode

<!DOCTYPE html>







MySQL的sql_mode解析与设置 - fireporsche - 博客园












MySQL and set of analytical sql_mode

Reference Address: http://blog.csdn.net/ccccalculator/article/details/70432123

Because use group by 1055 in MySQL is always an error occurs, which leads to the need to see what is the reason, and query the relevant information, notes will now be recorded for later use can refer to:

sql_mode: short: it defines sql syntax MySQL should support you, to check the data, etc.

select @@ sql_mode: Use this command we can view our current database of sql_mode

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Let us look at the meaning of each value sql_mode:

ONLY_FULL_GROUP_BY:

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

Because there only_full_group_by, so we have to use the right in MySQL group by statement, then only select column1 from tb1 group by column1 ( showing only group by a field, the other was reported to be wrong 1055 )

Example:

mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)

mysql> select id,name from tt1 group by name;
ERROR 1055 (42000):
mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)
mysql> select * from tt1 group by name;
ERROR 1055 (42000):
mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)
mysql> select name from tt1 group by name;
+-------+
| name  |
+-------+
| cai   |
| li    |
| wang  |
| xiong |
| ying  |
| zhang |
+-------+
6 rows in set (0.00 sec)

So we have to use words correctly use the group by must be removed only_full_group_by

the sql_mode = SET (SELECT Replace (@@ the sql_mode, 'ONLY_FULL_GROUP_BY', '')); the statement to be used to replace spaces out only_full_group_by, so that we can use

mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.00 sec)

mysql> select id,name from tt1 group by name;
+----+-------+
| id | name  |
+----+-------+
|  3 | cai   |
|  5 | li    |
|  6 | wang  |
|  1 | xiong |
|  2 | ying  |
|  4 | zhang |
+----+-------+
6 rows in set (0.00 sec)
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tt1 group by name;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  3 | cai   |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  4 | zhang |      0 |
+----+-------+--------+
6 rows in set (0.00 sec)

However, this method is only made temporary changes, we can change the configuration file my.ini

sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

STRICT_TRANS_TABLES:

 In this mode, if a value of a transaction can not be inserted into the table, it interrupts the current operation of the non-transactional tables without any restrictions

NO_ZERO_IN_DATE:

 In strict mode, do not accept the month or day part of the date zero. If you use the IGNORE option, we insert into a similar date '0000-00-00'. In non-strict mode, you can accept that date, but will generate a warning.

NO_ZERO_DATE:

In strict mode, do not '0000-00-00' as a legitimate date. You can still insert zero dates with the IGNORE option. In non-strict mode, you can accept that date, but will generate a warning

ERROR_FOR_DIVISION_BY_ZERO:

In strict mode, the INSERT or UPDATE process if division by zero (or MOD (X, 0)), then an error (or warning). If the pattern is not given, MySQL returns NULL when it is zero. If used in INSERT IGNORE or UPDATE IGNORE, MySQL generates a warning is zero, but the operation result is NULL

NO_AUTO_CREATE_USER:

Prevent GRANT automatically creates a new user, unless you also specify a 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

 

Third, it is said to be MySQL5.0 sql_mode or later supports three modes: ANSI, TRADITIONAL and STRICT_TRANS_TABLES. 

1, ANSI modes: Relaxed mode, change the syntax and behavior, to make it more in line with standard SQL. Insertion of data validation, if it does not define the type or length, data type, or to adjust the truncation saved, a warning message warning. For the beginning of the error referred to herein, may be set to ANSI sql_mode first mode, so that it can be inserted into the data, and the result for the divisor field values ​​of 0, the database will be replaced with a NULL value.

The current database is set to ANSI mode:

 mysql>  set @@sql_mode=ANSI;  

2, TRADITIONAL modes: strict mode, when inserting data into mysql database, strictly checking the data to ensure that incorrect data can not be inserted, error report errors, rather than warnings. When applied to things, things will be rolled back. NOTE: Once an error is found immediately abandon INSERT / UPDATE. If you use non-transactional storage engine, this approach is not what you want, because data errors before changes are not "rolling" appears, the result is an updated "only a part."

The current database mode is set to TRADITIONAL mode:

 
  1. mysql> set @@sql_mode=TRADITIONAL;   

3, STRICT_TRANS_TABLES modes: strict mode, the strict checking of data, incorrect data can not be inserted, reported error error. If you can not insert the given value to the transaction table, then give up the statement. For non-transactional table, if the value appears in the first row or rows with a single statement statement, the statement is discarded.

The current database mode to STRICT_TRANS_TABLES mode:

 
  1. mysql> set @@sql_mode=STRICT_TRANS_TABLES;  

Not the best and the worst mode, only the most suitable mode. You need to choose the most suitable mode according to their actual situation! ! !

Another said that, here's database schema changes are session-level, one-time, closed and then opened it does not count! ! !

You may be provided by the configuration file: vim /etc/my.cnf
added follows the my.cnf (the my.ini):
[mysqld]
the sql_mode = 'you want mode'

 

@. Posted 2018-03-21 18:10 fireporsche read ( ... ) Comments ( ... ) edit collections

Reference Address: http://blog.csdn.net/ccccalculator/article/details/70432123

Because use group by 1055 in MySQL is always an error occurs, which leads to the need to see what is the reason, and query the relevant information, notes will now be recorded for later use can refer to:

sql_mode: short: it defines sql syntax MySQL should support you, to check the data, etc.

select @@ sql_mode: Use this command we can view our current database of sql_mode

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Let us look at the meaning of each value sql_mode:

ONLY_FULL_GROUP_BY:

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

Because there only_full_group_by, so we have to use the right in MySQL group by statement, then only select column1 from tb1 group by column1 ( showing only group by a field, the other was reported to be wrong 1055 )

Example:

mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)

mysql> select id,name from tt1 group by name;
ERROR 1055 (42000):
mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)
mysql> select * from tt1 group by name;
ERROR 1055 (42000):
mysql> select * from tt1;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  3 | cai   |      0 |
|  4 | zhang |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
+----+-------+--------+
6 rows in set (0.00 sec)
mysql> select name from tt1 group by name;
+-------+
| name  |
+-------+
| cai   |
| li    |
| wang  |
| xiong |
| ying  |
| zhang |
+-------+
6 rows in set (0.00 sec)

So we have to use words correctly use the group by must be removed only_full_group_by

the sql_mode = SET (SELECT Replace (@@ the sql_mode, 'ONLY_FULL_GROUP_BY', '')); the statement to be used to replace spaces out only_full_group_by, so that we can use

mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.00 sec)

mysql> select id,name from tt1 group by name;
+----+-------+
| id | name  |
+----+-------+
|  3 | cai   |
|  5 | li    |
|  6 | wang  |
|  1 | xiong |
|  2 | ying  |
|  4 | zhang |
+----+-------+
6 rows in set (0.00 sec)
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tt1 group by name;
+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  3 | cai   |      0 |
|  5 | li    |      1 |
|  6 | wang  |      1 |
|  1 | xiong |      0 |
|  2 | ying  |      0 |
|  4 | zhang |      0 |
+----+-------+--------+
6 rows in set (0.00 sec)

However, this method is only made temporary changes, we can change the configuration file my.ini

sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

STRICT_TRANS_TABLES:

 In this mode, if a value of a transaction can not be inserted into the table, it interrupts the current operation of the non-transactional tables without any restrictions

NO_ZERO_IN_DATE:

 In strict mode, do not accept the month or day part of the date zero. If you use the IGNORE option, we insert into a similar date '0000-00-00'. In non-strict mode, you can accept that date, but will generate a warning.

NO_ZERO_DATE:

In strict mode, do not '0000-00-00' as a legitimate date. You can still insert zero dates with the IGNORE option. In non-strict mode, you can accept that date, but will generate a warning

ERROR_FOR_DIVISION_BY_ZERO:

In strict mode, the INSERT or UPDATE process if division by zero (or MOD (X, 0)), then an error (or warning). If the pattern is not given, MySQL returns NULL when it is zero. If used in INSERT IGNORE or UPDATE IGNORE, MySQL generates a warning is zero, but the operation result is NULL

NO_AUTO_CREATE_USER:

Prevent GRANT automatically creates a new user, unless you also specify a 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

 

Third, it is said to be MySQL5.0 sql_mode or later supports three modes: ANSI, TRADITIONAL and STRICT_TRANS_TABLES. 

1, ANSI modes: Relaxed mode, change the syntax and behavior, to make it more in line with standard SQL. Insertion of data validation, if it does not define the type or length, data type, or to adjust the truncation saved, a warning message warning. For the beginning of the error referred to herein, may be set to ANSI sql_mode first mode, so that it can be inserted into the data, and the result for the divisor field values ​​of 0, the database will be replaced with a NULL value.

The current database is set to ANSI mode:

 mysql>  set @@sql_mode=ANSI;  

2, TRADITIONAL modes: strict mode, when inserting data into mysql database, strictly checking the data to ensure that incorrect data can not be inserted, error report errors, rather than warnings. When applied to things, things will be rolled back. NOTE: Once an error is found immediately abandon INSERT / UPDATE. If you use non-transactional storage engine, this approach is not what you want, because data errors before changes are not "rolling" appears, the result is an updated "only a part."

The current database mode is set to TRADITIONAL mode:

 
  1. mysql> set @@sql_mode=TRADITIONAL;   

3, STRICT_TRANS_TABLES modes: strict mode, the strict checking of data, incorrect data can not be inserted, reported error error. If you can not insert the given value to the transaction table, then give up the statement. For non-transactional table, if the value appears in the first row or rows with a single statement statement, the statement is discarded.

The current database mode to STRICT_TRANS_TABLES mode:

 
  1. mysql> set @@sql_mode=STRICT_TRANS_TABLES;  

Not the best and the worst mode, only the most suitable mode. You need to choose the most suitable mode according to their actual situation! ! !

Another said that, here's database schema changes are session-level, one-time, closed and then opened it does not count! ! !

You may be provided by the configuration file: vim /etc/my.cnf
added follows the my.cnf (the my.ini):
[mysqld]
the sql_mode = 'you want mode'

 

Guess you like

Origin www.cnblogs.com/souhalois/p/11248148.html