mySQL trigger not firing - mysql

I have a table with 4 columns [year, v_num, t_num, percentage], I'm trying to populate percentage with v_num/t_num*100. The data is already in the table but the percentage column is NULL. I created an AFTER INSERT trigger to populate the column but it's not working. Does anyone have any suggestions?
TRIGGER
DELIMITER |
CREATE DEFINER = 'XXX' TRIGGER percentAfterUp
AFTER INSERT
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table` SET percentage = v_num/t_num*100;
END|
DELIMITER ;

If you want to set a value in a row, do it with a before insert trigger, not an after insert trigger:
DELIMITER |
CREATE DEFINER = 'XXX' TRIGGER percentAfterUp
BEFORE INSERT ON `table` FOR EACH ROW
BEGIN
SET new.percentage = (new.v_num / new.t_num) * 100;
END|
DELIMITER ;
If you actually want to update all the rows, don't use a trigger, just an update statement:
UPDATE `table`
SET percentage = (v_num/t_num)*100;

Related

Unable to create this MYSQL Trigger on PHPMYADMIN

I am trying to update a column ( called DateModified ) before update happens on the table row.
so here is my trigger:
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW BEGIN
SET new.DateModified = NOW();
END
BUT I GET THIS DAMN ERROR WHICH I JUST CANNOT FIGURE OUT WHY:
Use DELIMITER
DELIMITER //
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW
BEGIN
SET new.DateModified = NOW();
END //
DELIMITER ;

sum trigger on insert Mysql

I have a table called sum_test with id,a,b,c fields
I need a trigger that calculates the sum of a+b on c but Im new to triggers.
How can I do that on Mysql?
CREATE TRIGGER `example` AFTER INSERT ON `sum_test`
FOR EACH ROW BEGIN
UPDATE sum_test
SET c= (a+b)
WHERE id = id
END
If we are wanting to set column c on the row we are inserting, then we can do like this, in a BEFORE insert trigger:
DELIMITER $$
CREATE TRIGGER `sum_test_bi` BEFORE INSERT ON `sum_test`
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b ;
END$$
DELIMITER ;
I don't understand the reference to table suma. What does that table have to do with anything? We note that the condition id = id is going to evaluate to TRUE for every row in the table where id IS NOT NULL.

Insert and update system time using trigger

I am using a trigger for auto increment "slno", "Cust_Id". Now i need to create a insert trigger for inserting system date for the field Created_Date and Submitted_Date. And need a update trigger for only Submitted_date (Need to update the current system time on update).
Now i am using this trigger.
DELIMITER $$
CREATE TRIGGER tg_customer_details_insert
BEFORE INSERT ON customer_details
FOR EACH ROW
BEGIN
INSERT INTO customer_details_seq VALUES (NULL);
SET NEW.Created_Date = NOW();
SET NEW.Submitted_Date = NOW();
SET NEW.Slno = coalesce((select max(Slno) from customer_details), 0) + 1;
SET NEW.Cust_id = CONCAT('CUST', LPAD(LAST_INSERT_ID(), 3, '0'));
END$$
DELIMITER ;
This works fine for insert and i don't know how to put the update trigger for the "submitted date" inside this trigger.
Thanks
Acube.

#1442 - Can't update table 'sale_price' in stored function/trigger because it is already used by statement

I am trying to update the same table before inserting new row in the table.
I want to set the status of all previous rows with the same product_id to 0 and after that insert new row with the status 1....Please help. here is my code written mysql..
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
UPDATE sale_price
SET status=0
WHERE product_id=new.product_id;
END
//
Use a stored procedure instead:
DELIMITER $$
CREATE PROCEDURE procedureName (IN p_product_id)
BEGIN
UPDATE sale_price SET status = 0 WHERE product_id = p_product_id;
INSERT INTO sale_price (product_id) VALUES (p_product_id);
END $$
DELIMITER ;
Then call it with
CALL procedureName(1);
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
SET NEW.status= 0;
END

Can I update the just added row using MySQL triggers

The default initial value of one column in my database is the same as the row's auto-incremented id. I'm trying to use triggers to set it.
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END
But this keeps throwing a syntax error
#1064 - 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 '' at line 5
I've tried all sorts of permutations of this with no luck. Can anyone see what I'm doing wrong?
As zerkms said, you need to change the delimeter. But since you only use 1 line of code, you don't need the BEGIN and END. And that way, you don't need to change the delimiter either
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
Since you are getting an error you cannot update the row, I suggest the following:
Do NOT perform the update query at all. On default the order value = the ID value. So when the order value changes, you can update it properly.
If you are requesting the data with php, do something like this:
$order = $row['order'];
if ($order == '')
$order = $row['id'];
After you need it updating, you've got the correct value.
I don't think you can do that. An AFTER INSERT trigger cannot modify the same table, neither by issuing an UPDATE nor by something like this:
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
SET NEW.`order` = NEW.id ;
which results in this error:
> Error Code: 1362. Updating of NEW row is not allowed in after trigger
You can't either use a BEFORE INSERT trigger because then the NEW.id is not known (if you modify the above, the order column will get 0 value after the Insert.
What you can do, is use a transaction:
START TRANSACTION ;
INSERT INTO clusters (id)
VALUES (NULL);
UPDATE clusters
SET `order` = id
WHERE id = LAST_INSERT_ID();
COMMIT ;
You get the error because mysql treats ; in line 5 as the end of your trigger declaration, which obviously leads to the syntax error.
So you need to redefine delimiter before you specify the trigger body:
delimiter |
CREATE TRIGGER `default_order_value`
AFTER INSERT ON `clusters`
FOR EACH ROW
BEGIN
UPDATE `clusters` SET `order` = NEW.id WHERE `id` = NEW.id;
END;
|
delimiter ;
You can create just BEFORE INSERT TRIGGER, it's works like this:
CREATE TRIGGER `default_order_value`
BeFORE INSERT ON `clusters`
FOR EACH ROW
BEGIN
SET NEW.`order` = NEW.id ;
END
same as below we are using
DELIMITER $$
USE `e_store`$$
DROP TRIGGER /*!50032 IF EXISTS */ `Test`$$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `Test` BEFORE INSERT ON `categories`
FOR EACH ROW
BEGIN
DECLARE vtype VARCHAR(250) DEFAULT NULL;
SET vtype = NEW.name;
IF (NEW.MDNAME IS NULL)
THEN
-- SET NEW.MDNAME = 'NA';
SET NEW.MDNAME=MD5(NEW.name);
END IF;
END;
$$
DELIMITER ;
This worked for me:
CREATE TRIGGER `update_table_2`
AFTER UPDATE ON `table_1`
FOR EACH ROW
UPDATE table2
JOIN table_1
SET table_2.the_column = NEW.the_column
WHERE table_2.auto_increment_field = OLD.auto_increment_field