Move data from one table to another using on update trigger - mysql

I am new to DB development. Please help me create a trigger for moving data from one table to another.
I have two tables, one contains "Transaction Status" from where I want to move records on transaction status change into another table having completed transactions. So the value in one table will get deleted and will get inserted into another table.
Please correct me in the following trigger:
create trigger transaction_state after update on test_archive for each row begin
insert into test_me(select * from test_archive where new.Transaction_status = 2);
delete from test_archive where new.Transaction_status = 2;
end;

Why do I feel like I am helping you with homework? Your trigger, as written, will probably move ALL rows when someone updates a row to Transaction_Status=2. Since you didn't join the NEW table to the test_archive table, your WHERE clauses will be true for all rows.
if you really want all rows with Transaction_status=2 moved from test_archive to test_me, then get rid of the FOR EACH and the references to the NEW table.
create trigger transaction_state after update on test_archive
begin
insert into test_me
select * from test_archive where Transaction_status = 2;
delete from test_archive where Transaction_status = 2;
end;

Related

How to use trigger to update only one row in table with Mysql

I have an after insert trigger that is supposed to update the field total in my table "test" where the id_cart is equal to new.id_cart. However my trigger is updating every single row in the table not only the one desired. I would like to know how can I modify my trigger so it only updates the row that I want.
This is my trigger.
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW BEGIN
UPDATE test set total= (select sum(price_product) from test_product_quantity_cart where id_cart=new.id_cart);
END
So if the new row inserted in table "test_product_quantity_cart" has an new.id_cart=1, then only the row in table "test" with id_cart=1 should be uptated.
I think I am missing a "where" clause to indicate the update statement which rows it is suppossed to upate. However I do not know how to add that clause.
Thank you!
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW
UPDATE test
JOIN ( SELECT id_cart, SUM(price_product) total
FROM test_product_quantity_cart
WHERE id_cart=NEW.id_cart ) value_for_update USING (id_cart)
SET test.total = value_for_update.total;

MYSQL Trigger not adding count after inserting rows into another table

This is a continuation of this question: Insert data into a table with a foreign key SQL
I need to create a trigger that updates a counter variable after I insert a row into a table. The counter variable keeps track of how many new rows are inserted into the ItemBook table. However, after running the script, the trigger doesn't seem to be working. Also how can I reuse this trigger if I have created it already?
CREATE TABLE count (
countBook INT DEFAULT 0 NOT NULL,
);
CREATE
TRIGGER count_trigger AFTER INSERT
ON ItemBook
FOR EACH ROW
UPDATE count
SET countBook = (SELECT COUNT(*) FROM Itembook)
INSERT INTO Item
VALUES('Clippers','amazon.com', 'hair clippers');
SET SQL_SAFE_UPDATES = 0;
INSERT INTO ItemBook
VALUES('Clippers','Bob')
SET SQL_SAFE_UPDATES = 1;
SELECT * FROM count;
The countBook column returns nothing after running the script. Furthermore, if I try to rerun this script again it will say Trigger already exists (after creating it). How do I reuse this trigger?
The trigger would look something like this:
CREATE TRIGGER count_trigger AFTER INSERT ON ItemBook
FOR EACH ROW
BEGIN
UPDATE count
SET countBook = countBook + 1;
END;
It seems very curious to have a table with one row for this information. But this appear to be what you are trying to do.

What is MySQL's correct syntax for before delete trigger?

I'd like to create a before delete trigger that deletes rows from two different tables. But I can't figure out which parameters to use.
I got a house table, and when I delete a row, I'd like to delete every row in my two other tables: user_house and firm_house, which contains same house id as the one triggering the event.
What does FOR EACH ROW mean? And how can I properly set my trigger up?
USE `mydb`;
DELIMITER $$
CREATE TRIGGER `deleteUnions` BEFORE DELETE ON `house`
FOR EACH ROW
BEGIN
DELETE FROM user_house WHERE ?? = ??;
DELETE FROM firm_house WHERE ?? = ??;
END
Some details about the structure:
user_house is joined by user_id and house_id;
firm_houise is joined by firm_id and house_id.
Refer to the record that gets deleted in the trigger with OLD. Then use the id to delete from the other tables.
DELETE FROM user_house WHERE house_id = OLD.house_id;
DELETE FROM firm_house WHERE house_id = OLD.house_id;

After trigger to update several row in same table

Currently I'm trying to create a trigger that when the trigger fired it will update several record in same table.
After Insert TA
begin
update TA
set K = "W"
where Z = NEW.Z;
end;
But the trigger will always stop the insertion, then I found some posts and they say it is because of table being locked. When I try to unlock the table but this cannot be done in the trigger.
currently I just split the table to 1:1 relation for this problem, are there any others way you can do this?

INSTEAD OF UPDATE,DELETE,INSERT : determine each command

I need determine each of this this 3 commands in trigger [UPDATE,DELETE,INSERT].For last 2 I do next:
IF EXISTS (SELECT * FROM inserted)
BEGIN
END
ELSE IF EXISTS (SELECT * FROM deleted)
BEGIN
END
How can I get updating rows?
Thanks.
Not exactly sure what you're trying to accomplish, but you can test if it's an UPDATE if both inserted (values after update) and deleted (values before update) exist. From the documentation:
The deleted table stores copies of the affected rows during DELETE
and UPDATE statements. During the execution of a DELETE or UPDATE
statement, rows are deleted from the trigger table and transferred to
the deleted table. The deleted table and the trigger table ordinarily
have no rows in common.
The inserted table stores copies of the affected rows during
INSERT and UPDATE statements. During an insert or update
transaction, new rows are added to both the inserted table and the
trigger table. The rows in the inserted table are copies of the new
rows in the trigger table.
Thus, if inserted exists but not deleted, it's an INSERT; if deleted exists but not inserted, it's a DELETE; if they both exist, it's an UPDATE.