MySQL trigger issue - is it working properly? - mysql

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.

Related

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.

Phpmyadmin Database Trigger Creation Query

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?

PL SQL Trigger to update start time for row when a single column is updated

I'm fairly new to triggers and have already tried searching for a solution to my question with little results. I want to update a single row's start time column whenever it's active column is set to 1.
I have two columns ACTIVE (number) and START_TIME (timestamp) in my_table. I would like to create a PL/SQL trigger that updates the START_TIME column to current_timestamp whenever an update statement has been applied to the ACTIVE column - setting it to 1.
So far I have only seen examples for inserting new rows or updating entire tables which isn't what I'm looking to do. I'd have thought there would be a fairly simple solution to my problem.
This is what I've got so far from other examples. I know the structure of my solution is poor and I'm asking for any input to modify my trigger to achieve my desired result.
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (my_table.ACTIVE = 1)
begin
insert my_table.start_time = current_timestamp;
end;
\
you can use like this .it may help you
write the update query instead of insert query
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (new.ACTIVE = 1)
begin
update my_table set start_time =current_timestamp;
end;
I think it should be a BEFORE UPDATE, not AFTER UPDATE, so it saves both changes with a single action. Then you don't need the INSERT or UPDATE statements. I also added the "OF active" clause, so it will only start this trigger if that column was updated, which may reduce the workload if other columns get updated.
CREATE OR REPLACE TRIGGER routine_active
BEFORE UPDATE OF active ON my_table
FOR EACH ROW
BEGIN
IF active = 1
THEN
:NEW.start_time = current_timestamp;
END IF;
END;

how to use auto-incremented column of one table in another table-mysql workbench 6.2

I tried it with the help of this query "insert into class(faculty_id) select faculty_id from faculty;" but it is inserting already existing values only. Here one column of the table is under auto-increment so I wanted to take this incremented values to another table's column in mysql workbench 6.2. I even tried setting a foreign key between the two columns but it dint work. Please can anyone help out with this issue??
This is what you want i think. an after insert trigger will fire every time something is inserted into faculty, and after the data has been inserted. You can access the values that have just been inserted with the NEW. prefix.
DELIMITER //
create trigger classInsert after insert on faculty
for each row
begin
insert into class(faculty_id) values (NEW.faculty_id);
end//
DELIMITER ;
And it will automatically add an entry into the class table every time you add a new value to faculty.
Fiddle example

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 ;