MySQL database Exception Handling

Source: Internet
Author: User

MySQL databaseSome exceptions may occur during the operation. In this case, the content described below will help you.

MySQL exception handling:

Standard Format

Reference content is as follows:

DECLARE handler_type handler for condition_value [,...] statement
Handler_type:
CONTINUE
| EXIT
| UNDO -- not supported currently
Condition_value:
SQLSTATE [VALUE] sqlstate_value
| Condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| Mysql_error_code

Condition_value details
 

1. MySQL error code list

To view more error lists, go to the MySQL installation path.

For example, my/usr/local/mysql/share/mysql/errmsg.txt

Note: The SQLSTATE [VALUE] sqlstate_value format is specialized for ansi SQL, ODBC, and other standards.

Not all MySQL ERROR codes are mapped to SQLSTATE.

2. If you do not need to insert error code, replace it with a shorthand condition.

SQLWARNING indicates all error codes starting with 01.

Not found indicates all error codes starting with 02. Of course, a cursor can also be reached at the end of the dataset.

SQLEXCEPTION indicates all error codes except SQLWARNING and not found.

3. Example:

Reference content is as follows:

Create TABLE t (s1 int, primary key (s1 ));
Mysql> use t_girl
Database changed
Mysql> create TABLE t (s1 int, primary key (s1 ));
Query OK, 0 rows affected (0.00 sec)
Mysql>
Mysql>
Mysql> DELIMITER |
Mysql> create PROCEDURE handlerdemo ()
-> BEGIN
-> Declare exit handler for sqlstate '2013' begin end; -- EXIT when duplicate key values are encountered
-> SET @ x = 1;
-> Insert INTO t VALUES (1 );
-> SET @ x = 2;
-> Insert INTO t VALUES (1 );
-> SET @ x = 3;
-> END |
Query OK, 0 rows affected (0.00 sec)
Mysql> DELIMITER;
Mysql> call handlerdemo ();
Query OK, 0 rows affected (0.00 sec)
Mysql> select @ x;
+ ------ +
| @ X |
+ ------ +
| 2 |
+ ------ +
1 row in set (0.00 sec)
Mysql> call handlerdemo ();
Query OK, 0 rows affected (0.00 sec)
Mysql> select @ x;
+ ------ +
| @ X |
+ ------ +
| 1 |
+ ------ +
1 row in set (0.00 sec)
Mysql>
 

There may be many exceptions in the MySQL database. Here we will introduce these exceptions for your reference. I will continue to introduce this knowledge to you later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.