Updata calculated field in "AFTER INSERT' trigger - mysql

I need some help writing a MySQL trigger.
what i want to do is to calculated hashe value of autoincrement ID and store it in separate column:
create trigger hashing after insert on categories
for each row begin
set new.hashed = md5(CONCAT("key",`categories `.`id`));
end;
but i get error:
[Err] 1362 - Updating of NEW row is not allowed in after trigger
Can someone help me achieve what i need? Is this possible at all using triggers?
Thanks

What you are trying to do is not supported.
You can only set the value of the "hashed" column in a BEFORE INSERT trigger, not an AFTER INSERT trigger.
However, you can't access the value of the auto-increment "id" column in a BEFORE INSERT trigger because that value is not known yet.
If you are determined to do this via a trigger, one option is to create a separate table to store the hashed value (along with a foreign key to reference the "categories" table), and insert into that table using an AFTER INSERT trigger on your "categories" table.

Related

MYSQL Trigger to insert column value in same table

I have a mysql Innodb table 'classrooms_subjects' as
id|classroom_id|subject_id
classroom_id & subject_id are composite keys. Whenever i insert a row with classroom_id & subject_id, my id field is inserted as 0.
Now i want to create a trigger which will enter id field as last_inserted_id()+1.
Also I need to take care of multiple records inserted at a time. My trigger is like below:
CREATE TRIGGER `increment_id` AFTER INSERT ON `classrooms_subjects`
FOR EACH ROW BEGIN
UPDATE classrooms_subjects
SET classrooms_subjects.id = LAST_INSERT_ID() + 1 WHERE id=0;
END
when i am inserting a record I am getting the error as:
"Cant update table in trigger because it is already used by statement which invoked this trigger
For general info: using an update statement inside the trigger isn't right.
Better to use a before insert trigger and simply assign the value of your column using NEW.id
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
A column named with OLD is read only. You can refer to it (if you have
the SELECT privilege), but not modify it. You can refer to a column
named with NEW if you have the SELECT privilege for it. In a BEFORE
trigger, you can also change its value with SET NEW.col_name = value
if you have the UPDATE privilege for it. This means you can use a
trigger to modify the values to be inserted into a new row or used to
update a row. (Such a SET statement has no effect in an AFTER
trigger because the row change will have already occurred.)
You should probably structure your table to make the auto_increment work properly. Better a solution that works when multiple sessions are inserting to the DB at once.

how to use auto-incremented column of one table in another table-mysql workbench 6.2

I tried it with the help of this query "insert into class(faculty_id) select faculty_id from faculty;" but it is inserting already existing values only. Here one column of the table is under auto-increment so I wanted to take this incremented values to another table's column in mysql workbench 6.2. I even tried setting a foreign key between the two columns but it dint work. Please can anyone help out with this issue??
This is what you want i think. an after insert trigger will fire every time something is inserted into faculty, and after the data has been inserted. You can access the values that have just been inserted with the NEW. prefix.
DELIMITER //
create trigger classInsert after insert on faculty
for each row
begin
insert into class(faculty_id) values (NEW.faculty_id);
end//
DELIMITER ;
And it will automatically add an entry into the class table every time you add a new value to faculty.
Fiddle example

Need an idea to reinsert a row in a table with same primary key values using trigger in Mysql

I'm having a table with 3 columns say a,b,c where a is the primary key. Consider it has a value
1 hai hello
Using triggers i need to insert another row value as
1 bye hello
please note that i want to insert the value not to update and i want to perform this in trigger.
First i have planned to do it with trigger in BEFORE INSERT, but I'm getting an error while inserting the value.
Error: Can't update table 'table1' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
I'm already having another trigger in AFTER INSERT.

Insert unqiue record into one table and the results into a second table

Using MySQL and PHP, I have two tables ResponseTable and EntryTable
Response table has
RespID(pk,uq,ai, int), Response(string)
Entry table has
EntryID(pk,uq,ai,int), RespID(int), UserID(int)
I would like to insert a response into the ResponseTable where the Response doesn't exist, and then insert an entry into the EntryTable based on the RespID from the ResponseTable corresponding to the response.
How can this be done with the fewest statements?
EDIT:
Response is unique
The fewest statements from the front-end would be to use a stored procedure. Any way you do it, though, you will still need to have two INSERT statements. You just can't insert two different things with one query.
mysql supports TRIGGERS . you can add one to your ResponseTable that will activate "AFTER" "INSERT" and you can get it to use the values that are being inserted using the NEW key word.
the body of the trigger can be an insert into entry
something like:
CREATE TRIGGER `response_after_insert` AFTER INSERT ON `response`
FOR EACH ROW
BEGIN INSERT INTO entry SET RespID=NEW.RespID ON DUPLICATE KEY UPDATE operation=1;END;
So once you have your trigger set up, any time you do an insert on respnse, the trigger will get activated

MySQL Trigger cannot update table - getting ERROR 1442

I have the following trigger:
CREATE TRIGGER sum
AFTER INSERT
ON news
FOR EACH ROW
UPDATE news SET NEW.sum = (NEW.int_views + NEW.ext_views)/NEW.pageviews
It sums the int_views and ext_views column of a table and divides them by the total pageviews.
Whenever I try to add a new row to news, I get the following error:
ERROR 1442 (HY000) at line 3: Can't update table 'news' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
The trigger seems pretty simple to me. Is there a reason why the trigger fails to run?
The symptom is, that you are running an UPDATE (for all rows) inside a INSERT trigger - both modify the table, which is not allowed.
That said, if I guess the intention of your trigger correctly, you do not want to update all rows, but only the newly inserted row. You can achieve that easily with
CREATE TRIGGER sum
BEFORE INSERT
ON news
FOR EACH ROW
SET NEW.sum = (NEW.int_views + NEW.ext_views)/NEW.pageviews
Mind that this is a BEFORE INSERT trigger, as you want to change the row before it is written to the table.
If you try to update/insert on the same table that cause trigger to fire do not use the common sql command like
-> UPDATE TABLE_NAME SET COLUMN_NAME = VALUE WHERE CONDITION_LIST;
-> INSERT INTO TABLE_NAME VALUES("VALUE1","VALUE2");
This will not work. Only use set to assign the value of the column you update.
Example:
CREATE TRIGGER trigger_name BEFORE/AFTER INSERT/UPDATE ON table_name
FOR EACH ROW
SET NEW.COLUMN_NAME = "VALUE";
I was in a similar condition where I had to run two triggers:
UPDATE 3 fields on INSERTing a new ROW
UPDATE 3 fields on UPDATEing a ROW
After lot of efforts, I was finally able to write the TRIGGER in following way:
FOR updating values on INSERT
CREATE TRIGGER `INSERT_DISCOUNT_SERVICES` BEFORE INSERT ON `services`
FOR EACH ROW SET
NEW.discount_5_rate = (NEW.ndis_rate*0.05),
NEW.discount_10_rate=(NEW.ndis_rate*0.10),
NEW.discount_15_rate=(NEW.ndis_rate*0.15)
Similarly
FOR updating values on UPDATE
CREATE TRIGGER `UPDATE_DISCOUNTS_SERVICES` BEFORE UPDATE ON `services`
FOR EACH ROW SET
NEW.discount_5_rate = (NEW.ndis_rate*0.05),
NEW.discount_10_rate=(NEW.ndis_rate*0.10),
NEW.discount_15_rate=(NEW.ndis_rate*0.15)