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
Related
I am trying to create triggers for my Oracle database and I always get compiled with errors when trying to add it.
The statement is as follows
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking SET open_amount = open_amount - NEW.amount WHERE Booking.booking_id = NEW.booking_id;
END;
and I get the following error
2/5 PL/SQL: SQL Statement ignored
2/90 PL/SQL: ORA-00904: "NEW"."BOOKING_ID": invalid identifier
The trigger should subtract the amount in the newly created payment row from the open_amount field in the booking row and store the new value.
I managed to get it to work in mysql with the following statement
DELIMITER $$
CREATE TRIGGER `update_open_amount` AFTER INSERT ON `Payment`
FOR EACH ROW
BEGIN
UPDATE `Booking` SET `open_amount` = `open_amount` - NEW.`amount` WHERE `Booking`.`booking_id` = NEW.`booking_id`;
END;
$$
DELIMITER ;
but when I try to get it to work in oracle I get stuck.
How can I get the trigger to work because this is all that is still missing for migrating the database from mysql to oracle.
How can I fix this?
Thanks in advance
Please check syntax for referring new and old values. It should have preceeding colon (:)
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking
SET open_amount = open_amount - :NEW.amount
WHERE Booking.booking_id = :NEW.booking_id;
END;
I am new to mysql and am trying to create a trigger. I want to take a newly inserted value from one table and want to update another table by subtracting this values from the value in that table.
CREATE TRIGGER inventory_updated
AFTER INSERT ON ORDERdetails
for EACH ROW
BEGIN
UPDATE product_trial
SET Quantity = Quantity-NEW.quantity
where productid = new.ProductID;
END
This gives SQL syntax error near line 6 (Update query) error 1064.
Kindly help.
In your example, you don't need BEGIN-END because there is a single query executed in the for loop. If you remove them if solves the syntax error.
If you need to keep the BEGIN-END because your real life case is more complex, then you need to define a delimiter, as
DELIMITER \\
CREATE TRIGGER inventory_updated AFTER INSERT ON ORDERdetails for EACH ROW
BEGIN
UPDATE product_trial SET Quantity = Quantity-NEW.quantity where productid = new.ProductID;
END \\
I'm creating a trigger from the table ItemBorrower that is meant to update a field in the table Item. When ItemBorrower gets a new entry inserted, the currentDate field in the Item table should be updated to today's date. Here is my trigger:
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW UPDATE Item
BEGIN
SET Item.checkoutDate = CURDATE()
WHERE NEW.itemID = Item.itemID
END;
And there error I'm getting is about the last line:
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 8
I've played around with the delimeter (removing it, adding a ";" after Item.itemID), but to no apparent avail. Could be that I just did that totally wrong, too.
Any help appreciated!
Thanks,
Jona
I am not 100% sure what are you trying to accomplish from the trigger. I assumed that you have a table called item and you want to update the checkoutDate column with current date where the item.itemID = the last insert id in ItemBorrower.
Try this code
delimiter //
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW
BEGIN
UPDATE Item
SET checkoutDate = CURDATE()
WHERE itemID = NEW.itemID;
END; //
delimiter ;
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 ;
I have 2 tables:
ORDERS - Stores all the information from my shopping cart
ITEMS - Stores id | scode | product | price | saleprice | inventory
I have come up with the trigger below, when an order is inserted into the database in 'orders' I want it to update the 'inventory' quantity by -1 in the 'items table'.
CREATE TRIGGER `order_updater` AFTER INSERT ON `orders`
FOR EACH ROW UPDATE items
SET inventory = inventory - 1
WHERE id = new.id
END;
When I try to add it in triggers in phpMyAdmin I keep getting this message:
One or more errors have occured while processing your request:
The following query has failed: "CREATE TRIGGER orer_update BEFORE INSERT ON items FOR EACH ROW CREATE TRIGGER order_updater AFTER INSERT ON orders FOR EACH ROW UPDATE items SET inventory = inventory - 1 WHERE id = new.id END;"
MySQL said: #1303 - Can't create a TRIGGER from within another stored routine.
Can this be done with this trigger?
Will the trigger update a particular item or all items?
Can the trigger be added to the products .htm page or does it need to be in phpMyAdmin?
I'm not sure if the the code is quite right as I'm new to this, any pointers would be appreciated.
*UPDATE:
phpMyAdmin : I copied this from page it says: Version information: 4.0.8, latest stable version: 4.0.9*
Try something like:
DELIMITER $$
CREATE TRIGGER `order_updater` AFTER INSERT ON `orders`
FOR EACH ROW
BEGIN
UPDATE `items`
SET `inventory` = `inventory` - 1
WHERE `id` = new.`id`;
END$$
DELIMITER ;
UPDATE
What version of phpMyAdmin are you using? The following example, using version phpMyAdmin 4.1.0-beta2, works without problem.