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//
Related
I have two tables companies (id,company_name) and users (id,company_id,user_name);
Now I want to create a view when data is inserted in companies table.
I used following two ways but getting same error in both the cases. Error is :
#1422 - Explicit or implicit commit is not allowed in stored function or trigger.
First Query is :
DELIMITER $$
CREATE TRIGGER testInsert
AFTER INSERT ON companies
FOR EACH ROW BEGIN
CREATE VIEW test(user_name) AS
SELECT user_name
FROM users;
END$$
DELIMITER ;
Then I tried creating procedure first and called that procedure in trigger like this
create procedure createView()
create view vt as select * from users;
CREATE TRIGGER `testInsert3` AFTER INSERT ON `companies`
FOR EACH ROW
BEGIN
CALL createView();
END;
Please help me guys.
Thanks All.
please refer this link, i think this will help you
http://stackoverflow.com/questions/16256250/create-view-in-a-trigger
try creating the view like this
DELIMITER $$
CREATE TRIGGER testInsert
AFTER INSERT ON companies
FOR EACH ROW BEGIN
EXECUTE('CREATE VIEW test(user_name) AS SELECT user_name
FROM users')
END$$
DELIMITER ;
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
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.
I have two tables called account and transaction. I need to insert data to transaction automatically when account inserts data. I created this trigger in MySQL. It gives error 1064 (Syntax error). What is the problem?
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
MySQL gets confused with semicolons inside the BEGIN END block, so you must use DELIMITER to temporarily force MySQL to use a different delimiter.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;
You probably need to replace:
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
with:
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
Here is you original trigger
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
Replace account. with NEW. (optional : replace " with ')
DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
NEW.deposit,NEW.accNo,
'Teller','Cash','Deposit');
END $$
DELIMITER ;
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
-- trigger body
-- this code is applied to every
-- inserted/updated/deleted row
END;
Seems like you're missing a table name after 'account'. (edit: or a database name before account, I'm not certain as to what account pertains to)
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