sql trigger-how to trigger an insert that update another table - mysql

I have two tables named CustomerGoods(with column quantity) and Stock(with column Availablequantity). I want a trigger to occur when I insert into the CustomerGoods so that the quantity is added into Availablequantity in the Stock table.

You have not provided proper and to the point problem, but as per my understanding from what you have written.
Please try this.
CREATE
TRIGGER `AvailablequantityTrig` AFTER INSERT ON `CustomerGoods`
FOR EACH ROW BEGIN
UPDATE Availablequantity set quantity=quantity+1 where GoodsID = NEW.id;
END;
Here NEW.id is the id of goods and GoodsID is the id of goods in Availability table..
Please change your column names as required..
Thanks.

Related

Trigger AFTER UPDATE for effected rows only

My Prestashop table ps_stock available has a column called quantity where the stock of the product is stored. This table is updated by an app, changing the values of the quantity column.
I need a trigger in that table. Every time the quantity of a product is changed I need to extract which products are effected on that table to insert them in another table
I have tried a trigger like this:
IF (OLD.quantity <> NEW.quantity)
THEN
INSERT IGNORE INTO ps_ebay_product_modified (id_ebay_product_modified,id_ebay_profile,id_product)
SELECT ps_ebay_product.id_ebay_product,id_ebay_profile,id_product
FROM ps_ebay_product INNER JOIN ps_stock_available
WHERE OLD.quantity <> NEW.quantity;
END IF
What I need is a selection of the products that have been changed.
This should work
CREATE TRIGGER mytrigger AFTER UPDATE ON ps_stock
FOR EACH ROW
BEGIN
-- do some stuff here
END;

Update mysql rows with formula having different values for each row

I have two tables:
Table 1 : parts
id (primary key)
code
title
quantity
Table 2 : bill_items
id (primary key)
bill_id
parts_id: refers to primary key of table parts
qty
I would like to update parts table - qty every time I create a row in table bill_items. The qty in parts table is to be decremented by qty of bill_items. There may be N number of updates to table bill_items in one go. Would like to use a single INSERT....ON DUPLICATE or UPDATE statement.
Thank you for your time.
I think for this case better using trigger :
DELIMITER $$
CREATE
TRIGGER `bill_items_after_insert` AFTER INSERT
ON `bill_items`
FOR EACH ROW BEGIN
UPDATE parts set quantity = quantity - NEW.qty WHERE id = NEW.parts_id;
END$$
DELIMITER ;
I suggest you also make trigger for UPDATE and DELETE also for data consistency.
UPDATED
Based on your comment, it is possible to use normal insert and update using transaction for consistency data :
START TRANSACTION;
INSERT INTO bill_items (bill_id, parts_id, qty) VALUES (your_bill_id,your_parts_id,your_qty);
UPDATE parts SET quantity = quantity - your_qty WHERE id = your_parts_id;
COMMIT;

MySQL - Trigger that will automatic insert on other table

How can I create a trigger that will insert data into another table.
For example: I have a department table and a course table and the course table has a foreign key named department_id.
I insert CICCT into the course table but there is no CICCT in the department table.
My goal is to make the trigger add the value in the department table. How can I do this?
I tried this, is this correct?
CREATE TRIGGER department_insert BEFORE INSERT
ON course
FOR EACH row
begin
INSERT INTO department
(department_id,
description)
VALUES (new.id_dep,
NULL);
end;
Another question in the same subject, what do I need to do in order reduce the room capacity by one when a student enrolls?
Your trigger might look like this
CREATE TRIGGER department_insert
BEFORE INSERT ON course
FOR EACH ROW
INSERT IGNORE INTO department (department_id, description)
VALUES (NEW.id_dep, NULL);
Note:
to access column values of a row being inserted you have to use the NEW keyword
NEW.id_dep
^^^
use IGNORE clause in INSERT to ignore department_id values if they already exist in department table. It allows to greatly streamline your code.
Here is SQLFiddle demo
Make a separate post for your second question and specify table schemas

In a trigger how to insert values into another table but check if values already exists

Based on my previous post found here im able to insert the values to the 2nd table when the status on first table changes, but it keeps adding indiscriminately, i need to check if the submit_id has already been inserted into the 2nd table and then update the fields not insert it gain, how would i do that check before the trigger is executed?
Because the new.status and old.status refer to the row being edited not the row on table it's being inserted into, how can i compare that and insert or update if it already exists,
Thanks
You can use INSERT INTO ... ON DUPLICATE KEY UPDATE syntax for that
If order for it to work properly you have to have a UNIQUE constraint on submitId column in your second table (let's call it students).
ALTER TABLE students ADD UNIQUE (submitId);
Now an improved version of a trigger
DELIMITER $$
CREATE TRIGGER tg_au_submissions
AFTER UPDATE ON submissions
FOR EACH ROW
BEGIN
IF NEW.status = 1 THEN
INSERT INTO students (submitId, studentName, submitDate, contacts, email)
VALUES (NEW.submitId, NEW.studentName, NEW.submitDate, NEW.contacts, NEW.email)
ON DUPLICATE KEY UPDATE
studentName = VALUES(studentName),
submitDate = VALUES(submitDate),
contacts = VALUES(contacts),
email = VALUES(email);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo

Mysql Trigger update another table's quantity by 1

Hi I'm trying to create a trigger that adds 1 to the quantity of insert's id in a helper table holding the codes for the id's. Here are my 2 tables.
Animal
----------
AnimalId
Breedcode
Breed_codes
------------
Breedcode
breed
Quantity
CREATE TRIGGER `updater` AFTER INSERT ON `Animal`
FOR EACH ROW UPDATE breed_codes
SET Quantity = Quantity + 1
WHERE Breedcode = Animal.Breedcode
However it states that the Animal Breed code isn't found. Can anyone please help me fix my trigger. Thank you
In your UPDATE query you can use:
Column names from the table(s) you are updating,
Column names from the table you define trigger for with NEW.column (insert and update triggers) or OLD.column (update and delete triggers).
In your case instead of Animal.Breedcode you should use New.Breedcode.