Trigger Update Before Or After Update On Same Table - mysql

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.

Related

Mysql update trigger except for specific column updates

In MySQL, I want to fire a trigger when update on all columns except one column update.
In my table row I have 40 columns. I want trigger to update the column update_time whenever there is an update happens on any field except update_time field.
CREATE TRIGGER `UpdateDateTrigger`
BEFORE UPDATE ON `users`
FOR EACH ROW
IF NOT UPDATE(`update_time`) BEGIN
SET new.update_timestamp = now()
END
But it is not working as expected.
By looking at MySQL automatic update for specific column instead of whole record , I've got the solution to exclude two columns update in trigger
IF !((NEW.last_visited <> OLD.last_visited) ||
(NEW.update_time <> OLD.update_time)) THEN
SET new.update_time = now();
END IF
Thank you!
Instead of having a trigger function I would like to suggest you to create the table wisely so that the update_time field gets updated automatically when something is changed associated with that row.
Please look into the documentation here for automatic update on time for each update of the row. The create syntax is simple and effective.
CREATE TABLE t1 (
// .... Other columns
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
If you want a specific control over each of your column, then writing a trigger is the best idea I think. Please check the answer here.

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?

MYSQL Trigger to insert column value in same table

I have a mysql Innodb table 'classrooms_subjects' as
id|classroom_id|subject_id
classroom_id & subject_id are composite keys. Whenever i insert a row with classroom_id & subject_id, my id field is inserted as 0.
Now i want to create a trigger which will enter id field as last_inserted_id()+1.
Also I need to take care of multiple records inserted at a time. My trigger is like below:
CREATE TRIGGER `increment_id` AFTER INSERT ON `classrooms_subjects`
FOR EACH ROW BEGIN
UPDATE classrooms_subjects
SET classrooms_subjects.id = LAST_INSERT_ID() + 1 WHERE id=0;
END
when i am inserting a record I am getting the error as:
"Cant update table in trigger because it is already used by statement which invoked this trigger
For general info: using an update statement inside the trigger isn't right.
Better to use a before insert trigger and simply assign the value of your column using NEW.id
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
A column named with OLD is read only. You can refer to it (if you have
the SELECT privilege), but not modify it. You can refer to a column
named with NEW if you have the SELECT privilege for it. In a BEFORE
trigger, you can also change its value with SET NEW.col_name = value
if you have the UPDATE privilege for it. This means you can use a
trigger to modify the values to be inserted into a new row or used to
update a row. (Such a SET statement has no effect in an AFTER
trigger because the row change will have already occurred.)
You should probably structure your table to make the auto_increment work properly. Better a solution that works when multiple sessions are inserting to the DB at once.

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;

MySQL automatic update for specific column instead of whole record

I have a table APPLEVARIETY.
It has the following columns:
id
family
color
description (TEXT)
description_last_update_time (TIMESTAMP)
For the latter column I would like to use 'ON UPDATE CURRENT_TIMESTAMP'. However, this will cause the timestamp to be updated if at least one of the other columns are updated (id, family, color).
Instead I would like to specify that the description_last_update_time will get the CURRENT_TIMESTAMP if only the description column is updated. It should ignore updates on the rest of the columns.
I have looked through the MySQL manual, other questions on stackoverflow and of course used google extensively but found nothing.
So in other words: is there some way to specify a certain column with ON UPDATE? Is it even possible or am I moving in the wrong direction?
No thats not possible using the ON UPDATE CURRENT_TIMESTAMP this will get updated when any column is updated.
You may however achieve the same using a trigger. For that you will first need to remove ON UPDATE CURRENT_TIMESTAMP from the table definition.
The trigger will look like
delimiter //
create trigger APPLEVARIETY_update before update on APPLEVARIETY
for each row
begin
if new.description <> old.description then
set new.description_last_update_time = now();
end if;
end;//
delimiter ;