You are on page 1of 5

CS8481-Database Management Systems Lab Department of CSE 2020-2021

EX:NO: 7 EXCEPTION HANDLING


AIM:

To implement and execute PROCEDURE that handles all types of exceptions in MySQL
Database using Procedural Language concepts.
EXCEPTIONS HANDLER SYNTAX:

DECLARE handler_action HANDLER


FOR condition_value [, condition_value]
... statement

handler_action:
CONTINUE
| EXIT
| UNDO

condition_value:
mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION

The DECLARE….HANDLER statement specifies a handler that deals with one or more conditions. If one of
these conditions occurs, the specified statement executes. statement can be a simple statement such as SET
var_name = value, or a compound statement written using BEGIN and ENDThe handler_action value indicates
what action the handler takes after execution of the handler statement:

 CONTINUE: Execution of the current program continues.


 EXIT: Execution terminates for the BEGIN ... END compound statement in which the handler is
declared. This is true even if the condition occurs in an inner block.
 UNDO: Not supported.

The condition_value for DECLARE ... HANDLER indicates the specific condition or class of conditions that
activates the handler:

 A MySQL error code (a number) or an SQLSTATE value (a 5-character string literal).


 A condition name previously specified with DECLARE ... CONDITION. A condition name can be
associated with a MySQL error code or SQLSTATE value.
 SQLWARNING is shorthand for the class of SQLSTATE values that begin with '01'.
 NOT FOUND is shorthand for the class of SQLSTATE values that begin with '02'.
 SQLEXCEPTION is shorthand for the class of SQLSTATE values that do not begin with '00', '01', or
'02'.

St.Joseph’s College of 1
Engineering
Some common MySQL error codes and SQLSTATE codes
MySQL SQLSTATE Error message
error code code
1011 HY000 Error on delete of ‘%s’ (errno: %d)
Disk full (%s); waiting for someone to free
1021 HY000
some space . . .
1022 23000 Can’t write; duplicate key in table ‘%s’
1027 HY000 ‘%s’ is locked against change
1036 HY000 Table ‘%s’ is read only
1048 23000 Column ‘%s’ cannot be null
1062 23000 Duplicate entry ‘%s’ for key %d
Table ‘%s’ was locked with a READ lock
1099 HY000
and can’t be updated
Table ‘%s’ was not locked with
1100 HY000
LOCK TABLES
The SELECT would examine more than
MAX_JOIN_SIZE rows; check your
WHERE and use SET
1104 42000 SQL_BIG_SELECTS=1 or SET
SQL_MAX_JOIN_WHERE and use SET
SQL_BIG_SELECTS=1 or SET
SQL_MAX_JOIN_SIZE=# if the SELECT is
okay
1106 42000 Incorrect parameters to procedure ‘%s’
1114 HY000 The table ‘%s’ is full
Delayed insert thread couldn’t get
1150 HY000
requested lock for table %s
INSERT DELAYED can’t be used with table
1165 HY000
‘%s’ because it is locked with LOCK
TABLES
1242 21000 Subquery returns more than 1 row
Column set to default value; NULL supplied
1263 22004
to NOT NULL column ‘%s’ at row %ld
Out of range value adjusted for column ‘%s’
1264 22003
at row %ld
1265 1000 Data truncated for column ‘%s’ at row %ld
1312 0A000 SELECT in a stored program must have INTO
1317 70100 Query execution was interrupted
1319 42000 Undefined CONDITION: %s

DUPLICATE KEY EXCEPTION


mysql>create table article(art_id int,tag_id int,primary key(art_id,tag_id);
mysql> delimiter $$;

mysql> desc article;


-> $$
+ + + + -+ + +
| Field | Type | Null | Key | Default | Extra |
+ + + + -+ + +
| art_id | int(11) | NO | PRI | 0 | |
| tag_id | int(11) | NO | PRI | 0 | |
+ + + + -+ + +
2 rows in set (0.00 sec)

mysql> CREATE PROCEDURE in_article(IN art_id INT,IN tag_id INT)


-> BEGIN
-> DECLARE CONTINUE HANDLER for 1062
-> SELECT CONCAT('Duplicate Keys(art_id,tag_id)found') AS msg;
-> INSERT INTO article(art_id,tag_id)
-> Values(art_id,tag_id);
-> select COUNT(*) from article;
-> END $$
Query OK, 0 rows affected (0.00 sec)

mysql> CALL in_article(1,1);


-> $$
+ +
| COUNT(*) |
+ +
| 1 |
+ +
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL in_article(1,2);


-> $$
+ +
| COUNT(*) |
+ +
| 2 |
+ +
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL in_article(1,1);


-> $$
+ -+
| msg |
+ - -+
| Duplicate Keys(art_id,tag_id)found |
+ -+
1 row in set (0.00 sec)

+ +
| COUNT(*) |
+ +
| 2 |
+ +
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)


RESULT:

Thus the procedure was implemented to handle all types of exceptions in MySQL Database using
Procedural Language concepts.

You might also like