[Solved] Display a message when trigger fires in phpMyAdmin
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Display a message when trigger fires in phpMyAdmin

I have a table where stock quantity is recorded. I set trigger to warn the user when stock goes low. When testing it, the trigger fires, displays the message, but the stock is not updated. What do I do wrong? Is there another way to set the trigger (not using SIGNAL SQLSTATE)? I use phpMyAdmin 4.2.7.1 in uWamp. Thank you for suggestions.

Code for the trigger:

DELIMITER $$
CREATE TRIGGER low_stock_message  
AFTER update ON stock_quantity 
FOR EACH ROW BEGIN
    IF (NEW.Regular_Menu < 50) THEN 
        SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning, Regular Menu has a low stock!';
    END IF;
    IF (NEW.Savers < 50) THEN 
        SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning, Savers Menu has a low stock';
    END IF;
    IF (NEW.Breakfast < 50) THEN 
        SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Warning,  Breakfast Menu has a low stock!';
    END IF;
END$$
DELIMITER ;

Test query:

UPDATE stock_quantity
SET Regular_Menu=48
WHERE Outlet_ID=1;

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `stock_quantity` ADD INDEX `stock_quantity_idx_outlet_id` (`Outlet_ID`);
The optimized query:
SELECT
        stock_quantity.Regular_Menu 
    FROM
        stock_quantity 
    WHERE
        stock_quantity.Outlet_ID = 1

Related Articles



* original question posted on StackOverflow here.