Brief analysis of MySQL Exception Handling

  • 2020-06-15 10:24:36
  • OfStack

The exception handling analysis of MySQL is as follows:

A standard format


DECLARE handler_type HANDLER FOR condition_value[,...] statement
handler_type:
  CONTINUE
 | EXIT
 | UNDO -- This is not supported for the time being 
condition_value:
  SQLSTATE [VALUE] sqlstate_value
 | condition_name
 | SQLWARNING
 | NOT FOUND
 | SQLEXCEPTION
 | mysql_error_code
condition_value details 

1. List of common MYSQL ERROR CODE

http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
See the MySQL installation path for a more detailed list of errors
Such as I/usr/local/mysql share/mysql/errmsg txt
Note 1 below: SQLSTATE [VALUE] sqlstate_value this format is specifically for ANSI SQL and ODBC and other standards.
Not all MySQL ERROR CODE map to SQLSTATE.

If you don't want to put in ERROR CODE, use shorthand terms instead

SQLWARNING stands for all error codes that begin with 01
NOT FOUND represents all error codes beginning with 02, or of course a cursor reaching the end of the dataset.
SQLEXCEPTION represents all error codes except SQLWARNING and NOT FOUND

Let's use the examples in the manual now


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 '23000' BEGIN END; --  Exit if a duplicate key is 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> 

Now let's look at the case where an error continues


mysql> truncate table t;
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER $$
mysql> DROP PROCEDURE IF EXISTS `t_girl`.`handlerdemo`$$
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DEFINER=`root`@`localhost` PROCEDURE `handlerdemo`()
  -> BEGIN
  -> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' BEGIN END;
  -> 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.01 sec)
mysql> DELIMITER ;
mysql> call handlerdemo();
Query OK, 0 rows affected (0.00 sec)
mysql> select @x;
+------+
| @x |
+------+
| 3 | 
+------+
1 row in set (0.00 sec)
mysql> call handlerdemo();
Query OK, 0 rows affected (0.00 sec)
mysql> select @x;
+------+
| @x |
+------+
| 3 | 
+------+
1 row in set (0.00 sec)
mysql> 

As you can see, it works to the end.
Of course, SQLSTATE '23000' above can be replaced with 1062
Let's look at the first warning.


mysql> alter table t add s2 int not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

There is no default value for this column, and a warning or 1364 error will appear when inserted.


mysql> DELIMITER $$
mysql> DROP PROCEDURE IF EXISTS `t_girl`.`handlerdemo`$$
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE DEFINER=`root`@`localhost` PROCEDURE `handlerdemo`()
  -> BEGIN
  -> DECLARE CONTINUE HANDLER FOR 1062 BEGIN END;
  -> DECLARE CONTINUE HANDLER FOR SQLWARNING
  -> BEGIN
  -> update t set s2 = 2;
  -> END;
  -> DECLARE CONTINUE HANDLER FOR 1364
  -> BEGIN
  -> INSERT INTO t(s1,s2) VALUES (1,3);
  -> END; 
  -> SET @x = 1;
  -> INSERT INTO t(s1) VALUES (1);
  -> SET @x = 2;
  -> INSERT INTO t(s1) 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 * from t;
+----+----+
| s1 | s2 |
+----+----+
| 1 | 3 | 
+----+----+
1 row in set (0.00 sec)

A new record inserted when an error is encountered.


mysql> select @x;
+------+
| @x |
+------+
| 3 | 
+------+
1 row in set (0.00 sec)


Related articles: