MYSql after update trigger not updating table - mysql

I have the following trigger:
CREATE DEFINER = CURRENT_USER
TRIGGER `radia`.`orderstable_AFTER_UPDATE`
UPDATE ON `orderstable`
FOR EACH ROW begin
SET #VATCharged = Amount * (VAT*0.01);
But the VATCharged column does not update after there is a change to the amount or VAT.
The table has the following columns; Order Number(INT), AccNo(VARCHAR), Invoice Number(VARCHAR), Description(VARCHAR), Amount(INT), VAT(INT) and VATCharged(INT).
What is the solution to this problem?

You need to specify old or new against the column to get either old value before update or new value after update
set #VATCharged := new.Amount * (new.VAT*0.01);
EDIT :
If you want to update the VATCharged column in the table, then the trigger needs to be before update something as
delimiter //
create trigger `radia`.`orderstable_AFTER_UPDATE`
before update on `orderstable`
for each row
begin
set new.VATCharged = new.Amount * (new.VAT*0.01);
end ; //
delimiter ;

Related

Why this SQL trigger isn't saving any data?

So I have this trigger:
delimiter $$
CREATE TRIGGER check_liters
AFTER INSERT ON reports_data_sheet_area_brut
FOR EACH ROW BEGIN
IF (NEW.fuel > 10) THEN
UPDATE fuel_check
SET fuel_value = NEW.fuel
Where id = NEW.vehicule_id;
ELSEIF (NEW.fuel > 20) THEN
UPDATE fuel_check
SET fuel_value2 = NEW.fuel
Where id = NEW.vehicule_id;
END IF;
END$$
It can be added properly on triggers but it doesnt add data in fuel_check table when fuel>10 or 20.
This is reports_data_sheet_area_brut :
http://i.prntscr.com/a1f7af8c28c447e98eb0cf111e544596.png
This is fuel_check table(for the trigger):
http://i.prntscr.com/75f1467563d14febbdfd8b749a11013d.png
When I will try to add a value:
INSERT INTO reports_data_sheet_area_brut (vehicule_Id,area_Id,area,status_Id,status,start_time,end_time,duration,start_fuel_level_liters,end_fuel_level_liters,fuel,distance,consum_h )
VALUES
(43,432,"ceva",2,"Motor Oprit","2016-06-29 09:07:04","2016-06-30 10:07:06",30937,674,674,15,0,0);
After this SQL it only add column in reports_data_sheet_area_brut but not in fuel_check table from trigger.

How to use alias in mysql triggers?

I'm am trying to create this mysql trigger:
DELIMITER //
CREATE TRIGGER checkcollision2 AFTER UPDATE ON projectiles p1
FOR EACH ROW
BEGIN
IF (SELECT count(*) FROM players p WHERE p.wolf=1 AND NEW.x>=p.x AND NEW.x<=p.x+25 AND NEW.y>=p.y AND NEW.y<=p.y+25) > 0 THEN
DELETE FROM projectiles p2 WHERE p1.id=p2.id;
END IF;
END;//
DELIMITER ;
But this isn't working, as its getting a syntax error. I'm not sure if its because I'm using an alias names for the tables. I am doing that because, if the if statement is true, then I want to delete the current updated row that the trigger started on. I think I could do that by doing a delete from the same table, where the id is the same, but if I don't use alias, then all rows from this table would be deleted.
Does anyone see whats wrong here?
Alias names on identifies at trigger signature level is not supported and hence is incorrect.
Instead you can define alias within the trigger body if required.
And as you want to perform a delete operation based on a condition, you can make use of NEW to compare with a specific row-column value. Current where clause WHERE p1.id=p2.id will delete ALL rows from the table, which is unsafe.
Change your trigger definition as below:
DELIMITER //
DROP TRIGGER IF EXISTS checkcollision2 //
CREATE TRIGGER checkcollision2 AFTER UPDATE ON projectiles
FOR EACH ROW
BEGIN
DELCARE match_count INT DEFAULT 0;
SELECT count(*) INTO match_count
FROM players p
WHERE p.wolf = 1
AND NEW.x BETWEEN p.x AND ( p.x + 25 )
AND NEW.y BETWEEN p.y AND ( p.y + 25 )
IF match_count > 0 THEN
DELETE FROM projectiles WHERE id = NEW.id;
END IF;
END;
//
DELIMITER ;

Error while creating trigger

delimiter #
create trigger update_recharge_due_onrenewal after insert on recharge
for each row set
begin
update due_amount SET due_amount.amount=due_amount.amount-(select rate from
internet_package where recharge.package_id = internet_package.package_id)
where
due_advance.user_id = recharge.user_id;
end;#
Why is this showing error - unknown system variable 'begin'
Change for each row set by for each row.

mysql triggers have some issue in updating my table

hi
i have created a trigger which i want to update a table field when insertion in another field occurs.but i don,t know how should it know that its updating the same user row who is inserting here is some code and database senerio
--> admin_reg_abr
'id course_limit username
10 0 ahmad '
note: id is primry key
--> abroad_course
'id uni_id username
1 10 ahmad'
note: uni_id is foriegn key or same as id in admin_reg_abr.
i write the following code for trigger:
DROP TRIGGER IF EXISTS
ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER INSERT ON ahmad.abroad_course
FOR EACH ROW BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit =
admin_reg_abr.course_limit + 1 WHERE
admin_reg_abr.id = new.id; END //
i want to update the same id how insert in the table but its not working.
Didn't You mean:
DROP TRIGGER IF EXISTS ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER INSERT ON ahmad.abroad_course FOR EACH ROW
BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1
WHERE admin_reg_abr.id = new.uni_id;
END //
Note that in Your code NEW.id points nowhere in admin_reg_abr table
One more thing. You are talking about firing trigger on UPDATE, so, You would probably need another trigger:
DROP TRIGGER IF EXISTS ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER UPDATE ON ahmad.abroad_course FOR EACH ROW
BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1
WHERE admin_reg_abr.id = new.uni_id;
END //

Mysql auto fill field based on value of two other fields?

I know its possible to autoincrement values, but i was wondering if its possible to fill a field based on the value of two other fields. I have a table with the fields:
CREATE TABLE pligg_links (
...
link_votes INT,
link_reports INT,
link_votes_total INT,
...
);
Field link_votes_total should hold the value of field link_votes subtracted from link_reports. So basically, this is the math equation: link_votes_total = link_votes - link_reports. Is this possible without having to use php to do it before data is stored?
Yes, this can be done by creating a trigger for BEFORE INSERT and another one for BEFORE UPDATE:
DELIMITER //
CREATE TRIGGER trig_mytable BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
SET NEW.link_votes_total = NEW.link_votes - NEW.link_reports;
END
//
CREATE TRIGGER trig_mytable BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
SET NEW.link_votes_total = NEW.link_votes - NEW.link_reports;
END
//
DELIMITER ;
Further Reading:
MySQL 5.1 Reference Manual: CREATE TRIGGER Syntax
See:http://dev.mysql.com/doc/refman/5.1/en/triggers.html
DELIMITER //
CREATE TRIGGER bir_links
BEFORE INSERT ON links
FOR EACH ROW
BEGIN
SET link_votes_total = NEW.link_votes - NEW.link_reports;
END;
//
CREATE TRIGGER bur_links
BEFORE UPDATE ON links
FOR EACH ROW
BEGIN
SET link_votes_total = NEW.link_votes - NEW.link_reports;
END;
//
DELIMITER ;