Creating trigger for multiple table insert operation - mysql

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

Related

Make a trigger that copies new data from table to another table after insert

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.

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 ;

MySQL Trigger Not Executing like it should

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.

Incorrect syntax in trigger

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)

MySQL Trigger Syntax ? Whats wrong with this one?

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