Home > Database > Mysql Tutorial > body text

MySQL5的异常处理_MySQL

WBOY
Release: 2016-06-01 13:53:55
Original
855 people have browsed it

  1. Sample Problem: Log Of Failures 问题样例:故障记录

  当INSERT失败时,我希望能将其记录在日志文件中我们用来展示出错处理的问题样例是很普通的。我希望得到错误的记录。当INSERT失败时,我想在另一个文件中记下这些错误的信息,例如出错时间,出错原因等。我对插入特别感兴趣的原因是它将违反外键关联的约束

  2. Sample Problem: Log Of Failures (2)

  mysql> CREATE TABLE t2

  1 INT, PRIMARY KEY (s1))

  engine=innodb;//

  mysql> CREATE TABLE t3 (s1 INT, KEY (s1),

  FOREIGN KEY (s1) REFERENCES t2 (s1))

  engine=innodb;//

  mysql> INSERT INTO t3 VALUES (5);//

  ...

  ERROR 1216 (23000): Cannot add or update a child row: a foreign key

  constraint fails(这里显示的是系统的出错信息)

  我开始要创建一个主键表,以及一个外键表。我们使用的是InnoDB,因此外键关联检查是打开的。然后当我向外键表中插入非主键表中的值时,动作将会失败。当然这种条件下可以很快找到错误号1216。

  3. Sample Problem: Log Of Failures

  CREATE TABLE error_log (error_message

  CHAR(80))//

  下一步就是建立一个在做插入动作出错时存储错误的表。

  4. Sample Problem: Log Of Errors

  CREATE PROCEDURE p22 (parameter1 INT)

  BEGIN

  DECLARE EXIT HANDLER FOR 1216

  INSERT INTO error_log VALUES

  (CONCAT('Time: ',current_date,

  '. Foreign Key Reference Failure For

  Value = ',parameter1));

  INSERT INTO t3 VALUES (parameter1);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template