Hi all Im trying to recreate a trigger
I did a show create trigger and copied the original sql ;
Now when i run it i get an syntax error. ?? Any Idead
CREATE DEFINER=`root`#`localhost` TRIGGER `update_cached_tables_for_product_insert` AFTER INSERT ON `Product` FOR EACH ROW BEGIN INSERT INTO ProductOffercount (product_id, num_offers) VALUES (NEW.id, 0); END
Mysql treats ; after your query (that is the part of trigger definition) as the end of the whole statement. Thus you get syntax error.
You need to redefine delimiter before CREATE TRIGGER clause:
delimiter |
CREATE DEFINER=`root`#`localhost`
TRIGGER `update_cached_tables_for_product_insert` AFTER INSERT ON `Product`
FOR EACH ROW BEGIN
INSERT INTO ProductOffercount (product_id, num_offers) VALUES (NEW.id, 0);
END;
|
delimiter ;
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Related
I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.
I have three tables: users, account and accountinfo and I am trying to make a trigger that will add the id from users to the UserID column in the account table. Here is what I tried:
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END
However, I get an error right after my INSERT statement that says,
Syntax Error: insert 'semicolon'
Why am I getting this error is I have the semicolon or is my trigger just wrong?
I'm using MySQL 5.6 if that makes any difference as well.
You need to specify the delimiter:
delimiter //
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id);
END; //
delimiter ;
Try this:
CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users
FOR EACH ROW
BEGIN
INSERT INTO defaultdatabase.account(UserID) VALUES (new.id)
END;
Please insert a semicolon after END.
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 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
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