Update trigger MySQL giving syntax error - mysql

I want to create trigger to make update on table when inserting rows to other table, but I get a syntax error for this:
CREATE TRIGGER quantity AFTER INSERT ON sale_items
FOR EACH ROW
BEGIN
update products set quantity = quantity -1 where id =(
SELECT product_id
FROM sale_items
ORDER BY id desc
LIMIT 1)
END;
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 'END' at line 7

This seems like a silly trigger. Why are you fetching the last update id using a subquery? It should be available through new:
DELIMITER //
CREATE TRIGGER quantity AFTER INSERT ON sale_items
FOR EACH ROW
BEGIN
update products
set quantity = quantity - 1
where id = new.product_id
END//
DELIMITER ;

Use proper Delimiter in your trigger , the correct code is:
DELIMITER //
CREATE TRIGGER quantity AFTER INSERT ON sale_items
FOR EACH ROW
BEGIN
update products set quantity = quantity - 1
where id = new.product_id ;
END//
DELIMITER ;

Related

MySQL trigger if-else insert update

I'm trying to make this code work but to no avail..
DELIMITER $$
CREATE
TRIGGER `update_tbl1` AFTER UPDATE
ON `tbl1`
FOR EACH ROW BEGIN
IF (SELECT count(*) FROM tbl1 WHERE stn=NEW.stn) = 1
THEN
UPDATE tbl2 SET date_posted=NEW.date_posted WHERE stn=NEW.stn;
ELSE
INSERT INTO tbl2 (stn) VALUES (NEW.stn);
END IF
END$$
DELIMITER ;
I have two tables, and I want a trigger that will update tbl2 if the tbl1 is updated, only if the data already exists on the tbl2, otherwise, insert it. My code seems feasible and the error seems to be syntax-related but I can't find where.
EDIT:
Here is the 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 'INTO tbl2 (stn) VALUES (NEW.stn); END IF END' at line 10
About syntax error, I think it's just what you forgot semicolon ; after END IF, try following:
DELIMITER $$
CREATE
TRIGGER `update_tbl1` AFTER UPDATE
ON `tbl1`
FOR EACH ROW BEGIN
IF (SELECT count(*) FROM tbl1 WHERE stn=NEW.stn) = 1
THEN
UPDATE tbl2 SET date_posted=NEW.date_posted WHERE stn=NEW.stn;
ELSE
INSERT INTO tbl2 (stn) VALUES (NEW.stn);
END IF;
END$$
DELIMITER ;
I'm not sure what the syntax error is, but the logic you want is:
INSERT INTO tbl2 (stn)
VALUES (NEW.stn)
ON DUPLICATE KEY UPDATE SET date_posted = NEW.date_posted;
For this to work, you need a unique index on tbl2(stn):
CREATE UNIQUE INDEX unq_tbl2_stn ON tbl2(stn);
Note: This doesn't fix your particular syntax error. This addresses a logical error in the code.

trying to create a trigger that updates the quantity of one table from another table

I have two tables for products that are being sold. One is the table for the actual products and the other is for the ordered item.
The ordered item table records the itemId and quantity sold.
I want to create a trigger that takes the data from the ordered item table and update the product table quantity which the column is called quantityInStock.
this is the trigger that I wrote but I keep getting an error:
delimiter $$
create trigger stockUpdate
after insert on orderItem
for each row
begin
update item(quantityInStock)
set quantityInStock = quantityInStock - orderItem.quantity
where itemId = orderItem.itemId;
end$$
my error message:
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 '(quantityInStock)
set quantityInStock = quantityInStock - orderItem.quantity w' at line 5
Check your update syntax.
update table_name
set column_name = ...
where column_name = ...;
You can reference the previous/new row values with old.column_name, new.column_name respectively.
Some things to consider:
How to prevent quantity from going negative

Error writing trigger statement

I would like to limit the size of a table to X rows (I'll use 5 for example). When the limit is reached, I want to copy the oldest row to another table, then delete it. I currently have:
CREATE TRIGGER LimitRows BEFORE INSERT ON MyTable
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM MyTable) >= 5 THEN
INSERT INTO HistoryTable
SELECT *
FROM MyTable A
WHERE vhID = A.min(vhID);
DELETE FROM MyTable
WHERE vhID = min(vhID);
END IF;
END;
Currently, I get the error:
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 8
How do I write this trigger correctly? Also, how can I modify to cut the table down to 5 rows if it starts out at something like 100 rows?
You need to change the delimiter first
delimiter |
CREATE TRIGGER LimitRows BEFORE INSERT ON MyTable
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM MyTable) >= 5 THEN
INSERT INTO HistoryTable
SELECT *
FROM MyTable A
WHERE vhID = A.min(vhID);
DELETE FROM MyTable
WHERE vhID = min(vhID);
END IF;
END
|
delimiter ;
Otherwise the trigger definition would end at the first ; which would make it incomplete.

Trigger to update table based on another table update in mysql

I have two tables
1. Tag
2. Triger_testing
Tag desc
id int, is_active (tinyint)
Trigger_Testing Desc
tag_id (int), is_active(tinyint)
I want to create a trigger on tag table update which update trigger_testing table. So if tag.is_active is set to 0 the trigger must fire and update trigger_testing table and set trigger_testing.is_active=0 where trigger_testing.tag_id=tag.id.
I tried to create a trigger in MYSQL but getting syntax exception. Can someone help me out in resolving that issue.
Here is the code : -
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id
END IF
END$$
DELIMITER;
The error I am getting is :
Error Code: 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 'END IF
END$$
DELIMITER' at line 6
CREATE TRIGGER update_trigger_testing AFTER UPDATE ON tag
FOR EACH ROW
BEGIN
IF NEW.is_active=0 THEN
UPDATE trigger_testing SET is_Active=0 WHERE tag_id=NEW.id;
END IF;
END;

Create Query For MYSQL Trigger

First i have 2 tables named "item" and "buy_item"
//the "stock" column is in item table and the "qty" column is ini buy_item table
then I have SQL SERVER query to create a trigger like this
CREATE TRIGGER trigger1
ON dbo.buy_item
FOR UPDATE
AS begin
UPDATE item SET stock = stock - qty FROM deleted WHERE item.id = deleted.id
UPDATE item SET stock = stock + qty FROM inserted WHERE item.id = deleted.id
end
I need help to create the same function of this query in MYSQL query
and i already do this
CREATE TRIGGER trigger1
BEFORE UPDATE ON buy_item
FOR EACH ROW
BEGIN
UPDATE item SET stock = stock - buy_item.qty WHERE item.id=buy_item.id
UPDATE item SET stock = stock + NEW.qty WHERE item.id=buy_item.id
END
but this isn't work at all, it says the syntax is wrong
maybe anyone can help about this
Thanks before
Assuming that you can't change item id in buy_item your trigger in MySql should look like this
CREATE TRIGGER trigger1
AFTER UPDATE ON buy_item
FOR EACH ROW
UPDATE item
SET stock = stock + NEW.qty - OLD.qty
WHERE id = NEW.id;
Here is SQLFiddle demo
A trigger body in mysql requires a number of SQL commands separated by a semi-colon (;). To create the full trigger code one must define its own delimiter to something else — such as $$.
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE UPDATE ON buy_item
FOR EACH ROW BEGIN
UPDATE item SET stock = stock - buy_item.qty WHERE item.id=buy_item.id;
UPDATE item SET stock = stock + NEW.qty WHERE item.id=buy_item.id;
END$$
This web site as helped me in the past for syntax and other relevant issues to mysql and triggers
http://www.sitepoint.com/how-to-create-mysql-triggers/
this is the error message after i write the syntax
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE UPDATE ON buy_item
FOR EACH ROW BEGIN
UPDATE item SET stock = stock - buy_item.qty WHERE item.id=buy_item.id;
UPDATE item SET stock = stock + NEW.qty WHERE item.id=buy_item.id;
END$$
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for right syntax to use near 'DELIMITER $$
CREATE TRIGGER Trigger1
BEFORE UPDATE ON buy_item
FOR EACH ROW' at line 1'"
My MySQL server version is 5.5.13