Phpmyadmin Database Trigger Creation Query - mysql

I have two tables inventory table(columns 'inventory_id','asset','item','delivered_by', 'received_by', 'quantity ''date') and a summary table ('stock_id', 'asset', 'item', 'qty'). I want to create a database trigger that sums up the quantity of assets and items from the inventory table into the summary table after insertion of a new row in the inventory table. .
An Instance:
Expectation From trigger
Here is a query i tried to create the trigger but it failed
CREATE TRIGGER updateqty
AFTER INSERT ON inventory
FOR EACH ROW
BEGIN
UPDATE INTO stock SET qty = (old.qty + stock.qty)
WHERE inventory.item = summary.item_name OR summary.asset_name
Any ideas as to how to create the trigger for my scenario would be very much appreciated. Thanks in advance.

There is no such thing as update into, there is no such thing as old. in an insert trigger,the where clause is invalid but I cannot guess what it should be, every begin must have an end, every statement must have a terminator - usually ;. Are you attempting this in phpmyadmin, mysql workbench or something else?

Related

MySQL trigger issue - is it working properly?

I have a table which contains column of type DATE, now i want to create trigger after insert, which deletes all the past rows from the database.
My trigger looks like this:
CREATE TRIGGER deletePastNotesAfterInsert AFTER INSERT ON tableofnotes FOR EACH ROW DELETE FROM tableofnotes WHERE deadLine < CURRENT_DATE();
The problem is that whenever I try to insert something, it is automatically deleted, which should not be in case the date is not past. Thank you for your help in advance.

Trigger Update Before Or After Update On Same Table

I have this table:
Table: product
By using PHPMYADMIN, I am thinking of setting a trigger so that whenever I make changes to the price on any item, it will trigger to record the time on the price_change_time for that particular item.
My trigger is like this:
DELIMITER //
CREATE TRIGGER update_time BEFORE UPDATE ON product
FOR EACH ROW
BEGIN
UPDATE product SET price_change_time = NOW() WHERE NEW.price <> OLD.price;
END; //
DELIMITER ;
Sadly this won't work and I get:
MySQL said:Documentation
#1442 - Can't update table 'product' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I read some of the previous asked questions and answers which is somehow related, but still not able to suit it for my case.
Anyone willing to help? My question is:
1) Is there any way to achieve what I want by just using PHPMYADMIN?
2) If Not then what is the proper way?
You can create a column in a MySQL table something like this:
CREATE TABLE product (
...
price_change_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
...
);
This creates a "magic" column in your table that gets updated to the current time whenever its row is first INSERTed or UPDATEd.
Recent versions of MySQL, but not older ones, also allow this for DATETIME columns.
You can use DATE(price_change_time) to retrieve just the date part of a timestamp.

MySQL delete similar records after insert with Trigger

I have MySQL database running with InnoDB engine. I have table which has columns id, nickname, score (so it's a scoreboard).
I need to check AFTER INSERT all rows and find similar record, if there is one delete old one.
USE 'database';
DELIMITER $$
CREATE TRIGGER tests_AINS AFTER INSERT ON tests FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
So how do I proceed?

Multiple MySQL queries (no PHP)

I am wondering if it is possible to perform a SQL query then update another table with the generated ID and continue through all of the rows?
I have this SQL query that works but what I need to do is after each row is added to cards to then update merged.cars_id with the last generated ID so they are linked. normally I would do this with PHP but ideally I would like to just do it with MySQL if possible.
MAIN QUERY
INSERT INTO cards (first_contact_date, card_type, property_id, user_id)
SELECT first_contact_date, 'P', property_id, user_id FROM merged
THEN I NEED WITH MATCHING ROWS (Roughly)
UPDATE merged SET merged.card_id = LAST_INSERT_ID (FROM ABOVE) into the matching record..
Is something like this possible and how do I do it?
I would recommend using MySQL triggers to do this
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
A trigger is a function that will be executed AFTER or BEFORE the INSERT or DELETE or UPDATE is done over any record of your table.
In your case you need to do a AFTER INSERT on cards that just updates the merged table. Make sure its AFTER insert as you wont be able to access the new row's ID otherwise.
The code would look something like this, assuming the id field from the cards table its named "id"
delimiter |
CREATE TRIGGER updating_merged AFTER INSERT ON cards
FOR EACH ROW BEGIN
UPDATE merged SET card_id = NEW.id;
END;
|
delimiter ;
May I suggest Stored Procedures?
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
--EDIT--
Ah yes, triggers. For this particular situation, Jimmy has the answer. I will leave this post for the sake of the link.
I would set up a trigger to do this. For mysql, read http://dev.mysql.com/doc/refman/5.0/en/triggers.html. This is what triggers are designed to handle.

How can I use a trigger to update a field in one table, based on data from record insert in another?

I'm new to working with triggers and am having a hard time understanding how to write a trigger to update a field in one table, when a record is inserted in another.
To elaborate, I have 2 tables: servTickets and servTicketNotes.
servTickets has several text fields for customer, contact, phone, email, problem description, status, etc...the PK in this table is an INT field called callID.
servTicketNotes has only 2 fields - again, the PK is an INT field 'callID' and there is a BLOB field called image which stores an image of a service report.
What I'm struggling to do is have a trigger update the status field in servTickets with a value of Closed when a new record is inserted into servTicketNotes.
I'm confused if this is an INSERT AFTER or BEFORE or BOTH scenario, but basically if a report is sent in (thereby creating a record in servTicketNotes, I want the trigger to seek out the record with the same callID in the servTickets table and change the value of status to 'Closed'.
This seems like it should be so simple, but I can't seem to grasp how to get started...
Thanks in advance for your help/guidance!
is it probably a POST trigger - which means:
AFTER you have committed the incoming record, you want to take further action - i.e. inserting into the other table.
if you do it PRE commit, then you would worry about some error happening on the Notes and you might end up with an incorrect update to the status.
You can do this with an AFTER INSERT trigger. Try something like this:
DELIMITER $$
DROP TRIGGER IF EXISTS tr_a_ins_servTicketNotes $$
CREATE TRIGGER pabeta.tr_a_ins_servTicketNotes AFTER INSERT ON servTicketNotes FOR EACH ROW BEGIN
update servTickets
set status = 'Closed'
where callID = NEW.callID;
END $$
DELIMITER ;