MariaDB move data to another table with TRIGGER - mysql

i want to move my current data to another table when the update value is "jakarta"
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1
SELECT * FROM detail_mhs WHERE id_mhs = NEW.id_mhs;
DELETE FROM detail_mhs WHERE id_mhs = OLD.id_mhs;
END IF;
but with this trigger its make error like this
1442 - Can't update table 'detail_mhs' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
any solutions?

Please try below trigger and change col1 & col2 with your column's name and change primary key with column name which have primary key and not updating.
DELIMITER $$
CREATE TRIGGER detail_mhs_trig
AFTER UPDATE ON detail_mhs FOR EACH ROW
BEGIN
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1(col1,col2, alamat)
VALUES(old.col1, old.col2, new.alamat);
END IF; END$$
DELIMITER ;

Related

Create a trigger that finds string value and copies ID to insert to new table

I am trying to write an MYSQL statement that will copy the patient_id where they have a value of "Dose: Second" and paste it to my table vaccinatedpatients.
Also, I wanted to implement this within a trigger to automatically update itself after each new record on the table.
Any help with this will be great!
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW BEGIN
SELECT #variable = patient_id from patient where vaccine_Dose = 'Dose: Second';
INSERT INTO vaccinatedpatients VALUES(variable);
END
You can use an IF cLause, to determines if the patient gets the second dose
DELIMITER &&
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW
BEGIN
IF NEW.vaccine_Dose = 'Dose: Second' THEN
INSERT INTO vaccinatedpatients VALUES (NEW.patinet_id);
END IF;
END&&
DELIMITER ;
The DELIMITER may not be needed.

Update Trigger on same table MySQL 5.6

I have a table 'foods' with columns 'featured', 'restaurant_id', 'retaurant_stock'.
How can I update column 'restaurant_stock' using trigger or any other solution available on mySQL version 5.6 after update of column 'featured' on the same table on a condition?
Calculated columns are not available on mySQL version 5.6.
Here's my trigger:
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
UPDATE `foods` SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
UPDATE `foods` SET NEW.restaurant_stock = 0;
END IF;
END
Now when I update column 'featured' I get the following error message:
#1442 - Can't update table 'foods' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for you help!
You can't update the current row that way, you can use set to change the new values
DELIMITER $$
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
SET NEW.restaurant_stock = 0;
END IF;
END$$
DELIMITER ;
CREATE TRIGGER tr_bu_foods
BEFORE UPDATE
ON foods
FOR EACH ROW
-- update restaurant_stock
SET NEW.restaurant_stock = NEW.restaurant_id * (NEW.featured = 1);

Trigger After Insert Update -cant update table

i have this Trigger in my MySql:
CREATE DEFINER=`root`#`localhost` TRIGGER `mydatabase`.`content_AFTER_INSERT` AFTER INSERT ON `content` FOR EACH ROW
BEGIN
UPDATE member
SET member.points = member.points +10, member.postcountT = member.postcountT +1
WHERE member.user_id = NEW.owner;
END
and this error comes if i insert data into the "content" table:
Can't update table 'member' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
how can i resolve this? thanks.

MYSQL TRIGGER AFTER DELET - delete data from multiple tables

I would like to delete data in multiple tables, from a trigger but it seem like my syntax is incorrect.
{ DELIMITER $$
CREATE TRIGGER `delete_store`
AFTER DELETE ON `menufacture`
FOR EACH ROW
BEGIN
DELETE FROM company WHERE OLD.id = menufacture_id ;
DELETE FROM product WHERE OLD.id = product.menufacture_id;
DELETE FROM menufacture WHERE OLD.id = menufacture.parent ;
END $$`
}
#1442- can't update table 'menufacture' in stored function/trigger because it is already used by statement which invoked this stored function /trigger

#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