Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL trigger: print a warning message when an inserted entry is greater than a value

Tags:

database

mysql

I have create a table as below:

mysql> create table testa (a int, b int, c real);
Query OK, 0 rows affected (0.14 sec)

But when I want to implement a trigger like this, I face some syntax errors:

mysql> create trigger testa_trig  
       before insert ON testa 
       FOR EACH ROW 
       WHEN (NEW.c > 100) 
       BEGIN 
         Print "Warning: c > 100!"
       END;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN (NEW.c > 100) 
BEGIN
Print "Warning: c > 100!"
END' at line 4

I have checked the documentation at http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html but can't figure out the problem!

My MySQL version:

Server version: 5.5.38-0ubuntu0.12.04.1 (Ubuntu)

Based on the comments below, I tried the following cases, but also crashed:

mysql> create trigger testa_trig before insert on testa for each row 
       if (NEW.c > 100) begin insert into testb set bc=NEW.c end;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'begin insert into testb set bc=NEW.c end' at line 1
like image 667
Vahid Mirjalili Avatar asked Oct 01 '14 01:10

Vahid Mirjalili


People also ask

Can a trigger include select statement?

The trigger event that initiates the trigger action can be an INSERT, DELETE, UPDATE, or a SELECT statement. The MERGE statement can also be the triggering event for an UPDATE, DELETE, or INSERT trigger.

What is Definer in MySQL trigger?

The DEFINER clause specifies the MySQL account to be used when checking access privileges at trigger activation time. If the DEFINER clause is present, the user value should be a MySQL account specified as ' user_name '@' host_name ' , CURRENT_USER , or CURRENT_USER() .

What is after INSERT trigger?

An AFTER INSERT Trigger means that MySQL will fire this trigger after the INSERT operation is executed.

How can you access the new value for the address inside the trigger in MySQL?

Answer: NEW. qty references the qty on the table that the trigger is set on, not the table that is being updated. CREATE TRIGGER after_sales_insert AFTER INSERT ON sales FOR EACH ROW BEGIN UPDATE products SET qty = qty - NEW.


1 Answers

Couple of things wrong here.

  1. Delimiters. When you make a MySQL procedure or trigger, you need to be very explicit about delimiters so the query interpreter can distinguish between ends of lines in your procedure and the end of your declaration.
  2. Location of the BEGIN statement. It should be directly after FOR EACH ROW.
  3. Use of WHEN instead of IF.
  4. Use of PRINT instead to SIGNAL SQLSTATE '...' SET MESSAGE_TEXT = '...'. This is how you raise exceptions in MySQL 5.5+.

Here is code that should work!

DELIMITER $$

CREATE TRIGGER testa_trig  
BEFORE INSERT ON testa 
FOR EACH ROW BEGIN
    IF (NEW.c > 100) THEN 
        SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning: c > 100!';
    END IF;
END$$

DELIMITER ;
like image 137
DefiniteIntegral Avatar answered Sep 24 '22 14:09

DefiniteIntegral