DELETE Trigger Not working - MYSQL - mysql

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.

Related

create a trigger to Delete from NotSoldTable if found SoldTable

Trying to create a Delete Trigger. If the sku is found in sold table need to delete it in not sold table
delimiter $$
drop trigger delete_sold_trigger $$
create trigger delete_sold_trigger after insert on soldtable
for each row
begin
IF soldtable.sku EXIST IN notsoldtable THEN
DELETE from notsoldtable WHERE notsoldtable.sku=soldtable.sku;
END IF;
end$$
delimiter ;
CREATE TRIGGER delete_from_notsoldtable_trigger
AFTER INSERT ON soldtable
FOR EACH ROW
DELETE FROM notsoldtable
WHERE notsoldtable.sku=NEW.sku;

How can I use MySQL Trigger to update quantity field in 'items' table with quantity - quantity_delivered

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?

After insert, delete on other table trigger

I want a trigger that deletes the row in user_briefcases after a row is inserted in user_briefcases_sold
both tables have briefcase_id and user_id
I have no idea why this wont work:
CREATE TRIGGER delete_user_briefcase_when_sold
DELIMITER $$
AFTER INSERT ON user_briefcases_sold FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id && user_id = NEW.user_id;
END;
$$ DELIMITER ;
You have syntax error
try this
DELIMITER $$
CREATE
TRIGGER delete_user_briefcase_when_sold AFTER INSERT
ON user_briefcases_sold
FOR EACH ROW BEGIN
DELETE FROM user_briefcases WHERE briefcase_id = NEW.briefcase_id AND user_id = NEW.user_id;
END$$
DELIMITER ;

Mysql trigger on insert, if row not exist

My trigger will not work.
Basically when a user_briefcase is created i want to also create a row in user_complementary_info if it not already exist, where user_complementay.user_id = the newly created user_briefcase.user_id
My trigger
DROP TRIGGER IF EXISTS complementary_user_info
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) user_complementary WHERE user_id=NEW.user_briefcases.user_id) = 0 THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id)
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS complementary_user_info;
DELIMITER $$
CREATE TRIGGER complementary_user_info AFTER INSERT user_briefcases FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT 1 FROM user_complementary_info WHERE user_id = NEW.user_id) THEN
INSERT INTO user_complementary (user_id)
VALUES (NEW.user_briefcases.user_id);
END IF;
END $$
DELIMITER ;

Using Trigger to update table in another database

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