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.