Official document address: 13.7.7.42 SHOW WARNINGS Statement
SHOW WARNINGS [LIMIT [offset,] row_count]
SHOW COUNT(*) WARNINGS
SHOW WARNINGS
Is a diagnostic statement used to display information about the conditions ( error
, waring
and 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.
LIMIT
The clause has the SELECT
same syntax as the statement. See 13.2.10 SELECT statement .
SHOW WARNINGS
It can also be used EXPLAIN
later to display the EXPLAIN
generated extended information. See 8.8.3 Extended EXPLAIN output format .
SHOW WARNINGS
Displays 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 WARNINGS
will be displayed regardless of the sentence type (diagnostic or non-diagnostic).
SHOW COUNT(*) WARNINGS
Diagnostic statements show error
, waring
and note
the total. You can also warning_count
retrieve 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 SELECT
statement, is considered non-diagnostic and will clear the message list.
A related diagnostic sentence SHOW ERRORS
only displays error
the situation (not including waring
and note
), and the SHOW COUNT(*) ERRORS
sentence only displays the error
total number. See 13.7.7.17 SHOW ERRORS statement . GET DIAGNOSTICS
The 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 INSERT
data 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_count
The system control variables stored in the server error
, waring
and the note
maximum number of messages to control the SHOW WARNINGS
number of messages displayed. To change the number of messages that the server can store, change max_error_count
the value.
max_error_count
Only control the number of messages stored, not the number of messages counted. warning_count
The value of is not max_error_count
limited, even if the number of generated messages exceeds max_error_count
. The following example demonstrates this. ALTER TABLE
The 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_count
is set to 1
, only one message is stored and displayed, but all three messages are counted (as warning_count
shown 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_count
set to 0
. In this case, it warning_count
still indicates how many warnings have occurred, but the message will not be stored and cannot be displayed.
sql_notes
System variables control note
whether messages increase in warning_count
value and whether the server stores them. By default, it sql_notes
is 1
, but if set to 0
, note
messages will not increase in warning_count
value 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
, waring
and note
the total number. In the C API, mysql_warning_count()
this value can be obtained by calling a function.
On the mysql
client, you can use the warnings
and nowarning
commands, or their shortcuts, \W
and \w
to 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.