MYSQL Trigger Query - mysql

I am attempting to create a trigger to update quantities between 2 separate databases. This query runs successfully, but when I show triggers in mysql, it brings up an empty set. Any help would be much appreciated.
delimiter $$ CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code
END $$
delimiter ;

That's because your code has two synax errors:
delimiter $$ --delimiter statements need to be on separate lines
CREATE TRIGGER `quantity_to_clb` AFTER UPDATE ON product
FOR EACH ROW BEGIN
UPDATE cl_boutique.product AS clb
LEFT JOIN cherrylane.product AS cl
ON clb.model = cl.code SET clb.quantity = cl.available
WHERE clb.model = cl.code; -- ; was needed here
END $$
delimiter ;

Related

MySql Looping Each Row with calculation

I want to update each row in MySQL table using stored procedure but the problem is my code below is not working:
DELIMITER //
CREATE PROCEDURE UPDATESCORE()
BEGIN
LOOP
UPDATE scoretable SET `final_average` = (`term1_result` + `term2_result`) / 2;
END LOOP
END
DELIMITER ;
Just Remove the Loop From your procedure as Update work for all row if you don't have Where clause. Also Update query work by default in safe update mode so you need to set that to false.
SET SQL_SAFE_UPDATES = 0;
and then there is no error when you run that procedure also.
Your Update query would:
DELIMITER //
CREATE PROCEDURE UPDATESCORE()
BEGIN
UPDATE scoretable SET `final_average` = (`term1_result` + `term2_result`);
END //
DELIMITER ;
Remove loop, update works on each row if there is no where clause.

How can I use MySQL Trigger to update quantity field in 'items' table with quantity - quantity_delivered

How can I use MySQL Trigger to update quantity field in items table with quantity-quantity_delivered when quantity_delivered in the requests table is updated ?
Here Is My Query, I have tested but I see its not getting updated, can any one help me please?
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items .quantity = items.quantity - requests.quantity_delivered
WHERE items .item_id = requests.item_id;
END;
$$
DELIMITER ;
I have resolved by myself like this and its working for me thanks All!
DELIMITER $$
CREATE TRIGGER `quantityTrigger` AFTER UPDATE ON `requests` FOR EACH ROW BEGIN
UPDATE items i
SET i.quantity=i.quantity-NEW.quantity_delivered
WHERE i.item_id=NEW.item_id;
END
$$
DELIMITER ;
You should be using the new. values in your trigger like this
drop trigger if exists items_update;
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items.quantity = items.quantity - new.quantity_delivered
where items.item_id = new.item_id;
END;
$$
DELIMITER ;
As an aside should this be an after insert trigger?

Why does it say this trigger contains sql syntax errors?

i have written a trigger in mysqlworkbench that should update the product stock after a new line had been added to the order table(my code is in dutch so thats why im explaining this) But it doesn't work, when i try to apply it mysqlworkbench says its sql code contains errors.
Here's the trigger:
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
if Product.productnr = NEW.productnr
then
Update Product
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where productnr = NEW.productnr;
end if;
END
What is Product in the if statement? That is, no doubt, generating a syntax error, because it is unrecognized.
I think you intend for the body to be simply:
Update Product p.
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where p.productnr = NEW.productnr;
The if irrelevant.
I would expect the full version to look something like:
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT`
AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
UPDATE Product p
SET Hoeveelheid = (Hoeveelheid - NEW.aantal)
WHERE p.productnr = NEW.productnr;
END $$
DELIMITER ;
You have delimiter trouble. You need
DELIMITER $$
CREATE ..... TRIGGER ...
BEGIN
END$$
DELIMITER ;

MySQL Update Statement does not work in Stored Procedure

I have a simple sql update statement for MySQL. When I run it alone, it works fine.
UPDATE
temp_student
INNER JOIN student
ON temp_student.card = student.card
SET studentid = student.id;
But when I put it in a stored procedure, none of the rows in the table is updated. Anyone can provide a clue? Thanks.
DELIMITER $$
USE `eceintern2`$$
DROP PROCEDURE IF EXISTS `copy_from_temp_student`$$
CREATE DEFINER = `root` #`localhost` PROCEDURE `copy_from_temp_student` ()
BEGIN
UPDATE
temp_student
INNER JOIN student
ON temp_student.card = student.card
SET studentid = student.id;
END $$
DELIMITER ;
The syntax seems to be no problem.
DELIMITER $$
CREATE PROCEDURE `copy_from_temp_student`()
BEGIN
UPDATE `temp_student`
INNER JOIN `student` ON `temp_student`.`card` = `student`.`card`
SET `temp_student`.`studentid` = `student`.`id`;
END */$$
DELIMITER ;
SQL Fiddle demo

How can i create a trigger to verify if a record exists on another table?

I am using version 5.0 of mysql.
I'm trying to create a trigger to check if one entry(name of Food) exists in the other table.
I´ve done this:
delimiter //
CREATE TRIGGER verifyExists BEFORE INSERT ON Sold
FOR EACH ROW
BEGIN
IF NEW.nameF not in (
select A.nameF
From Available D
where (NEW.nameF = A.nameF and NEW.nameR = A.nameR)
)
END IF;
END;
//
delimiter ;
this doesen't work, why?
You have a couple of errors:
delimiter //
CREATE TRIGGER verifyExists BEFORE INSERT ON Sold
FOR EACH ROW
BEGIN
IF NEW.nameF not in (
select A.nameF
From Available A -- CHANGED THE ALIAS TO A
where (NEW.nameF = A.nameF and NEW.nameR = A.nameR)
) THEN -- MISSING THEN
CALL `Insert not allowed`;
END IF;
END;
//
delimiter ;
sqlfiddle demo
If you could use SIGNAL, it is the best way, but since it was only introduced in mysql 5.5, you will have to do it by other route. One way is to call a non existant function, like showed above. From this answer