How to structure this IF condition in MySQL trigger - mysql

I am trying to write a MySQL trigger. I have two tables that look like this:
When a customer makes a purchase a new record is added to each table. I have added column ‘sku_copy’ to Table B, so it does not get populated when a new record is created.
When a new record is created, I want my trigger to copy the ‘sku’ field in Table A to the ‘sku_copy’ field in Table B. However, the problem I am having is how to structure the following condition in the trigger.
IF: ‘order_id’ in Table A matches ‘order_id’ in Table B. THEN: copy ‘sku’ from that Table A record to the record in Table B with the matching ‘order_id’. The data should be added to Table B ‘sku_copy'.
ELSE: don’t do anything.
Can someone show me how to write this into my trigger?
Thanks for any help you can give.

Try this but why do u think of using trigger for this ?
DELIMITER $$
CREATE TRIGGER trigger_name
AFTER INSERT ON tableA
FOR EACH ROW BEGIN
INSERT INTO tableB
SET sku_copy = OLD.sku,
order_id = OLD.order_id,
order = OLD.order;
END$$
DELIMITER ;

I want to post the trigger that was constructed with examples offered here and on another forum. Thanks to everyone who helped with this.
DELIMITER $$
CREATE TRIGGER sku_AfterInsert
AFTER INSERT ON uau3h_virtuemart_order_items
FOR EACH ROW BEGIN
UPDATE uau3h_virtuemart_orders
SET order_item_sku_copy = NEW.order_item_sku
WHERE virtuemart_order_id = NEW.virtuemart_order_id;
END$$;
DELIMITER ;

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;

SQL trigger for add some data

Thank you for your help.
I have 3 tables on MySQL. One Table for my users and second is for my user features, the third one is for roles. First of all, I need to add registration on a measures table when the user is inserted. I mean if I add a new user to tbl_users the trigger will grab the user id and write on the tbl_measures that's it. I need just this.
INSERT INTO tbl_measures (user_id) VALUES (tbl_users.id)
this my triggers it is working partly. when I add a new user trigger add a new line to tbl_measures without id.
When you refer NEW.id in your insert trigger, there are two main cases. In the first case you get the proper id value. This is what you expect. In the second case it's null. This is what happens.
Essential is whether the trigger runs before or after the insert. Before the insert the id does not exist, because the record was not created yet. After the insert the id exists. Long story short code:
DELIMITER //
CREATE TRIGGER PROOFOFCONCEPT
AFTER INSERT
ON EACH ROW
BEGIN
INSERT INTO tbl_measures(user_id)
VALUES (NEW.id);
END; //
DELIMITER ;
I found the solution:
CREATE TRIGGER `after_members_insert` AFTER INSERT ON `tbl_users`
FOR EACH ROW BEGIN
INSERT INTO tbl_measures(user_id)
VALUES(new.id);
END

MySQL Trigger to Update a field in one table after inserting into another

Tables
I have the two tables above. In tblUniformAndMaterials the field AllocatedMaterials is populated from a drop down list which is fed by from tblMaterials. Once selected the field MaterialID in tblUniformAndMaterials is Auto Populated from tblMaterials.
What I want to accomplish by using a Trigger is after the record in tblUniformAndMaterials is inserted I want to update the NiveauDeStock field in tblMaterials to (NiveauDeStock-1). In other words after each material allocation I reduce the stock level by one.
Reference for CREATE TRIGGER https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
Try this:
DELIMITER $$
CREATE TRIGGER tblUniformAndMaterials_ai
AFTER INSERT
ON tblUniformAndMaterials
FOR EACH ROW
BEGIN
UPDATE tblMaterials
set NiveauDeStock = NiveauDeStock -1
WHERE CodeDeMaterial = NEW.MaterialID;
END;
$$
DELIMITER ;
Note AFTER INSERT and how NEW.MaterialID is the ID inserted into tblUniformAndMaterials.
Good Luck!

How to update to other table when inserted into one table in mysql

I have two tables in mysql. One is create_load_test and another isload_test. I am inserting data into create_load_test table via html form. It is get updating.
There are 9 columns in create_load_test and 3 columns in load_test.
I want to update id and load_test column values of 'load_test' table, When a row is inserted in create_load_test table via html form.
How to do this? I know only basics of mysql.
Can you help me to solve this?
create_load_test:
load_test table:
try something like this using trigger.
CREATE TRIGGER `after_update_A` AFTER UPDATE ON `A` FOR EACH ROW
BEGIN
UPDATE TABLE B
SET username = NEW.username
, password = NEW.password
, email = NEW.email
WHERE id = NEW.id;
END $$
I have used like this, now it is working fine.
DELIMITER $$
CREATE TRIGGER triggered_from_createload
BEFORE insert ON create_load_test
FOR EACH ROW
BEGIN
INSERT INTO load_test
SET load_test.id = new.id,
load_test.loadtest = new.loadtest;
END$$
DELIMITER ;

MySQL select statement result in after delete trigger always null

I'm having a little issue with a trigger in a MySQL database. I have a DB with two tables: "tasks" and "files". The "tasks" table have a field which is a foreign key of the primary key from the "files" table. It also sometimes may be null.
What I'm trying to acomplish is to delete in the first place a row in the "tasks" table, and after that delete the corresponding row in the "files" table using a trigger.
This is the trigger I'm using right now:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DECLARE fileId int;
SELECT file INTO fileId FROM tasks WHERE id=old.id;
DELETE FROM files WHERE id=fileId;
END;//
DELIMITER ;
The field "file" in the "tasks" table is the one containing the foreign key. In the examples I've been running, that field has never been null.
The problem is that the select statement always returns null. The delete statement that triggers this trigger goes fine, but the row in the "files" table is never deleted. I've tried to insert the "fieldId" variable on a testing table, and it's always saving a null value.
Is there any problem on that trigger? Maybe I'm trying to do something merely impossible?
All the help is much appreciated :)
Since it should be looping over each deleted row, why would this not work?
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files WHERE id=old.file;
END;//
DELIMITER ;
If that doesn't work, could try this:
DELIMITER //
CREATE TRIGGER after_delete_file AFTER DELETE ON tasks
FOR EACH ROW
BEGIN
DELETE FROM files INNER JOIN tasks ON files.id=tasks.file WHERE tasks.id=old.id;
END;//
DELIMITER ;
but I don't think that should be necessary.
AFTER delete means that the data is deleted, of course you can't find it. Try creating the trigger for BEFORE delete.
You could also more carefully use all of the old values rather than selecting from the table that was deleted from.