13.7.7.42 SHOW WARNINGS statement

Official document address: 13.7.7.42 SHOW WARNINGS Statement


SHOW WARNINGS [LIMIT [offset,] row_count]

SHOW COUNT(*) WARNINGS

SHOW WARNINGSIs a diagnostic statement used to display information about the conditions ( error, waringand note) caused by the execution of the statement in the current session . DML statements (such as INSERT, UPDATE, LOAD DATA) and DDL statements (such as CREATE TABLE, ALTER TABLE) will generate a warning.

LIMITThe clause has the SELECTsame syntax as the statement. See 13.2.10 SELECT statement .

SHOW WARNINGSIt can also be used EXPLAINlater to display the EXPLAINgenerated extended information. See 8.8.3 Extended EXPLAIN output format .

SHOW WARNINGSDisplays information about conditions generated by the most recently executed non-diagnostic statement in the current session. If the most recent sentence caused an error during the parsing process, the result SHOW WARNINGSwill be displayed regardless of the sentence type (diagnostic or non-diagnostic).

SHOW COUNT(*) WARNINGSDiagnostic statements show error, waringand notethe total. You can also warning_countretrieve this value from system variables:

SHOW COUNT(*) WARNINGS;

SELECT @@warning_count;

The difference between these statements is that the first one is a diagnostic statement that does not clear the message list. The second statement, because it is a SELECTstatement, is considered non-diagnostic and will clear the message list.

A related diagnostic sentence SHOW ERRORSonly displays errorthe situation (not including waringand note), and the SHOW COUNT(*) ERRORSsentence only displays the errortotal number. See 13.7.7.17 SHOW ERRORS statement . GET DIAGNOSTICSThe statement can be used to check the information of a single condition. See 13.6.7.3 GET DIAGNOSTICS statement .

The following is a simple example, which shows the INSERTdata conversion warning for the target . This example assumes that strict SQL mode is disabled. If strict mode is enabled, the warning will turn into an error and terminate INSERT.

mysql> CREATE TABLE t1 (a TINYINT NOT NULL, b CHAR(4));
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO t1 VALUES(10,'mysql'), (NULL,'test'), (300,'xyz');
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 3

mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
  Level: Warning
   Code: 1265
Message: Data truncated for column 'b' at row 1
*************************** 2. row ***************************
  Level: Warning
   Code: 1048
Message: Column 'a' cannot be null
*************************** 3. row ***************************
  Level: Warning
   Code: 1264
Message: Out of range value for column 'a' at row 3
3 rows in set (0.00 sec)

max_error_countThe system control variables stored in the server error, waringand the notemaximum number of messages to control the SHOW WARNINGSnumber of messages displayed. To change the number of messages that the server can store, change max_error_countthe value.

max_error_countOnly control the number of messages stored, not the number of messages counted. warning_countThe value of is not max_error_countlimited, even if the number of generated messages exceeds max_error_count. The following example demonstrates this. ALTER TABLEThe statement produces three warning messages (strict SQL mode is disabled in this example to prevent errors after a single conversion problem). Because it max_error_countis set to 1, only one message is stored and displayed, but all three messages are counted (as warning_countshown by the value of):

mysql> SHOW VARIABLES LIKE 'max_error_count';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_error_count | 1024  |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> SET max_error_count=1, sql_mode = '';
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE t1 MODIFY b CHAR;
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 3

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1263 | Data truncated for column 'b' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT @@warning_count;
+-----------------+
| @@warning_count |
+-----------------+
|               3 |
+-----------------+
1 row in set (0.01 sec)

To disable message storage, max_error_countset to 0. In this case, it warning_countstill indicates how many warnings have occurred, but the message will not be stored and cannot be displayed.

sql_notesSystem variables control notewhether messages increase in warning_countvalue and whether the server stores them. By default, it sql_notesis 1, but if set to 0, notemessages will not increase in warning_countvalue and the server will not store them:

mysql> SET sql_notes = 1;
mysql> DROP TABLE IF EXISTS test.no_such_table;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+-------+------+------------------------------------+
| Level | Code | Message                            |
+-------+------+------------------------------------+
| Note  | 1051 | Unknown table 'test.no_such_table' |
+-------+------+------------------------------------+
1 row in set (0.00 sec)

mysql> SET sql_notes = 0;
mysql> DROP TABLE IF EXISTS test.no_such_table;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW WARNINGS;
Empty set (0.00 sec)

MySQL server sends a count to each client, the client indicates the most recently executed statement generated error, waringand notethe total number. In the C API, mysql_warning_count()this value can be obtained by calling a function.

On the mysqlclient, you can use the warningsand nowarningcommands, or their shortcuts, \Wand \wto enable and disable the automatic warning display (see 4.5.1.2 mysql client commands ). E.g:

mysql> \W
Show warnings enabled.
mysql> SELECT 1/0;
+------+
| 1/0  |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.03 sec)

Warning (Code 1365): Division by 0
mysql> \w
Show warnings disabled.

Guess you like

Origin blog.csdn.net/wb1046329430/article/details/115036118