modify the table from its own trigger in Mysql- PhpMyAdmin - mysql

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

Related

Error when Updating a Table with Trigger

I have a trigger setup that every time a new table is created, it reads the ID (which is in varchar) and converts it to integer and stores it in another column.
However when I run it I encounter the following error:
Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
This is my trigger:
CREATE TRIGGER `var_to_int` AFTER INSERT ON `products`
FOR EACH ROW UPDATE `products`
SET `int_id`= CAST(`var_id` AS INT)
WHERE `int_id` IS NULL OR `int_id` = ''
How do I prevent this?
This error is thrown by the PHP script creating a new item in the table.
You can not update the table itself.
Trigger starts when it is UPDATE, and then it has to do UPDATE. An endless loop is formed. Try using an additional table to store temporary data to later update the correct table

How can I trigger this SQL query the right way?

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

MySQL Trigger, Before Insert: Update if already exists

I have an application, which cannot update rows, only insert them, but If something is already inserted into the database and it tries to insert a row, it generates a duplicate key error.
I tried to create a trigger for the underlying database table, so If it tries to insert a row that exists, it changes to update instead.
Something like this:
CREATE DEFINER = CURRENT_USER TRIGGER `Test`.`Job_BEFORE_INSERT` BEFORE INSERT ON `Job` FOR EACH ROW
BEGIN
INSERT INTO Job(id, createdAt, updatedAt, `name`,
version, ...)
VALUES(NEW.id, NEW.createdAt, NEW.updatedAt, NEW.name,
NEW.version, ...)
ON DUPLICATE KEY UPDATE
updatedAt=CURRENT_TIMESTAMP, `name`=VALUES(`name`), version=VALUES(version), ...;
END
But it generates an error on the first insert:
Error Code: 1442. Can't update table 'job' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I read that you can't change the same table in a trigger or a stored procedure. Am I trying to make something impossible? Changing an insert into to an update if the row is existing?

delete a record before inserting a new record into a table using trigger in mysql

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.

MySQL - Trigger to delete previous record

I'm trying to create a trigger that will allow only one record in a database, so it would delete any previous records.
But currently, it doesn't allow me to insert anything it, because it's instantly deleted.
DELIMITER $$
CREATE TRIGGER test_insert
BEFORE INSERT ON test
FOR EACH ROW BEGIN
DELETE FROM test WHERE id = NEW.id - 1;
END$$
How would I delete a previously (or all previous) inserted record?
"But currently, it doesn't allow me to insert anything it, because it's instantly deleted."
Acutally, when you do an INSERT, the execution of your trigger should be throwing exception:
Error Code: 1442
Can't update table 'test' in stored function/trigger because it is
already used by statement which invoked this stored function/trigger.
(Unless something is radically different in a newer version of MySQL.)
The operation you want to perform (i.e. deleting rows from the same table you are inserting into) cannot be done in a MySQL trigger.
You could use a combination of a UNIQUE KEY and a BEFORE INSERT trigger to prevent more than one row from being inserted. The BEFORE INSERT trigger could set the value of the column that has a unique
key on it to be a static value, then the INSERT statement would throw a duplicate key ("Duplicate entry") exception.
Then, you could use an INSERT ... ON DUPLICATE KEY UPDATE ... statement to update the values of columns other than the unique id, e.g.
CREATE TRIGGER `test_insert` BEFORE INSERT ON test
FOR EACH ROW BEGIN
SET NEW.id := 1;
END
ALTER TABLE test ADD UNIQUE KEY (id);
INSERT INTO test (somecol) VALUES ('someval')
ON DUPLICATE KEY UPDATE somecol = VALUES(somecol) ;
To do that you first need to find some value at which you want to delete all and insert for. This way you don't need a trigger, you simply delete all previous cases and then add a new row. Unless for some reason unexplained from your code you would need a trigger a simple solution in your loop could work Like:
$query = mysql_query("DELETE FROM test WHERE id = NEW.id -1");
$new_id = $new.id -1;
$query2 = mysql_query("INSERT INTO test VALUES('$new_id','$var1','$var2'));