MySQL - Trigger for updating same table after insert - mysql

Here's what I'm trying to do:
When there's a new INSERT into the table ACCOUNTS, I need to update the row in ACCOUNTS where pk = NEW.edit_on by setting status='E' to denote that the particular (old) account has been edited.
DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` AFTER INSERT on ACCOUNTS
FOR EACH ROW BEGIN
update ACCOUNTS set status='E' where ACCOUNTS.pk = NEW.edit_on ;
END$$
DELIMITER ;
The requirement is NOT that I manipulate the newly inserted column, but an already existing column with pk = NEW.edit_on
However, I can't update the same table: Can't update table ACCOUNTS ... already used by the statement that invoked this trigger
Please suggest a workaround
PS: I have already gone through Updating table in trigger after update on the same table, Insert into same table trigger mysql, Update with after insert trigger on same table and mysql trigger with insert and update after insert on table but they dont seem to answer my question.
Edit
ACCOUNTS Table:
CREATE TABLE `ACCOUNTS` (
`pk` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(9) unsigned NOT NULL,
`edit_on` bigint(10) unsigned DEFAULT NULL,
`status` varchar(1) NOT NULL DEFAULT 'A',
PRIMARY KEY (`pk`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=2147483726 DEFAULT CHARSET=latin1

It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.

This is how I update a row in the same table on insert
activationCode and email are rows in the table USER.
On insert I don't specify a value for activationCode, it will be created on the fly by MySQL.
Change username with your MySQL username and db_name with your db name.
CREATE DEFINER=`username`#`localhost`
TRIGGER `db_name`.`user_BEFORE_INSERT`
BEFORE INSERT ON `user`
FOR EACH ROW
BEGIN
SET new.activationCode = MD5(new.email);
END

Had the same problem but had to update a column with the id that was about to enter, so you can make an update should be done BEFORE and AFTER not BEFORE had no id so I did this trick
DELIMITER $$
DROP TRIGGER IF EXISTS `codigo_video`$$
CREATE TRIGGER `codigo_video` BEFORE INSERT ON `videos`
FOR EACH ROW BEGIN
DECLARE ultimo_id, proximo_id INT(11);
SELECT id INTO ultimo_id FROM videos ORDER BY id DESC LIMIT 1;
SET proximo_id = ultimo_id+1;
SET NEW.cassette = CONCAT(NEW.cassette, LPAD(proximo_id, 5, '0'));
END$$
DELIMITER ;

On the last entry; this is another trick:
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_schema = ... and table_name = ...

DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` **BEFORE** INSERT on ACCOUNTS
FOR EACH ROW BEGIN
SET NEW.STATUS = 'E';
END$$
DELIMITER ;

Instead you can use before insert and get max pkid for the particular table and then update the maximium pkid table record.

Related

MYSQL: I am trying to set a trigger to update my current row on update.(not updating all the rows just the updated row) [duplicate]

Here's what I'm trying to do:
When there's a new INSERT into the table ACCOUNTS, I need to update the row in ACCOUNTS where pk = NEW.edit_on by setting status='E' to denote that the particular (old) account has been edited.
DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` AFTER INSERT on ACCOUNTS
FOR EACH ROW BEGIN
update ACCOUNTS set status='E' where ACCOUNTS.pk = NEW.edit_on ;
END$$
DELIMITER ;
The requirement is NOT that I manipulate the newly inserted column, but an already existing column with pk = NEW.edit_on
However, I can't update the same table: Can't update table ACCOUNTS ... already used by the statement that invoked this trigger
Please suggest a workaround
PS: I have already gone through Updating table in trigger after update on the same table, Insert into same table trigger mysql, Update with after insert trigger on same table and mysql trigger with insert and update after insert on table but they dont seem to answer my question.
Edit
ACCOUNTS Table:
CREATE TABLE `ACCOUNTS` (
`pk` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(9) unsigned NOT NULL,
`edit_on` bigint(10) unsigned DEFAULT NULL,
`status` varchar(1) NOT NULL DEFAULT 'A',
PRIMARY KEY (`pk`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=2147483726 DEFAULT CHARSET=latin1
It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.
This is how I update a row in the same table on insert
activationCode and email are rows in the table USER.
On insert I don't specify a value for activationCode, it will be created on the fly by MySQL.
Change username with your MySQL username and db_name with your db name.
CREATE DEFINER=`username`#`localhost`
TRIGGER `db_name`.`user_BEFORE_INSERT`
BEFORE INSERT ON `user`
FOR EACH ROW
BEGIN
SET new.activationCode = MD5(new.email);
END
Had the same problem but had to update a column with the id that was about to enter, so you can make an update should be done BEFORE and AFTER not BEFORE had no id so I did this trick
DELIMITER $$
DROP TRIGGER IF EXISTS `codigo_video`$$
CREATE TRIGGER `codigo_video` BEFORE INSERT ON `videos`
FOR EACH ROW BEGIN
DECLARE ultimo_id, proximo_id INT(11);
SELECT id INTO ultimo_id FROM videos ORDER BY id DESC LIMIT 1;
SET proximo_id = ultimo_id+1;
SET NEW.cassette = CONCAT(NEW.cassette, LPAD(proximo_id, 5, '0'));
END$$
DELIMITER ;
On the last entry; this is another trick:
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_schema = ... and table_name = ...
DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` **BEFORE** INSERT on ACCOUNTS
FOR EACH ROW BEGIN
SET NEW.STATUS = 'E';
END$$
DELIMITER ;
Instead you can use before insert and get max pkid for the particular table and then update the maximium pkid table record.

How can I update MySQL table row's when 'BEFORE UPDATE' trigger is firing?

I created a table
Databases Name - mytrigger;
Table name - employee_audit
use mytrigger;
create table employee_audit(
id int auto_increment primary key,
employeeNumber int not null,
lastName varchar(50) not null,
changee datetime default null,
action varchar(50) default null
);
After that, I created one update trigger
My trigger name is
before_employees_update
DELIMITER $$
create trigger before_employees_update
before update on employee_audit
for each row
begin
insert into employee_audit
set action ='update',
employeeNumber = OLD.employeeNumber,
lastName = OLD.lastName,
changee = now();
end$$
DELIMITER ;
After that, I inserted values in table using this command ->
insert into employee_audit values(1,112,'prakash','2015-11-12 15:36:20' ,' ');
After that, I want to update my table row where id =1
update employee_audit set lastName = 'Sharma' where employeeNumber =112;
But it is not executed give an error
ERROR 1442 (HY000): Can't update table 'employee_audit' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
When I searched on Google I found a lot of Question with the same error. But not able to fix my problem. what is the reason I'm not able to update my row?
What i suggest,you can create one log table like employee_audit_LOG .
And on every insert or update in main table you can make new entry in this table or update existing record.
Also you can add updated_timestamp column to that LOG table which maintain when did specific record get updated.
The error itself tells you the answer. This is because, you can't use the same table on which trigger is being executed. You need to store your audit logs into some different table.

Triggers mysql sql

Whats wrong with this code?
CREATE TRIGGER User_trigger AFTER DELETE ON users
FOR EACH ROW
BEGIN
INSERT INTO del_users ('fullname') VALUES ('fullname');
END;
Please help.
There are several issues:
If you use BEGIN ... END block you have to change DELIMITER. On the other hand if your trigger contains only one statement just don't use BEGIN ... END. Take a closer look at Defining Stored Programs
In MySQL to be able to refer to columns of a row being deleted you have to use OLD keyword. Take a closer look at Trigger Syntax and Examples
That being said and assuming that your simplified table schema look something like this
CREATE TABLE users
(
id int not null auto_increment primary key,
fullname varchar(8)
);
CREATE TABLE del_users
(
id int not null auto_increment primary key,
user_id int, fullname varchar(32),
deleted datetime
);
Your trigger would look
CREATE TRIGGER tg_ad_users
AFTER DELETE ON users
FOR EACH ROW
INSERT INTO del_users (user_id, fullname, deleted)
VALUES (OLD.id, OLD.fullname, NOW());
Here is SQLFiddle demo
or with BEGIN ... END block
DELIMITER //
CREATE TRIGGER tg_ad_users
AFTER DELETE ON users
FOR EACH ROW
BEGIN
INSERT INTO del_users (user_id, fullname, deleted)
VALUES (OLD.id, OLD.fullname, NOW());
END//
DELIMITER ;
Here is SQLFiddle demo
Try the following:
CREATE TRIGGER User_trigger AFTER DELETE
ON users
AS
INSERT INTO del_users (fullname)
SELECT d.fullname from Deleted d
Here is information on using the DELETED and INSERTED Tables:
http://technet.microsoft.com/en-us/library/ms191300.aspx

update statement in Insert trigger on the same table

I want to write a trigger that includes an UPDATE statement on the same table, like this:
create trigger AFTER INSERT ON myTable
FOR EACH ROW BEGIN
update myTable set ... where ...
end
How can I do this?
Quoting MySQL docs:
A trigger can also affect other tables, but it is not permitted to
modify a table that is already being used (for reading or writing) by
the statement that invoked the function or trigger.
You can do this (update the table that has triggered the after-insert trigger) BUT to avoid recursion, your FOR INSERT and FOR UPDATE trigger must be separate procedures. The other issue is that Microsoft SQL Server calls the FOR UPDATE trigger only once for any update statement. There is no FOR EACH ROW in SQL Server. Use a single UPDATE... table... WHERE statement inside your FOR INSERT TRIGGER to update a lot of rows at the same time.
I think it is not possible.
I create one table script is given below
CREATE TABLE `tbl1` (
`id` INT(10) NULL DEFAULT NULL,
`msg` VARCHAR(50) NULL DEFAULT NULL
)
Then i created trigger on that table using following query
delimiter |
create trigger trigger1 before insert on tbl1 for each row begin
update tbl1 set msg='insert occured in tbl1' where id=5;
end
|
delimiter ;
when i try to run following query
insert into tbl1 values (1,'ddd');
I got following error
"Cant update table 'tbl1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger"
So its not possible.
For more info visit
http://bugs.mysql.com/bug.php?id=50684

MySQL Trigger for after insert and update table

i was created a table for content management.I am not familiar with Mysql trigger.
Key Default
a varchar(50)
b varchar(50)
c text
status varchar(100)
This my table.I was created a trigger When each table update table change the status.
DELIMITER $$
CREATE TRIGGER tr2 BEFORE insert or UPDATE ON p
FOR EACH ROW BEGIN
SET NEW.status = '1';
END;
$$
DELIMITER ;
This trigger only update when the row will be modified .How can create trigger for change the "status" during every insert and update.. Please help me any one
This is not correct syntax. Just create a trigger before update
DELIMITER $$
CREATE TRIGGER tr2 BEFORE UPDATE ON p
FOR EACH ROW BEGIN
SET NEW.date = '1';
END;
$$
DELIMITER ;
and make the default value for column status = '1'.
CREATE TABLE p(
a varchar(50),
b varchar(50),
c text,
status varchar(100) NOT NULL DEFAULT '1'
);
That way whenever you insert a row into p and don't specify a value for status, status will be 1. You could create another trigger BEFORE INSERT but I don't recommend that. And the above solution does the same.