This is my problem:
Create a trigger which will add the id and age columns of rows deleted
from the table "metadata" to the "metadata2" table.
The query that will be run to check if the trigger works is:
DELETE FROM metadata WHERE ID=40124197; SELECT * FROM metadata2.
So, what did I do? I did this:
CREATE TRIGGER somerandomtrigger
AFTER INSERT ON metadata2
FOR EACH ROW
BEGIN
INSERT INTO metadata2 (id, age)
END
Yet it keeps saying: Query failed: near "END": syntax error, but I don't get why.
It seems you used the wrong event trigger on the worng table.
When you delete a record from metadata table, you want to insert some data from this record into metadata2.
On delete trigger, there is a a records managed by MySQL named OLD containing the record that has been deleted.
CREATE TRIGGER somerandomtrigger
AFTER DELETE ON metadata
FOR EACH ROW
BEGIN
INSERT INTO metadata2(id, age) VALUES (OLD.id, OLD.age);
END;
Syntax and examples of MySQL trigger
Related
I have created a table test1 as
CREATE TABLE test1
(id INT,
name VARCHAR(20))
ENGINE=INNODB;
Now I want to delete a record from this table before inserting a new record having the same id(that is,record already existing with same id,then insert the new record) .I tried the code give below.
create trigger before_insert_test1 before insert
on test1
for each row
begin
delete from test1 where id=NEW.id;
end;
but it shows error
ERROR 1442 (HY000): Can't update table 'test1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Can anyone help.
thanks in advance
I don't think you can update/delete from the same table that you're inserting into. The table is being locked by the insert statement, even though you specified after insert. The insert transaction is not yet completed.
My suggestion would be to do a test and delete the record before you do the insert, or use the MySQL ON DUPLICATE KEY UPDATE statement if it applies
create trigger before_insert_test1 before insert on test1
DELETE FROM test1 WHERE id=NEW.id;
end;
You shan't need for loop, delete statement will delete every record with given id.
I'm using Mysql in phpMyAdmin,where i have to delete a entry from tableA if i insert a row with same primary key.I thought to do it in trigger of tableA BEFORE INSERT
For Ex,
if the tableA contains
1 Hai Hello
here 1 is the primary key
And now if i insert a row 1 Bye Hello then the trigger BEFORE INSERT will delete the old entry and then the new row (2nd) will be inserted. But Mysql has restriction of not being able to update a table inside a trigger defined for that same table.
It gives the error
#1442 - Can't update table 'tableA' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
So i changed my way, i called a procedure from trigger BEFORE INSERT of tableA and in that procedure i do the task what i thought to do in trigger. But unfortunately i'm getting the same error.
In trigger BEFORE INSERT i simply called the procedure as
CALL proce1(new.Reg_No);
In procedure i have done this
DECLARE toup integer;
select count(*) into toup from tableA where Reg_No=reg;/*Here Reg_No is primary key */
if toup > 0 then
delete from tableA where Reg_No=reg;
end if;
Need some other Idea to achieve this. Help Me.....
I don't like to use triggers so much because they are hard to manage somethimes. Also they will cause you a downgrade in performance. I am not against trigger as they can be handy in some cases.
In your case have you though of using REPLACE(....) or INSERT INTO .... ON DUPLICATE KEY UPDATE or even INSERT INTO IGNORE .....
REPLACE(....) will delete a record if a record is found and insert a new one with the same ID.
INSERT INTO .... ON DUPLICATE KEY UPDATE will allow you to override existing field if a duplicate is found.
INSERT INTO IGNORE ..... will allow you to ignore the new inserted row if one already exists
Since you mentioned in the comments that you are importing records from a file, then try to use LOAD DATA INFILE logic which will allow you to REPLACE field on duplicate
LOAD DATA LOCAL INFILE 'x3export.txt'
REPLACE INTO TABLE x3export
I want to see if can I use mysql to manage a little stock.
table A contain all the movements:
art_code, qty_load, qty_unload, date
table B with the existence:
art_code, total_load, total_unload, available, date
I've created a trigger: (after update on)
INSERT INTO STOCK VALUES(NEW.ART_CODE, TOTAL_LOAD, TOTAL_UNLOAD, TOTAL_LOAD-TOTAL_UNLOAD, NOW());
but after the first correct run it says a row already exists, how could I replace the old row with the new row?
You can use INSERT ... ON DUPLICATE KEY UPDATE command. It will help you to insert and to update existed records using one statement.
INSERT ... ON DUPLICATE KEY UPDATE Syntax
My table has the following columns: type, how, what, index
I need a trigger so when I insert a new record containing:
--TYPE--HOW--WHAT--INDEX--
INSERT INTO table VALUES(10, 'OP', 'IP', -1);
If there is already another record with WHAT = 'IP' then trigger should delete the previous record and continue inserting. Is this possible to do in MySQL?
I have tried to design my trigger using the Navicat but everytime I have ended with an exception showing up.
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)