Would like to create a trigger in mysql need to
update t2.orderstatus as 'inactive' on update of t2.inactivedate and
update t2.orderstatus as 'active' on update of t2.activedate. Tried to adapt below one. but failed
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE TRIGGER trigger_on_stockhistory_update
AFTER update ON stockhistory.inactivedate
FOR EACH ROW
BEGIN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END;
$$
DELIMITER;
You can not set trigger on field change. It may be only on table level:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
IF OLD.inactivedate <> NEW.inactivedate THEN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END IF;
IF OLD.activedate <> NEW.activedate THEN
UPDATE mastersku
SET ordernow ='Active'
WHERE `SSKU` = NEW.SSKU;
END IF;
END$$
DELIMITER ;
Or a little elegant query using CASE statement:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
UPDATE mastersku
SET ordernow = CASE
WHEN OLD.inactivedate <> NEW.inactivedate THEN 'InActive'
WHEN OLD.activedate <> NEW.activedate THEN 'Active'
ELSE ordernow
END
WHERE
SSKU = NEW.SSKU;
END$$
DELIMITER ;
Related
I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
IF (NEW.returnBike <> null) THEN
UPDATE Bike SET isRented = false
WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
END IF;
Any advice?
I would be inclined to just do:
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
UPDATE Bike
SET isRented = false
WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
IF (NEW.returnBike IS NOT NULL) THEN
UPDATE BikeSET isRented = false
WHERE id = new.Bike;
END IF;
END; $$
DELIMITER ;
so to remove problem with syntax semicolon added delimiter and $$ after end;
Anyway thanks to give me some advice with if, comparing!
After insert in the table Aluguel update field Status to busy.
Not work.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = busy
WHERE id_apart = apartamento_id_apart;
END$$
DELIMITER ;
you have to use new key word
DELIMITER $$ CREATE TRIGGER Tgr_Status_Update AFTER UPDATE
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
---------------------------------------insert trigger -----------------
DELIMITER $$ CREATE TRIGGER Tgr_Status_Insert AFTER INSERT
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
To refer to a column from the table you're inserting into, you need to use NEW.column_name.
And if busy is a string, you need to put it in quotes.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = 'busy'
WHERE id_apart = NEW.apartamento_id_apart;
END$$
DELIMITER ;
DEMO
I want to create a trigger in mysql which insert a row in a table change_history when there is any update in another table event_data if event_data.title='event_media'.
DELIMITER $$
CREATE TRIGGER `after_evdata_update`
AFTER UPDATE ON event_data
FOR EACH ROW
BEGIN
if OLD.title <=> 'event_media'
INSERT INTO change_history (badge,city,country,venue,company,
date,rehosted,type,cancelled,name,tagline,description,agenda,pricing,
edition,photo_video,exhibitors,speakers,official_url,twitter_handle,
twitter_hashtag,facebook_url,contact,post_review_id) values(1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,null);
end if;
END $$
DELIMITER ;
I think this should work. Please try.
BEGIN
IF (OLD.title <> 'event_media')
THEN
INSERT INTO `change_history` (`xxx_id`,`and_all_other_fields_similar_way`)
VALUES (old.xxx_id,old.and_all_other_fields_similar_way);
END IF;
END
I have created a trigger , and this trigger updating the field value , but not checking the if condition ,
DELIMITER $$
create trigger `njsystem`.`test` BEFORE UPDATE on `njsystem`.`tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
UPDATE tbl_users SET user_active = 0;
END IF;
END; $$
DELIMITER ;
What I understand from your problem statement is that, you wanted to set user_active to 0 if user_failed_logins count goes above 3. Here is solution for that, you can also change values using new.
DELIMITER $$
create trigger `test` BEFORE UPDATE on `tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
SET NEW.user_active = 0;
END IF;
END; $$
DELIMITER ;
im using the following trigger to update the user table in another database in mysql 5.0.7
The creation of trigger gives no error but upon updating the user table in the first database the trigger is not working. Any suggestions?
DELIMITER $$
DROP TRIGGER IF EXISTS after_update_user;
CREATE TRIGGER after_update_user;
AFTER UPDATE ON db_test.user FOR EACH ROW;
BEGIN
UPDATE TABLE db_testplus.user;
SET name = NEW.name;
WHERE id = NEW.id;
END
$$
DELIMITER ;
I also used this code without the semicolons but still the same
DELIMITER $$
DROP TRIGGER IF EXISTS after_update_user
CREATE TRIGGER after_update_user
AFTER UPDATE ON db_test.user FOR EACH ROW
BEGIN
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id
END;
$$
DELIMITER ;
Finally the code that worked
delimiter |
DROP TRIGGER IF EXISTS after_update_user|
CREATE TRIGGER after_update_user AFTER UPDATE ON db_test.user
FOR EACH ROW BEGIN
UPDATE db_testplus.user SET name = NEW.name WHERE id = NEW.id;
END;
|
delimiter ;
Could you please check below
AFTER UPDATE ON db_test.user FOR EACH ROW
BEGIN
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id
END;
Try this;
CREATE TRIGGER after_update_user
AFTER UPDATE ON db_test.user FOR EACH ROW
UPDATE TABLE db_testplus.user
SET name = NEW.name
WHERE id = NEW.id;
Omitting begin-end keywords worked for me.
This works for me in MySQL 5.1.73:
CREATE TRIGGER `after_update`
AFTER UPDATE ON `test_table`
FOR EACH ROW UPDATE another_db.updated_table
SET some_name = NEW.some_name
WHERE id = NEW.id