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 //
Related
I want to add a trigger if name has update with "NEW_" string. Below is my current trigger but its working without checking the updated value contain "NEW_" . please advice
if user updates name from "Ann" to "NEW_Ann" trigger should work.
if user updates name from "Ann" to "Ann111" trigger shouldn't work
Code:
$this->execute("ALTER TABLE `users`
ADD COLUMN `date_` TIMESTAMP NULL DEFAULT NULL");
$this->execute("CREATE TRIGGER `update_user_deleted_date` BEFORE UPDATE ON `users` FOR EACH ROW BEGIN
IF (NEW.name!= OLD.name) THEN
SET NEW.date_deleted = NOW();
END IF;
END ;");
Use this:
CREATE TRIGGER update_user_deleted_date BEFORE UPDATE ON users
FOR EACH ROW BEGIN
IF (LOCATE('NEW_', NEW.name) != 0) THEN
SET NEW.date_deleted = NOW();
END IF;
END;
LOCATE('NEW_', NEW.name) != 0 means that the location of string "NEW_" is found
How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.
I have 2 tables:
table 1 = SAMPLE_TABLE
table 2 = RESULT_TABLE (with proposed trigger)
I would like to use a trigger in RESULT table that, when a new record is inserted into into it, will update a field in SAMPLE table called, 'status' = "complete". The field 'status' to be updated in SAMPLE is related to RESULT by:
table 1 = SAMPLE_TABLE ('client_sampleID')
=
table 2 = RESULT_TABLE ('sampleID')
This is the proposed trigger
CREATE DEFINER = `user`#`%` TRIGGER `database`.`RESULT_TABLE_BEFORE_INSERT`
AFTER INSERT ON `RESULT_TABLE` FOR EACH ROW
BEGIN
UPDATE SAMPLE_TABLE
SET status = 'complete'
WHERE SAMPLE_TABLE.client_sampleID = RESULT_TABLE.sampleID;
END
My questions:
is this above trigger OK?
there are 100+ 'client_sampleID' (all same, entered as a batch) per 'sampleID'. Is there a more efficient way of setting the 'status' so that it happens only after encountering the first instance?
You are very close. You just need to use new in the trigger:
CREATE DEFINER = `user`#`%` TRIGGER `database`.`RESULT_TABLE_BEFORE_INSERT`
AFTER INSERT ON `RESULT_TABLE` FOR EACH ROW
BEGIN
UPDATE SAMPLE_TABLE st
SET status = 'complete'
WHERE st.client_sampleID = new.sampleID;
END
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 ;
How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.