Updating after delete | mysql trigger - mysql

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 ;

Related

How to update a table in different database with data from current table?

I have two table in two separate databases. However, they are supposed to have the same data. I would like to make sure that whenever I make changes to data in table_a from database_a, they get reflected in table_b from database_b.
Is there any MySQL command that I can run to achieve this?
I read this question: Copy Data from a table in one Database to another separate database but it seems to insert data instead of updating it.
Thanks.
The best way to accomplish this would be with triggers. I haven't tested this, but it gives you the idea.
DELIMITER $$
CREATE
TRIGGER table_a_after_insert AFTER INSERT
ON database_a.table_a
FOR EACH ROW BEGIN
-- update to match columns in your table
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END$$
CREATE
TRIGGER table_a_after_update AFTER UPDATE
ON database_a.table_a
FOR EACH ROW BEGIN
DECLARE updated_rows INT;
-- again update the column list to match your table
UPDATE database_b.table_b
SET
name = NEW.name
WHERE id = NEW.id;
-- in case the row didn't already exist in table_b, insert it
SET #updated_rows = ROW_COUNT();
IF updated_rows = 0
THEN
INSERT INTO database_b.table_b (
id,
name
)
VALUES (
NEW.id,
NEW.name
);
END IF;
END$$
CREATE
TRIGGER table_a_after_delete AFTER DELETE
ON database_a.table_a
FOR EACH ROW BEGIN
-- obviously update the column list to match the columns in your table
DELETE FROM database_b.table_b
WHERE id = OLD.id;
END$$
You'll have to make sure the user has the right privileges to database_b.table_b
You can use Database triggers (https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html) for something like this.
CREATE TRIGGER triggername AFTER INSERT ON database_a.table_a
FOR EACH ROW
BEGIN
INSERT INTO database_b.table_b (id, ...) VALUES (NEW.id, ...);
END;
You will however have to create triggers for each event. When inserting into table_a you need to insert into table_b, when deleting from table_a you will have to delete from table_b and so on.
€dit: Update for instance could look like this:
CREATE TRIGGER triggername AFTER UPDATE ON database_a.table_a FOR EACH ROW
UPDATE TABLE database_b.table_b
SET table_b.id = NEW.id,
...(SET each column here)...
WHERE table_b.id = NEW.id;

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;

MySQL trigger not working. What could be the issue?

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.

MySQL trigger issue with if clause

I'have a problem trying to create a trigger. I have table_1 and table_2, the update event on table_1 should update a row in table_2 identified using the where clause:
CREATE DEFINER=`root`#`localhost` TRIGGER `update_table_2` AFTER UPDATE ON `table_1` FOR EACH ROW BEGIN
IF table_1.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;
END
What's my problem?!
Thanks
In a trigger, you should use the old and new keywords to reference the record that has been updated (the old values and new ones respectively).
In your case, you should do :
IF new.key=0 THEN
UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';
END IF;

MySQL Trigger to create a row in a different table

I know there must be a simple way to do this but I couldn't find one. I want to create a trigger that basically, when a row is created in table1, creates a new row in table2 with a foreign key of the id of table1. what is the general syntax for this? thanks!!
CREATE TRIGGER Syntax
For example -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER INSERT
ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2(id) VALUES(NEW.id);
END$$
DELIMITER ;
something like this:
CREATE TRIGGER `create_t1` AFTER INSERT ON `table1` FOR EACH ROW BEGIN
INSERT INTO table2
SET t1ID = NEW.ID,
when = Now();
END;