MySQL Trigger and Error Code 1362 - mysql

So I have the following query for a trigger:
DELIMITER $$
CREATE TRIGGER user_log_update BEFORE UPDATE on user_log
FOR EACH ROW
BEGIN
insert into user_log
(id, user_id, name, username, password, email, user_type_id, created)
VALUES(OLD.id, OLD.user_id, OLD.name, OLD.username, OLD.password, OLD.email, OLD.user_type_id, OLD.created);
IF (OLD.id = 1) THEN
SET OLD.id = OLD.id +1;
END IF;
SELECT * FROM user_log;
END$$
DELIMITER ;
When I try to execute this part of the script, I get Error Code: 1362. Updating of OLD row is not allowed in trigger
I don't know why I got this error and I don't see anything wrong in the syntax.
Does anyone know how to fix it?

So, increment new instead:
DELIMITER $$
CREATE TRIGGER user_log_update BEFORE UPDATE on user_log
FOR EACH ROW
BEGIN
insert into user_log(id, user_id, name, username, password, email, user_type_id, created)
VALUES(OLD.id, OLD.user_id, OLD.name, OLD.username, OLD.password, OLD.email, OLD.user_type_id, OLD.created);
IF (OLD.id = 1) THEN
SET NEW.id = OLD.id +1;
END IF;
END$$
DELIMITER ;

delimiter |
create trigger display_student_table
before insert on student_table
for each row
Begin
set new.student_name=trim(new.student_name);
set new.branch=trim(new.branch);
set new.address=trim(address);
End ;
|
delimiter ;

Related

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

I have error "#1363 - There is no NEW row in on DELETE trigger "

I have table "book" and "store_order" have relation
I want to make trigger(but it contain error):
DELIMITER $$
CREATE TRIGGER t1
before delete ON store_order
FOR EACH ROW
BEGIN
update book set number = number + NEW.quantity where ISBN = NEW.ISBN;
END
$$
DELIMITER ;
DELIMITER $$
CREATE
TRIGGER t2 AFTER delete
ON library.store_order
FOR EACH ROW BEGIN
update library.book
set library.book.number = (library.book.number + OLD.quantity)
where library.book.ISBN = OLD.ISBN;
END$$
DELIMITER ;
Use OLD instead of NEW when you want to get the deleted object.
for an example of my case. I'm getting the id of the newly added role by calling
NEW.id
and getting the same field's value while deleting by calling
OLD.id
Example:
DELIMITER $$
CREATE TRIGGER after_insert_role
AFTER INSERT ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping`
(`operation_type`, `table_name`, `oid`, `end_point`)
VALUES
('insert', 'role', NEW.id, 'new/role');
END $$
DELIMITER $$
CREATE TRIGGER after_delete_role
AFTER DELETE ON role FOR EACH ROW
BEGIN
INSERT INTO `sync_mapping` (`operation_type`, `table_name`, `oid`, `end_point`) VALUES
('delete', 'role', OLD.id, 'delete/role');
END $$
Whenever we are deleting the data
USE
OLD. instead of NEW.
CREATE TRIGGER user_history_delete AFTER DELETE ON user FOR EACH ROW
INSERT INTO user_history(action , name )
VALUES ("Delete" , OLD.name );

MySQL Insert Trigger not working with If Then Conditional

I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;

Resultset from trigger MySQL

This is my first time using trigger and I'm having a bit of trouble.
I'm creating a notification system that when a new notification is created it will add a row to the notify table. What is added depends on what notification type it is.
So far I've tried:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END; $$
DELIMITER ;
I searched around and then changed it to:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE insert_user_id INT(11);
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
SELECT ID INTO insert_user_id FROM user_customer WHERE clientID=NEW.user_reference;
INSERT INTO notify (user_id, notification_id) VALUES(insert_user_id, NEW.id);
END IF;
END; $$
DELIMITER ;
I've also tried mixing the above 2 up and all I'm getting is either can not return result set or there is a syntax error with my select query.
(Also, I'm assuming that the NEW keyword is predefined for triggers?)
Your first try was actually on the right track, but you did not write the result of your SELECT into a variable. Returning a result set from a trigger is not possible.
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE v_group INT;
SELECT notify_group INTO v_group FROM notification_types WHERE type=NEW.type;
IF (v_group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END $$
DELIMITER ;

Executing query in MySQL Trigger not working

I have two table 'testing1' with fields 'firstname' and 'lastname'. Another table 'testing2' with field 'firstname'.
So the trigger checks first whether the 'NEW.firstname' exists in 'testing2' table or not. If it does then it doesn't execute the INSERT query but if it doesn't exist then the INSERT query is executed and 'NEW.firstname' is added in 'testing2' table.
Here's the trigger that i created ... but I'm getting error in the IF loop ...
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount)
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END $$
I'm unable to figure out where did I made the mistake... Any help ??
Try
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount) THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END$$
DELIMITER ;
But maybe you shoud use IF (rowCount)>0
This worked for me :
DELIMITER //
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname INTO rowCount;
IF rowCount = 0 THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END //
DELIMITER ;