DELIMITER $$
CREATE TRIGGER arrange_serial
AFTER DELETE ON db.table
FOR EACH ROW
BEGIN
UPDATE db.table set serial=serial-1 where serial > DELETED.serial;
END$$
DELIMITER ;
On delete I'm getting the error that db.DELETED does not exist. Can anyone suggest me where I'm wrong.
You are supposed to reference the old value of the deleted row. Use OLD in the place of DELETED;
DELIMITER $$
CREATE TRIGGER arrange_serial
AFTER DELETE ON db.table
FOR EACH ROW
BEGIN
UPDATE db.table set serial=serial-1 where serial > OLD.serial;
END$$
DELIMITER ;
Related
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 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.
Here s my trigger:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
SET #old_stud_id = NEW.student_id;
INSERT INTO assigned_groups (student_id) VALUES (#old_stud_id);
END$$
DELIMITER ;
I used mysql variable because direct assignment wasn't working.
But I found this one is working:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
INSERT INTO assigned_groups (student_id) VALUES (83);
END$$
DELIMITER ;
I also found that renaming student_id column in assigned_groups to student_id_1 will work, but that's not a feasible solution as i have to make many triggers like this.
EDIT: Sorry guys, it turn out to be some problem with my PHP code. When i edit directly in mysql, trigger works correctly. But not when I update it through PHP code. What might be the issue?
You can do it like this, I tested and it is working:
DELIMITER $$
CREATE TRIGGER after_changing_student_year
AFTER UPDATE ON student_details
FOR EACH ROW
BEGIN
DECLARE old_stud_id INT;
SET old_stud_id = NEW.student_id;
INSERT INTO assigned_groups (student_id) VALUES (old_stud_id);
END$$
DELIMITER ;
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.