create a trigger to Delete from NotSoldTable if found SoldTable - mysql

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;

Related

Trigger produces no output

Why does the trigger have no output and is not created?
create trigger chronik BEFORE UPDATE
ON eastatus_t
FOR EACH ROW
BEGIN
insert into sendechronik_t(mandant, fk_eakopf_posnr, lfd_nr, statusdatum, fk_statusart_mc, statuszeit, fk_ort_status)
SELECT (mandant, fk_eakopf_posnr, lfd_nur, statusdatum, fk_statusart_mc, statuszeit, fk_ort_status)
FROM eastatus_t
END$$
DELIMITER ;

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?

I want to create a trigger in mysql which insert a row in a table when update a row in another table

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

DELETE Trigger Not working - 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.

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 ;