MySQL SHOW ERRORS

MySQL SHOW ERRORS

MySQL SHOW ERRORS Statement

Introduction

The SHOW ERRORS statement in MySQL is used to display error messages generated by the most recent SQL statement execution. It provides details about syntax errors, constraint violations, and other execution failures.

Unlike SHOW WARNINGS, which displays both warnings and errors, SHOW ERRORS only displays errors.

Syntax

SHOW ERRORS;

Optional Clause:

To limit the number of errors displayed, use:

SHOW ERRORS LIMIT n;

Where n is the number of errors to display.

Example Usage

1. Generate an Error

Let's execute an incorrect SQL statement and use SHOW ERRORS to inspect the error.

SELECT * FORM employees;

Error Output:

ERROR 1064 (42000): You have an error in your SQL syntax...

Now, use SHOW ERRORS to display the last error:

SHOW ERRORS;

Output:

+-------+------+------------------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------------------+ | Error | 1064 | You have an error in your SQL syntax... | +-------+------+------------------------------------------------+
  • Level → The type of message (Error).
  • Code → The MySQL error code (1064 for syntax error).
  • Message → The detailed error description.

2. Using LIMIT with SHOW ERRORS

To retrieve only the first error:

SHOW ERRORS LIMIT 1;

3. Checking the Number of Errors

To count the number of errors in the last statement:

SHOW COUNT(*) ERRORS;

Output:

+--------------+ | Errors | +--------------+ | 1 | +--------------+

Difference Between SHOW ERRORS and SHOW WARNINGS

CommandShows Errors?Shows Warnings?
SHOW ERRORS✅ Yes❌ No
SHOW WARNINGS✅ Yes✅ Yes

Conclusion

  • SHOW ERRORS displays only errors from the last executed SQL statement.
  • It helps debug queries by showing MySQL error messages.
  • Use SHOW COUNT(*) ERRORS to get the number of errors.
  • If you want both warnings and errors, use SHOW WARNINGS.

Would you like an example with stored procedures? 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close