function not found when i try to update the mother of trigger - mysql

I have a problem with MySQL trigger. When I tried to update a database which is the mother of the trigger it says function not found
DELIMITER $$
CREATE TRIGGER trg_barang BEFORE UPDATE ON barang FOR EACH ROW BEGIN
INSERT INTO log_barang
set id_barang = OLD.id_barang,
harga_barang_baru=new.harga_barang,
harga_barang_lama=Old.harga_barang();
END
$$
DELIMITER ;
this trigger will show the data from "barang" and will store to this new trigger.and then when I try to update, it says function "old.harga_barang not found" how do I fix it?

Related

MySQL Create trigger for get value from another column

i have a table which contains columns as "barkoda1,barkoda2,barkoda3,#pdfa1,#pdfa2,#pdfa3", what i'm trying to do is when a record assigned to barkoda1 copy same record to #pdfa1,for example if barkoda1 defined as "1234567890" #pdfa1 should be "1234567890.pdf".
i tried to create a trigger for it, but failed;
DELIMITER //
CREATE TRIGGER pdfdeneme AFTER INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
UPDATE finyeb_ba_2369_bio_for_2__r_d_ SET `#pdfa1`=NEW.barkoda1;
END//
DELIMITER ;
when i create this trigger,it doesn't return any error but when i add record to table mysql returns "it is already used by statement which invoked this stored function/trigger".
also tried this way
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
INSERT INTO finyeb_ba_2369_bio_for_2__r_d_(`#pdfa1`) VALUES(NEW.barkoda1);
END//
DELIMITER ;
But same error happened, what am i doing wrong here?
You need a BEFORE INSERT TRIGGER
And then only use the SET Clause
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
SET NEW.`#pdfa1` = CONCAT(NEW.barkoda1,'.pdf');
END//
DELIMITER ;
But your column name is a bad choice, it leads on the first glance to false conclusions.

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.

Syntax Error for MySQL Trigger

I created a simple trigger as below:
CREATE TRIGGER `trigger_update_rche_prep` BEFORE UPDATE ON `rche_prep`
FOR EACH ROW
SET NEW.case_number = 'ABC';
Which is working, when I want to add the BEGIN...END syntax to it, like below:
CREATE TRIGGER `trigger_update_rche_prep` BEFORE UPDATE ON `rche_prep`
FOR EACH ROW
BEGIN
SET NEW.case_number = 'ABC';
END;
It fails, I have no idea why this will happen, can anyone help?
Try this:
DELIMITER //
CREATE TRIGGER `trigger_update_rche_prep` BEFORE UPDATE ON `rche_prep`
FOR EACH ROW
BEGIN
SET NEW.case_number = 'ABC';
END;//
DELIMITER ;

I am trying to make a trigger in mysql, but not working

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 ;

MySQL Trigger On A Column

I hav a table with several fields. One field is "date_assigned" and the other is "assigned". "date_assigned" has a datatype of timestamp and can be null. "assigned" has a datatype of tinyint and the values are either 0 (default; 'not assigned') or 1 ('assigned').
I would like to create a trigger that would automatically update the "assigned" value to 1 when "date_assigned" is updated with a value (is not null).
I've used triggers before, but have not used them in conjunction with checking if a value is null. I'm unclear on the syntax, so any help would be appreciated. So far, I've tried:
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;
I just get Error Code: 1064. I looked upo the code, and it appears that it's a syntax error. So what syntax mistake am I making, and is this even the correct 'grammar'?
Try putting BEGIN in a new line as follows.
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW
BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END; //Change here also.
$$
DELIMITER ;
This is because of default delimiter in MySQL set to ;. So, the first line should look like DELIMITER $$;
DELIMITER $$;
CREATE TRIGGER `<database>`.`<trigger_name>`
AFTER UPDATE ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;