mysql triggers to multiply two column values by using triggers - mysql

i have 4 columns in my table, vrms,irms,total,id.
i have created a trigger where i want to insert the result of the trigger in another table.
my code:
CREATE TRIGGER total_sum BEFORE INSERT ON meter5
FOR EACH ROW BEGIN
INSERT INTO hourly
SELECT vrms,irms,id,
NEW.irms * NEW.vrms AS total;
END
error:
not allowed to return a resultset from a trigger in mysql.

You need the trigger to be after insert something as
delimiter //
create trigger total_sum after insert on meter5
for each row
begin
insert into hourly (id,vrms,irms,total) values (new.id,new.vrms,new.irms,new.vrms*new.irms);
end ; //
delimiter ;

Related

How to write a MySql insert trigger based on inserted data value?

I want to wrote a trigger for table users. when new user added, if his Title is IT related, then create a record in IT contact list table.
So I wrote below trigger.
CREATE TRIGGER `test1` INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (username,Title);
END IF;
END;
It has error 'unknow column Title in the field list' But it did exist in table Users and IT_contact_list.So what's the issue?Thx.
Fixed and tested in workbench. Try this:
delimiter //
drop trigger if exists test1 //
CREATE TRIGGER `test1` AFTER INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(new.Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (new.username,new.Title);
END IF;
END//
insert into Users values('john','HardwareIT');
insert into Users values('bill','Hardware engineer');
select * From IT_contact_list;
--result set:
john HardwareIT

Mysql trigger after update New is wrong

I created a trigger that on a table update inserts row into another table, but one of the values that I pass is getting the old value of the table I am updating.
This is the trigger
DELIMITER $$
CREATE TRIGGER ordersHistory
AFTER UPDATE
ON orders FOR EACH ROW
BEGIN
IF (NEW.status <> OLD.status) THEN
INSERT INTO test (`test`) VALUES (NEW.ourNotes);
INSERT INTO test (`test`) VALUES (OLD.ourNotes);
END IF;
END $$
This insert at the table exactly the same thing.(the old value of ourNotes)

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 ;

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

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)