195

I just ran a simple MySQL CREATE TABLE statement that produced the line

"Query OK, 0 rows affected, 1 warning (0.07 sec)."

It didn't actually show me what the warning was, though. How can you see the contents of a warning that just occurred? I'm using MySQL 5.1, if it makes a difference. The only thing I found online was "SHOW WARNINGS;" but that only produced

"Empty set (0.00 sec)."

1
  • 3
    as others have pointed out for interactive mysql you can, (1.) start the interactive session with the --show-warnings (see man mysql) or (2.) if you are in an existing interactive session, you can enable the same behavior with warnings (see man mysql). Jul 13, 2017 at 17:17

7 Answers 7

240

SHOW WARNINGS is the only method I'm aware of, but you have to run it immediately after a query that had warnings attached to it. If you ran any other queries in between, or dropped the connection, then SHOW WARNINGS won't work.

The MySQL manual page for SHOW WARNINGS doesn't indicate any other methods, so I'm fairly certain that you're stuck with it.

1
  • 1
    This helped me - but you have to run it immediately after a query that had warnings attached to it
    – asgs
    Oct 8, 2018 at 20:28
123

You can also set the command line to always display warnings after a query using \W

You can switch them off again with \w

3
  • 7
    This was perfect for me. Add '\W' to the top of my script and we're good to go!
    – Matt Klein
    Jul 18, 2013 at 17:35
  • 1
    It still exists: dev.mysql.com/doc/refman/5.7/en/mysql-commands.html but you may not be using the command-line mysql client. It only works in mysql, not in a MySQL library.
    – Yvan
    May 31, 2017 at 16:56
  • Is there a way to set this flag from the .my.cnf file?
    – StR
    Jan 24, 2020 at 12:34
24

@HorusKol, do you have documentation for that? I couldn't find any. But I did find out that the command line option --show-warnings will do the job, according to the MySQL manual.

2
  • 9
    if you do \? in the command line for MySQL help, you can see the two options for \w (warnings off) and \W (warnings on) - this may be version dependent, but I'm pretty sure it's been in MySQL 5 all this time.
    – HorusKol
    Feb 4, 2011 at 0:14
  • @HorusKol, double thanks for coming back and answering my horrible comment-as-an-answer. (I was new, and didn't know any better!)
    – Pops
    Feb 7, 2011 at 20:04
3

When MySQL database imports data, warnings often appear.

These warnings are easy to be ignored.

I suddenly want to see what the warnings are when I get to the data today.

MySQL view warning command

show warnings;

The command is very concise, and the general warning is like this when I check it.

1.

Warning | 1265 | Data truncated for column ‘title’ at row 1265

This warning is that the field length is not enough,

the imported data is automatically cropped by the system.

2.

| Warning | 1366 | Incorrect string value: ‘\xB5\xDA\xB6\xFE\xBD\xEC...‘ for column ‘Journal title’ at row 1444 |

This is the wrong character set of the data.

For the second one, you need to modify the character set of the database or the txt encoding format of the imported data.

3.

| Warning | 1366 | Incorrect integer value: ‘‘ for column ‘work number’ at row 13 |

This is to insert empty data, if the field allows null values, you can ignore these warnings. 4.

| Warning | 1262 | Row 11 was truncated; it contained more data than there were input columns |

This is a redundant column of imported data.

The MySQL warning is easy to understand, and it is easy to modify the table after the warning message.

0

All the suggestions above didn't work for me. I used SHOW WARNINGS\G and it worked.

0

You can use --show-warnings with login to show warnings as shown below. *By default, warnings are hidden:

mysql -u john -p --show-warnings

Or, you can use warnings(WARNINGS) or \W after login to show warnings as shown below:

mysql> warnings
Show warnings enabled.

Or:

mysql> WARNINGS
Show warnings enabled.

Or:

mysql> \W
Show warnings enabled.

Then, a warning is shown as below:

mysql> SELECT 1/0;
+------+
| 1/0  |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.00 sec)

Warning (Code 1365): Division by 0

And, you can use nowarning(NOWARNING) or \w after login to hide warnings as shown below:

mysql> nowarning
Show warnings disabled.

Or:

mysql> NOWARNING
Show warnings disabled.

Or:

mysql> \w
Show warnings disabled.

Then, a warning is hidden as below:

mysql> SELECT 1/0; 
+------+
| 1/0  |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.00 sec)

Or on Windows, you can set show-warnings under [mysql] in my.ini as shown below. *My answer explains [mysql] and my answer explains where my.ini is located on Windows:

# "my.ini"

[mysql]
...
show-warnings

Then, you can log in by setting my.ini's location to --defaults-file= or --defaults-extra-file= to show warnings as shown below. *--defaults-file= or --defaults-extra-file= must be the 1st option otherwise there is the error:

mysql --defaults-file='C:\ProgramData\MySQL\MySQL Server 8.0\my.ini' -u john -p

Or:

mysql --defaults-extra-file='C:\ProgramData\MySQL\MySQL Server 8.0\my.ini' -u john -p
0

You can use \W to enable and \w to disable the visibility of the warning. After enabling, the warning will be visible whenever it occurs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.