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.
Related
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;
My current project doesn't use any database migrations and so if any changes arise they are found manualy.
Is it possible to find out which tables in a database have new rows that were created in the last n days (for example 7 days)?
Thanks for any suggestions!
Create a Trigger for insert to make a value in the table for 1 as newly inserted or 0 it is inserted before .... its a flag that shows that a new value is added ... and then you can make an update statement to make sure that they are all take 0 value ... so that every time you insert in the table you add this column with 1
Example :
id StudentName flag
1 Frank 0
2 Andrew 1
insert into tablename(id,StudentName,flag) Values(3,'Alice',1)
always make the flag with 1 to know that it is a new record and it is recently added.
update tablename set flag = 0
this statment to make sure that all is inserted before and it is not new !!
Create A common table Which shows when table Update
Create table TableUpdatedRrecord (Table_Name varchar(max),Flag int,TableCreatedDate DateTime,Modified Date)
First Create table Which Has Record when New Rows were Inserted in table
Create table TableUpdatedRrecord (Table_Name varchar(max),Flag INT,TableCreatedDate DateTime,Modified DatetIME)
Now Create a Trigger for each table which is executed when table has a new insert
CREATE TRIGGER TriggerName
ON dbo.TableName
AFTER INSERT AS
BEGIN
DECLARE #CreatedDate datetime
Set #CreatedDate=(Select t.create_date from sys.tables t where name='TableName')
INSERT INTO TableUpdatedRrecord
(Table_Name,[Flag],[TableCreatedDate],[Modified])
VALUES
('TableName'
,1
,#CreatedDate
,GetDate())
END
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.
I have two table:
Am using this query to populate my marks table with all 'id' in student table:
$module1= "insert into marks (stud_id,moduleID)
select sid, 1
from user_student
where sid not in (
select * from (
select distinct stud_id from marks where moduleID=1
) alias_name
)";
So i need to take all 'sid' from Table student and populate them into table marks my query above does all that. The problem am encountering is that every time i make a change to the data e.g column test1 , new record are inserted again.
If i populate the marks table manually, Can't i have a code that when new data is available in student table, the data is just updated in marks table...
I don't understand some points of your insert query, why insert sid in stud_id if there's a id in student table to relate them?
Maybe solve your problem the creation of a composed unique key to moduleID and stud_id and use a REPLACE in place of INSERT.
But the right way to do it is using a TRIGGER like:
DROP TRIGGER IF EXISTS `student_after_insert`;
CREATE TRIGGER `student_after_insert` AFTER INSERT ON `student`
FOR EACH ROW INSERT INTO marks (stud_id,moduleID)
SELECT NEW.`id`, `modules_table`.`id` FROM `modules_table`;
PS: I suppose you have a table of modules with name modules_table. Change it accordingly.
I haven't understand your question at the best, my tip is:
Make a unique constraint on marks table (up to you is the choice of best constraint rule).
Then use a trigger to make the right insert/update:
Something like that, (hope that syntax is right):
CREATE TRIGGER myTrigger
AFTER INSERT ON students
FOR EACH ROW
BEGIN
// check that insertion doesn't broke the constraint previously define, then
insert into marks (stud_id,moduleID) values (new.sid, 1);
END
Note: NEW is the row that you have as soon inserted/updated on students. Similar you have OLD metarow.
I have to table named 'stuff' and 'given'. I wanted to add one more columnt to 'stuff' and after ALTER TABLE i used UPDATE. However this makes difference for only that moment. I mean, m new column will give the amount of stock after each given amount. 'stuff' has 'totalAmount' and given has 'amount'.
I used:
ALTER TABLE stuff ADD stock MEDIUMUNINT UNSIGNED;
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
This works for only that UPDATE. How can i make these two table so synchronized after each given amount, the stock will be affected either?
Thanks in advance :)
Create an trigger on INSERT for the table "given" which will update the "stuff" in table "stuff"
CREATE TRIGGER [TRIGGER_ALTER_STUFF] ON [dbo].[given]
FOR INSERT, UPDATE
AS
BEGIN
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
END