
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we use SIGNAL statement with MySQL triggers?
Actually, MySQL SIGNAL statement is an error handling mechanism for handling unexpected occurrences and a graceful exit from the application if need to be. Basically, it provides error information to the handler. Its basic syntax would be as follows −
SIGNAL SQLSTATE | condition_value [SET signal_information_item = value_1,[, signal_information_item] = value_2, etc;]
Here, the SIGNAL keyword is an SQLSTATE value or a condition name declared by a DECLARE CONDITION statement. The SIGNAL statement must always specify an SQLSTATE value or a named condition that defined with an SQLSTATE value. The SQLSTATE value for a The SIGNAL statement consists of a five-character alphanumeric code. We must not start our own SQLSTATE code with '00' because such values indicate success and are not valid for signaling an error. If our value is invalid, a Bad SQLSTATE error occurs. For catch-all error handling, we should assign an SQLSTATE value of '45000', which signifies an “unhandled user-defined exception.”
To illustrate the use of SIGNAL statement with MySQL triggers we are using the following example in which we are creating a BEFORE INSERT trigger on the student_age table. It will throw an error message using SIGNAL statement if we will try to enter the age of less than 0.
Example
mysql> Create trigger before_insert_studentage BEFORE INSERT on student_age FOR EACH ROW BEGIN IF NEW.age<0 then SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'age less than 0'; END if; END; // Query OK, 0 rows affected (0.12 sec) mysql> Insert into student_age(age, name) values(-10,'gaurav')// ERROR 1644 (45000): age less than 0
It is clear from the above result set that if we will try to insert age less than 0 then it will throw an error using SIGNAL statement.
- Related Articles
- How can we use a MySQL subquery with INSERT statement?
- Can we use ADD and CHANGE with ALTER Statement in MySQL?
- How we can find all the triggers associated with a particular MySQL table?
- Can we use WHERE clause inside MySQL CASE statement?
- Can we use SELECT NULL statement in a MySQL query?
- Can we use Switch statement with Strings in java?
- How can we get the metadata of triggers?
- How can we use MySQL REPLACE statement to prevent of insertion of duplicate data?
- How can we use ASCII() function with MySQL WHERE clause?
- How can we use CHAR_LENGTH() function with MySQL WHERE clause?
- How can we use BIN() function with MySQL WHERE clause?
- How can we use FIND_IN_SET() function with MySQL WHERE clause?
- How can we use MySQL INSTR() function with WHERE clause?
- How can we use MySQL SUM() function with HAVING clause?
- How can we use a MySQL subquery with FROM clause?
