mysql error handler 例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 错误控制DECLARE { exit|continue } HANDLER FOR {SQLSTATE [VALUE] | condition_name  | SQLWARNING  | NOT FOUND  | SQLEXCEPTION  | mysql_error_code} SQLWARNING    是对所有以01开头的SQLSTATE代码的速记。 NOT FOUND 
错误控制 DECLARE { exit|continue } HANDLER FOR
{SQLSTATE [VALUE] | condition_name  | SQLWARNING  | NOT FOUND  | SQLEXCEPTION  | mysql_error_code}

SQLWARNING    是对所有以01开头的SQLSTATE代码的速记。
NOT FOUND     是对所有以02开头的SQLSTATE代码的速记。
SQLEXCEPTION  是对所有没有被SQLWARNING或NOT FOUND捕获的SQLSTATE代码的速记

example:
DECLARE EXIT HANDLER FOR 1452
begin
....
end;
切记, declare 后不需要 ;
前提
create table t1 ( id int not null auto_increment primary key ) engine=innodb;
create table t2 ( num int, key(num), foreign key(num) REFERENCES t1(id) ) engine=innodb;

错误日志记录
create table error_log (id int not null auto_increment primary key,
time datetime, code int,  msg varchar(80) );

create table t3 ( id int not null );

create table t4 ( id int check  id > 10 );

create table t5 ( id int not null auto_increment primary key, sex char(1), check sex in  ('m','f'));
ex1
ex: 希望记录 insert 失败的记录 (失败原因, 外键)

插入错误信息具有错误代码 ERROR 1452 (23000)


delimiter //
create procedure test12 ( in a int)
begin
DECLARE EXIT HANDLER FOR 1452
  begin
   insert into error_log (time,code, msg) values(now(), 1452, concat("t2 Foreign key reference failure for value=",a));
  end;
insert into t2 values (a);
end;
//
delimiter ;
ex2
更新 t1 表主键中涉及 t2 外键报错
ERROR 1451 (23000):

delimiter //
create procedure test18 ( in a int, in b int )
begin
DECLARE EXIT HANDLER FOR 1451
  begin
   insert into error_log (time,code, msg) values(now(), 1451, concat("t1 update faile, reference t2 for value=",b));
  end;
update t1 set id=b where id=a;
end;
//
delimiter ;

call test18(1,10);
ex3
t1 插入重复值 错误代码 1062

delimiter //
create procedure test13( in a int)
begin
declare exit  handler for 1062
begin
  insert into error_log (time,code, msg) values (now(), 1062, concat('t1 Duplicate entry=',a));
end;
insert into t1 values (a);
end;
//
delimiter ;
ex4
t3 表插入空值
假如直接插入, 则只出现警告, 默认使用 0 值取代

令无法插入 null 值

delimiter //
create procedure test15( in a int )
begin
declare exit handler for 1048
begin
  insert into error_log (time,code, msg) values (now(), 1048, 'table t3 empty values');
end;
insert into t3 values (a);
end;
//
delimiter ;

测试:
set @a=1;
call test15(@a); <- 插入t3;
call test15(@b); <- 插入error_log
ex5
参考官方 13.1.2. ALTER TABLE语法 描述:

所有的存储引擎均对CHECK子句进行分析,但是忽略CHECK子句  (参考 show create table t4, 没有check 语法)

只能够利用存储过程应用对应检查 令 check id > 10 生效

delimiter //
create procedure test16( in a int )
begin
  if a <= 10
  then
insert into error_log (time,code, msg) values (now(), 9901, concat('table t4 values less then 10, values=',a));
  else
    insert into t4 values (a);
  end if;
end;
//
delimiter ;
ex6
令 check sex in ('m','f') 生效

delimiter //
create procedure test17( in a char(1) )
begin
  if a <> 'm' and a <> 'f'
  then
insert into error_log (time,code, msg) values (now(), 9901, concat('table t5 sex values not in, values=',a));
  else
    insert into t5 (sex) values (a);
  end if;
end;
//
delimiter ;
ex7
t3 表插入空值  (使用 sqlstate 控制码)
假如直接插入, 则只出现警告, 默认使用 0 值取代,
令插入时具有默认值

tt@localhost[new]>insert into t3 values (@c);
ERROR 1048 (23000): Column 'id' cannot be null  [23000 <- 控制码]



delimiter //
create procedure test19( in a int )
begin
declare exit handler for SQLSTATE '23000'
begin
  insert into error_log (time,code, msg) values (now(), 23000, 'table t3 empty values');
  insert into t3 values (1);
end;
insert into t3 values (a);
end;
//
delimiter ;

测试:
set @a=1;
call test15(@a); <- 插入t3;
call test15(@b); <- 插入error_log 与 t3 表
ex8
错重错误定义 
create unique index id_idx on t3(id);

1. 重复键 [ERROR 1062 (23000)] 2. 插入字符, 第一次默认插入 0 3. 空值 [ERROR 1048 (23000):]
注意:  in a varchar(20) 才能够判断是否字符 int 则全自动转换成为数字

delimiter //
create procedure test21( in a varchar(20))
begin

declare exit handler for 1062
begin
  insert into error_log (time,code, msg) values (now(), 23000, 'table t3 duplicate key');
end;

declare exit handler for 1048
begin
   insert into error_log (time,code, msg) values (now(), 23000, 'table t3 null values');
end;

if ( a regexp  '^[[:digit:]]*$' ) = 1
then
  insert into t3 values (a);
else
  insert into error_log (time,code, msg) values (now(), 23000, 'table t3 not number');
end if;
end;
//
delimiter ;
 
判断纯字符函数
isnum() 函数
delimiter //
create function isnum( a char(20))
returns int
begin
declare r int;
select a regexp  '^[[:digit:]]*$' into r;
if r = 1
then
   return 1;
else
   return 0;
end if;
end;
//
delimiter ;
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5月前
|
关系型数据库 MySQL
MySQL 报错 [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file
MySQL 报错 [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file
407 0
|
1月前
|
关系型数据库 MySQL
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
28 0
|
1月前
|
SQL 关系型数据库 MySQL
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet', sys...
|
7月前
|
关系型数据库 MySQL Linux
【Linux环境】centos安装mysql5.7.26报 ./mysqld: error while loading shared libraries: libaio.so.1: cannot op
【Linux环境】centos安装mysql5.7.26报 ./mysqld: error while loading shared libraries: libaio.so.1: cannot op
128 0
|
4月前
|
关系型数据库 MySQL
ERROR 1449 (HY000): The user specified as a definer (‘mysql.infoschema‘@‘localhost‘) does not exist
ERROR 1449 (HY000): The user specified as a definer (‘mysql.infoschema‘@‘localhost‘) does not exist
25 0
|
7月前
|
XML 关系型数据库 MySQL
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
132 0
|
7月前
|
关系型数据库 MySQL Devops
docker容器刚启动就停止 — 运行mysql 报错 mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
docker容器刚启动就停止 — 运行mysql 报错 mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
274 0
|
4月前
|
SQL 数据采集 关系型数据库
如何解决MySQL报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL?
如何解决MySQL报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL?
|
4天前
|
关系型数据库 MySQL 数据库
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
|
11天前
|
SQL 关系型数据库 MySQL
MySQL SQL error: #1271 - Illegal mix of collations for operation ‘UNION‘
MySQL SQL error: #1271 - Illegal mix of collations for operation ‘UNION‘