MYSQL Trigger to update different table based on inserted value - mysql

I need to update oc_product_attribute with a value obtained from an inserted row in vehicles. How can I get this specific value? (xxxCOLUMN_VALUExxx)
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute SET oc_product_attribute.vehicle_name =
(SELECT CONCAT(vehicles.make_name, ' ', vehicles.model_name, ' ',
vehicles.chassis) FROM vehicles WHERE vehicles.id = xxxCOLUMN_VALUExxx)
WHERE oc_product_attribute.id = xxxCOLUMN_VALUExxx
END
Even simplified below: I still get an error in MYSQL:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis);
END;
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 7
And Simplified further:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name = 'test' WHERE 1
END;
I still get the same error - Could it be MYSQL doesn't have triggers enabled?

I guess you need to update oc_product_attribute like this:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis)
WHERE oc_product_attribute.id = NEW.id;
END
Although I am no sure if oc_product_attribute.id is holding the vehicles.id. However, the important poart is that in an INSERT AFTER TRIGGER you can access the values of the inserted row by NEW.columnName.
N.B. In update triggers you also can access the value before the change with OLD.columnName

Related

Trying to resolve a syntax error for MYSQL update trigger

I am trying to create a trigger to update the Project table whenever the Assign table is updated, yet I keep receiving this error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
Does anyone have any idea of why I am getting this syntax error? Please note that I am a beginner at this so my SQL statements may be entirely wrong. Also, I know for sure that my query being ran in my IF statement works because I've ran it on its own without being inside a trigger.
delimiter $$
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
#RUN THIS QUERY TO UPDATE MY TABLE
UPDATE Project p JOIN
(SELECT Project.projNo, COUNT(DISTINCT empid) as numEmployeeAssigned
FROM (Project LEFT JOIN Assign ON Project.projNo=Assign.projNo)
GROUP BY projNo
) as tb
ON p.projNo = tb.projNo
SET p.numEmployeeAssigned = tb.numEmployeeAssigned
#END QUERY
END IF
END$$
delimiter;
You can just increment or decrement the count:
UPDATE Project p
SET p.numEmployeeAssigned = p.numEmployeeAssigned +
(case when p.projNo = new.ProjNo then 1 else -1 end)
WHERE p.projNo IN (old.ProjNo, new.ProjNo) AND
old.ProjNo <> new.ProjNo;
Obviously, your code is missing semi-colons after the UPDATE statement and after the END IF.
However, let me suggest that the UPDATE statement could most likely be simplified. As I understand your code, you want to update the count of employees in Project whenever an employee changes its assignment. If so, there is no need to actually query assign. You can just use conditional logic like this:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo);
END IF;
END$$
You could even remove the IF statement and put that condition in the query directly:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo) AND OLD.projNo <> NEW.projNo
END$$

How to update table with SQL trigger? [duplicate]

So I have a table that holds a list of cuisines and a total of how many restaurants there are with that cuisine. (e.g. Italian | 7)
I'm trying to set-up a trigger in phpMyAdmin that will increment the total every time a new restaurant is added to the database. Here is what I have so far:
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant
BEGIN
UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
END;
But I keep getting an error message and it's telling me that it has to do with my Syntax. Where am I going wrong? I followed the example I saw pretty well.
Here is the error message:
#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 'BEGIN UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE' at line 2
do you have to have for each row
CREATE TRIGGER UpdateStats
AFTER INSERT ON Restaurant
FOR EACH ROW
UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
You need to use FOR EACH ROW and get rid of the BEGIN...END like this:
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant
FOR EACH ROW
UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
BEGIN...END is only used for multiple queries in the same trigger and requires redefinition of the termination character. So your first ; is terminating the statement and leaving the BEGIN open without an END.
try with "for each row":
CREATE TRIGGER UpdateStats AFTER INSERT ON Restaurant
FOR EACH ROW
BEGIN
UPDATE RestaurantStats SET TotalRestaurants = TotalRestaurants + 1 WHERE Cusine = NEW.cusine;
END;
Haven't tried it myself so not sure if it works...

MySQL TRIGGER ON INSERT issue

i'm going mad with a MySQL trigger. MySQL says there's an error in the code but i can't figure out what's the problem.
this is the TRIGGER
CREATE TRIGGER UPDATE_COUNTRY
AFTER INSERT
ON `dog`FOR EACH ROW
BEGIN
IF (NEW.export=1)
THEN
IF (NEW.year > (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origin))
THEN
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
ELSEIF (NEW.year = (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origine)
AND NEW.month > (SELECT MAX(`updated_month`) FROM `dog` WHERE `code`=NEW.origine AND `updated_year`=NEW.year))
THEN UPDATE `dog` SET `updated_month`=NEW.month WHERE `code`=NEW.origin;
ELSE
RETURN NEW;
END IF;
END IF;
RETURN NEW;
END;
My SQL says
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 13
Thank you all!
Your trigger is:
AFTER INSERT ON `dog`FOR EACH ROW
And your line 13 is
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
This kinda suck, but you cannot update/insert/delete with a trigger on the same table that you are currently inserting/updating/deleting data because the table is locked.
This is a limitation from MySQL (yeah and not all SGDB actually have this limitation...(
You will need to work around a different way if you want to update your "dog" table. You could use a store procedure to do both the insert and the update.

What is wrong with the following if statement inside MySql trigger

I'm building a trigger that updates a table when another table get updated. But, for some reason MySql doesn't like the if statement inside it.
The scenario is, there is a table named Group that has level label configuration that are stored in level1, level2, and level3 columns. The other table, Membership, stores member configuration with their level title.
For example if there is a member has level1 title, novice, and there's an update in Group_configuration level1 from 'novice' into 'newbie', trigger will update the member title into 'newbie'. This also applies to level2 and level3.
So my trigger code looks like this:
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
but I keep getting this 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
Any ideas where do I miss?
regards
please check that you set the delimiter to something else than ; before actually creating the trigger:
DELIMITER |
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
DELIMITER ;
Maybe this helps?
On line 9 add a semicolon. I believe your entire conditional is confused because of this.

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL 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 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;