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
Related
I am trying to update a column ( called DateModified ) before update happens on the table row.
so here is my trigger:
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW BEGIN
SET new.DateModified = NOW();
END
BUT I GET THIS DAMN ERROR WHICH I JUST CANNOT FIGURE OUT WHY:
Use DELIMITER
DELIMITER //
CREATE TRIGGER `date_mod_category` BEFORE UPDATE ON `categories`
FOR EACH ROW
BEGIN
SET new.DateModified = NOW();
END //
DELIMITER ;
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 ;
How can I use MySQL Trigger to update quantity field in items table with quantity-quantity_delivered when quantity_delivered in the requests table is updated ?
Here Is My Query, I have tested but I see its not getting updated, can any one help me please?
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items .quantity = items.quantity - requests.quantity_delivered
WHERE items .item_id = requests.item_id;
END;
$$
DELIMITER ;
I have resolved by myself like this and its working for me thanks All!
DELIMITER $$
CREATE TRIGGER `quantityTrigger` AFTER UPDATE ON `requests` FOR EACH ROW BEGIN
UPDATE items i
SET i.quantity=i.quantity-NEW.quantity_delivered
WHERE i.item_id=NEW.item_id;
END
$$
DELIMITER ;
You should be using the new. values in your trigger like this
drop trigger if exists items_update;
DELIMITER $$
CREATE TRIGGER items_update AFTER UPDATE ON requests
FOR EACH ROW
BEGIN
UPDATE items
SET items.quantity = items.quantity - new.quantity_delivered
where items.item_id = new.item_id;
END;
$$
DELIMITER ;
As an aside should this be an after insert trigger?
First table - rm_desc
Second table - room_cat_mapping
Want to achieve : while deleting record from rm_desc automatically it should delete related record from room_cat_mapping as well.
BUT BELOW TRIGGER IS NOT WORKING, ANY clue new to trigger
DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `DELETE_ROOM_TYPES` BEFORE DELETE
ON `rm_desc`
FOR EACH ROW
BEGIN
DELETE FROM room_cat_mapping
WHERE room_cat_mapping.prop_id = rm_desc.res AND room_cat_mapping.room_cat_id = rm_desc.rm_cat;
END$$
DELIMITER ;
This should do:
DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$
CREATE
TRIGGER `DELETE_ROOM_TYPES` AFTER DELETE
ON `rm_desc`
FOR EACH ROW
BEGIN
DELETE FROM room_cat_mapping
WHERE room_cat_mapping.prop_id = OLD.res AND room_cat_mapping.room_cat_id = OLD.rm_cat;
END$$
DELIMITER ;
I changed the trigger to AFTER and referenced the deleted row values using OLD.
I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.