MySQL trigger not working. What could be the issue? - mysql

I need to update an existing table after insert on another table. This is what I have.
DELIMITER $$
CREATE TRIGGER `some_trigger`
AFTER INSERT ON `old_table`
FOR EACH ROW BEGIN
UPDATE `new_table` set `some_column` = new.`column`
WHERE `new_table`.id = new.id
END $$
DELIMITER;
Trigger definition successfully executed and trigger exists with
SQL_MODE - NO_ENGINE_SUBSTITUTION
DEFINER - root#%
Is there something horribly wrong with this?

Can you please confirm that the new.id i.e the id generated on old_table insertion is also present in the new table ?
Because the possible reason could be that in new table new.id is not present at the time on old_table insertion.

Related

How to UPDATE ALL OLD ROWS in A TABLE with AFTER/BEFORE INSERT ON trigger IN THE SAME TABLE in mysql?

I need to update the status column of all the old rows of a table to false before/after inserting a new row into that table.
I have visited these but didn't solve my query.
MySQL - Trigger for updating same table after insert
DELIMITER $$
DROP TRIGGER update_old_status $$
CREATE TRIGGER update_old_status
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
DECLARE MYCOUNTER INT;
SELECT COUNT(*) INTO MYCOUNTER FROM table_name;
IF (MYCOUNTER > 0) THEN
UPDATE table_name SET status = false WHERE status = true;
END IF;
END $$
DELIMITER ;
Here what i have as error message:
Caused by: java.sql.SQLException: Can't update table 'table_name' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
You can't use a trigger to update the same table for which the trigger is running, because there's too high a risk of creating an infinite loop.
For example, an insert trigger updates its table, which runs another trigger on update, that inserts to the same table, which runs the insert trigger...
You can't and don't need to use a trigger for this. Instead, use a transaction and run the UPDATE before your INSERT in your application.
Pseudocode:
START TRANSACTION;
UPDATE table_name SET status = false;
INSERT INTO table_name ...values...
COMMIT;

Updating after delete | mysql trigger

I have two tables for example table1 and table2. If something is deleted in table1 i want to update a column in table2. Is this even possible with a trigger in phpmyadmin? if yes what do i have to add or which syntax i have to use for it to work?
I tried this so far:
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
-- this is the part i dont know what to do and i couldnt find any related to my task
END//
DELIMITER ;
Well, in your TRIGGER you can access the value you just deleted with OLD.your_column_name.
So just do :
DELIMITER //
CREATE TRIGGER `update` AFTER DELETE ON table1
FOR EACH ROW BEGIN
UPDATE table2 SET column_name = your_new_value WHERE column_name = OLD.old_value;
END//
DELIMITER ;

Creating Last Modified SQL Trigger

I am trying to create a trigger to update last_modified field when the row is updated.
This is what i have tried:
USE `mydb`;
DELIMITER $$
DROP TRIGGER IF EXISTS mydb.products_AUPD$$
USE `mydb`$$
CREATE TRIGGER `products_AUPD` AFTER UPDATE ON products
FOR EACH ROW BEGIN
SET NEW.`last_modified` = NOW();
END;
$$
DELIMITER ;
The problem is MySQL workbench wont let me save it (I'm assuming that there is something wrong with it but i can't find out what)
Turns out the version of MySQL Workbench I was using(6.0xxx) had a bug in it which played havoc with triggers. I updated to 6.2xxx.
That was the first problem.
The second problem was that i was using the NEW keyword in an AFTER trigger which is not allowed:
Within the trigger body, you can refer to columns in the subject table
(the table associated with the trigger) by using the aliases OLD and
NEW. OLD.col_name refers to a column of an existing row before it is
updated or deleted. NEW.col_name refers to the column of a new row to
be inserted or an existing row after it is updated.
So my new trigger looks like this:
CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`products_BEFORE_UPDATE` BEFORE UPDATE ON `products` FOR EACH ROW
BEGIN
SET NEW.`last_modified` = NOW();
END;
Notice there is no USE or DELIMITER statement? That is because the new version of MySQL Workbench puts this in for you (including the end delimiter $$):
USE `mydb`;
DELIMITER $$
USE `mydb`$$
DROP TRIGGER IF EXISTS `mydb`.`products_BEFORE_INSERT` $$
USE `mydb`$$
CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`products_BEFORE_INSERT` BEFORE INSERT ON `products` FOR EACH ROW
BEGIN
SET NEW.`last_modified` = NOW();
END;$$

Delete the same row inserted in a table by trigger in mysql

I'm trying to remove a row after inserted in a temp table and I get this error:
INSERT INTO `temp_program_counter` (`id`, `program_id`) values (NULL, '275')
Can't update table 'temp_program_counter' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
And here is my trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` AFTER INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
this table is a temp table that get's data with delay and after receiving must update the main table and be deleted from temp.
thanks for helping in advance.
You probably have already resolved this in some way but I'll post the answer anyway:
You cannot refer to the same table that triggered the trigger in a trigger (or a combination of trigger and stored procedure). This could mean an infinite loop, so sql prevents it by giving that error, resulting in always the same error.
Try BEFORE INSERT
Because, you are trying to delete a record which has just been inserted but may be not yet committed in this session. Hence you are getting the error.
If you use BEFORE INSERT, then all other record in the table with id=new.id would be deleted and the new record will be inserted successfully.
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` BEFORE INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;

mysql create trigger ON INSERT

Suppose I have the following table
create table try ( name varchar(10), username varchar(10))
Is it possible to have a trigger 'ON INSERT' to set username=user() ?
I tried doing
create trigger userIns ON INSERT on try for each row set new.username=user();
but this gives some syntax error
I have seen documentation about BEFORE INSERT,AFTER INSERT. How about ON INSERT?
if not, whats my best bet?
The syntax is :
CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
...
END
Your syntax is wrong in trigger_time part.
Trigger activation time can be BEFORE or AFTER. You must specify the
activation time when you define a trigger. You use BEFORE keyword if
you want to process action prior to the change is made on the table
and AFTER if you need to process action after change are made.
Try this :
create trigger userIns BEFORE INSERT on try for each row set new.username=user();
More : MYSQL Trigger Tutorial
Correct Syntax is
Create Trigger trigger_name trigger_time trigger_event
on table_name
for each row
begin
....
end
Trigger_time => Before | After
trigger_event => Insert | Update | Delete
Please check
create trigger userIns INSERT on try for each row begin set new.username=user() end;
None of the above was my problem. I was really looking for an ON INSERT trigger - but it does not exist in MYSQL. So I used BEFORE INSERT and that works fine
create trigger userIns BEFORE INSERT on try for each row set new.CREATED_BY=user();
The problem is you need to set the delimiter to something other than ';' before you begin the trigger code. Put the new delimiter after 'end' and then change the delimiter back to ';' after you are done.