MySQL Trigger Not Executing like it should - mysql

I wrote the trigger below, but when I insert into the table it doesn't capitalize the inserted word, like it is supposed to. (no errors either). Any possible solutions?
(I have an employee and an employee2 table, since I read you cant have the trigger in the table it is trying to update).
DELIMITER $$
DROP TRIGGER IF EXISTS mytrigger$$
CREATE TRIGGER mytrigger before INSERT ON employee FOR EACH ROW
BEGIN
update employee2
SET employee2.FName = CONCAT(UCASE(LEFT(employee2.FName, 1)), LCASE(SUBSTRING(employee2.FName, 2)));
END;
$$
DELIMITER ;

You can have triggers on the table you are trying to update, you just can't run UPDATE queries. Instead, you modify the data before it is inserted:
DELIMITER $$
DROP TRIGGER IF EXISTS mytrigger$$
CREATE TRIGGER mytrigger before INSERT ON employee FOR EACH ROW
BEGIN
SET NEW.FName = CONCAT(UCASE(LEFT(NEW.FName, 1)), LCASE(SUBSTRING(NEW.FName, 2)));
END;
$$
DELIMITER ;
The "NEW" keyword gives you access to the data that is going to be inserted.

Related

how to insert just the row I updated in one table into another table by using trigger in myspl?

I am learning to create a trigger that can insert the row I update in one table into another table.
the query below is my creating trigger:
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`)
SELECT 'deliverycompany',`deliverycompany`.`CompanyID`,USER(),CURDATE() FROM `deliverycompany` ;
END$$
DELIMITER ;
when the trigger is fired, All rows in 'deliverycompany' are insert into the 'editor_log' rather than just the row I updated.
How to just select the row I update
if using where how can I locate the row I updated (any value in the row)
Try something like this
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`) VALUES (OLD.deliverycompany, OLD.deliverycompany, USER(),CURDATE()) ;
END$$
DELIMITER ;

create trigger AFTER INSERT ON DUPLICATE KEY UPDATE [duplicate]

Is it possible to fire a mysql trigger for both the insert and update events of a table?
I know I can do the following
CREATE TRIGGER my_trigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
.....
END //
CREATE TRIGGER my_trigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
.....
END //
But how can I do
CREATE TRIGGER my_trigger
AFTER INSERT ON `table` AND
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
.....
Is it possible, or do I have to use 2 triggers? The code is the same for both and I don't want to repeat it.
You have to create two triggers, but you can move the common code into a procedure and have them both call the procedure.
In response to #Zxaos request, since we can not have AND/OR operators for MySQL triggers, starting with your code, below is a complete example to achieve the same.
1. Define the INSERT trigger:
DELIMITER //
DROP TRIGGER IF EXISTS my_insert_trigger//
CREATE DEFINER=root#localhost TRIGGER my_insert_trigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
-- NEW.id is an example parameter passed to the procedure but is not required
-- if you do not need to pass anything to your procedure.
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
2. Define the UPDATE trigger
DELIMITER //
DROP TRIGGER IF EXISTS my_update_trigger//
CREATE DEFINER=root#localhost TRIGGER my_update_trigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
3. Define the common PROCEDURE used by both these triggers:
DELIMITER //
DROP PROCEDURE IF EXISTS procedure_to_run_processes_due_to_changes_on_table//
CREATE DEFINER=root#localhost PROCEDURE procedure_to_run_processes_due_to_changes_on_table(IN table_row_id VARCHAR(255))
READS SQL DATA
BEGIN
-- Write your MySQL code to perform when a `table` row is inserted or updated here
END//
DELIMITER ;
You note that I take care to restore the delimiter when I am done with my business defining the triggers and procedure.
unfortunately we can't use in MySQL after INSERT or UPDATE description, like in Oracle

MySQL error code 1235

In MySQL I tried to define a trigger like this:
DELIMITER $$
CREATE TRIGGER vipInvite
AFTER INSERT ON meetings
FOR EACH ROW
BEGIN
IF(NOT EXISTS (SELECT * FROM participants
WHERE meetid = NEW.meetid AND pid ='vip'))
THEN
IF(EXISTS(SELECT * FROM meetings WHERE meetid = NEW.meetid AND slot > 16))
THEN
INSERT INTO participants(meetid, pid)
VALUES (NEW.meetid,(SELECT userid
FROM people WHERE people.group = 'tap' GROUP BY invite));
END IF;
END IF;
END $$
DELIMITER ;
Produces this error:
This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table.
Is there a way to work around this so I can define multiple triggers?
This error means you already have an AFTER INSERT trigger on meetings table.
If it is the same trigger (meaning vipInvite) that you created earlier and now you want to replace it then you need to drop it first
DROP TRIGGER vipInvite;
DELIMITER $$
CREATE TRIGGER vipInvite
...
END$$
DELIMITER ;
Now if you have some other trigger you have to merge code from both triggers into one, then drop existing trigger, and then create a new one.
To show the list of existing triggers use SHOW TRIGGERS.
SHOW TRIGGERS WHERE `table` = 'meetings';
How to reproduce this error in MySQL:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple
triggers with the same action time and event for one table'
Run the following queries:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
DELIMITER //
CREATE TRIGGER mytrigger2 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
If you want to hook more than one action onto the same event/table, you will have to cram all of it into one trigger. You could call many stored procedures like this:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
CALL fromulate_the_moobars(NEW.myid);
CALL its_peanut_butter_jelly_time(NEW.myname);
END//

Creating trigger for multiple table insert operation

I have created table in Slq yog which was working fine. then i created same on mysql terminal.
this also executes with no error:
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$ DELIMITER ;
But after this when I enter show triggers or any other command it ask for -> no input
so I need to terminate mysql. Then when I check show triggers says Empty set.
Where is the mistake here?
DELIMITER $$
CREATE TRIGGER trigger1 AFTER INSERT ON table2 FOR EACH ROW BEGIN
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp values (id,NEW.sent,NEW.pcount,NEW.ncount);
END
$$
DELIMITER ;
new delimiter must be on another line

MySQL Fire Trigger for both Insert and Update

Is it possible to fire a mysql trigger for both the insert and update events of a table?
I know I can do the following
CREATE TRIGGER my_trigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
.....
END //
CREATE TRIGGER my_trigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
.....
END //
But how can I do
CREATE TRIGGER my_trigger
AFTER INSERT ON `table` AND
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
.....
Is it possible, or do I have to use 2 triggers? The code is the same for both and I don't want to repeat it.
You have to create two triggers, but you can move the common code into a procedure and have them both call the procedure.
In response to #Zxaos request, since we can not have AND/OR operators for MySQL triggers, starting with your code, below is a complete example to achieve the same.
1. Define the INSERT trigger:
DELIMITER //
DROP TRIGGER IF EXISTS my_insert_trigger//
CREATE DEFINER=root#localhost TRIGGER my_insert_trigger
AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
-- NEW.id is an example parameter passed to the procedure but is not required
-- if you do not need to pass anything to your procedure.
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
2. Define the UPDATE trigger
DELIMITER //
DROP TRIGGER IF EXISTS my_update_trigger//
CREATE DEFINER=root#localhost TRIGGER my_update_trigger
AFTER UPDATE ON `table`
FOR EACH ROW
BEGIN
-- Call the common procedure ran if there is an INSERT or UPDATE on `table`
CALL procedure_to_run_processes_due_to_changes_on_table(NEW.id);
END//
DELIMITER ;
3. Define the common PROCEDURE used by both these triggers:
DELIMITER //
DROP PROCEDURE IF EXISTS procedure_to_run_processes_due_to_changes_on_table//
CREATE DEFINER=root#localhost PROCEDURE procedure_to_run_processes_due_to_changes_on_table(IN table_row_id VARCHAR(255))
READS SQL DATA
BEGIN
-- Write your MySQL code to perform when a `table` row is inserted or updated here
END//
DELIMITER ;
You note that I take care to restore the delimiter when I am done with my business defining the triggers and procedure.
unfortunately we can't use in MySQL after INSERT or UPDATE description, like in Oracle