MySQL update automatically a column after update - mysql

As the title says, I have a table with a column named last_update of type DATE. I want to make a trigger that set it to CURDATE() every time I update a row(set it for this particular row). I tried this:
delimiter $$
CREATE TRIGGER before_customer_update before update on customer for each row
begin
update customer set last_update = CURDATE() where id = old.id;
end $$
delimiter ;
but a get an error
can't update table in stored function/trigger because it is already
used by statement
How can I resolve this?

It would be easier to change the column definition to
last_update timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you really want to do it in a trigger (refer to the current record with NEW):
Replace
update customer set last_update = CURDATE() where id = old.id;
with
set NEW.last_update = CURDATE();

Related

How can I update my table field create_date to current date using trigger in sql?

Error Code: 1442. Can't update table 'participant' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So I want to achieve this -> once the participant is updated I want to update his create_date to current date
drop trigger update_user;
DELIMITER //
CREATE
TRIGGER update_user BEFORE UPDATE
ON Competition.participant
FOR EACH ROW BEGIN
update participant
set create_date = CURRENT_TIMESTAMP
where id_participant = old.id_participant;
END//
DELIMITER ;
update participant
set name = 'Rostyk'
where id_participant = 1;
CREATE
TRIGGER update_user
BEFORE UPDATE
ON Competition.participant
FOR EACH ROW
SET NEW.create_date = CURRENT_TIMESTAMP;
But it is more simple to define create_date column as ON UPDATE CURRENT_TIMESTAMP, and do not use triggers.

MySQL Auto Update Date when any row value is updated

I have an existing table with values already filled in. Last column is of type date.
My requirement is that if any value is updated, that row's corresponding date column should be automatically updated to the current date.
For example, if the table is as follows:
and if I change any of the values of col1 (78) /col2 (nvvb) /col3 (566) of say row no. 2 on date 18Nov2018, then the col4 of row 2 should automatically update to today's date of 18Nov2018 from existing 13-Oct-17.
I've checked existing topics by could not find an answer.
The closest I can find is this code, but it is not working for the above requirement:
create table if not exists my_table (
index1 char(32) not null primary key,
title varchar(50),
my_timestamp timestamp not null default current_timestamp on update current_timestamp
)
Any help will be greatly appreciated.
Thanks
First, you need to create code blocks for whatever code you are displaying in your question/answer by highlighting the lines of code and pressing ctrl-k
You will need a trigger that changes the date value in the row to the current date using the CURDATE() function. BEFORE UPDATE simply means apply the current block enclosed in "BEGIN... END" before the actual update call is made by MySQL. You access the row to be updated with NEW.<field>. To store a value in a particular tuple, use the SELECT ... INTO .... statement
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE UPDATE ON table_name
FOR EACH ROW
BEGIN
SELECT CURDATE() INTO NEW.`col4(date)`;
END $$
DELIMITER ;
For more info, see: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://dev.mysql.com/doc/refman/8.0/en/ansi-diff-select-into-table.html
UPDATE: YOU CANNOT UPDATE WITHIN AN UPDATE TRIGGER, MySQL does not prevent an update trigger from being called by itself, nor does it prevent deadlock issues, so you cannot use this. Must use the method below.
Using the method described in the comments (if your version supports it):
CREATE TABLE last_mod (
last_modified_date DATE NOT NULL,
PRIMARY KEY (last_modified_date)
)
CREATE TABLE IF NOT EXISTS my_table (
my_date DATETIME NOT NULL,
INDEX par (my_date),
FOREIGN KEY (my_date) REFERENCES last_mod(mast_modified_date) ON UPDATE CASCADE
);
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
UPDATE last_mod SET last_modified_date = CURDATE();
END $$
DELIMITER ;

Mysql After Insert trigger to check new value

I have two tables:
oee_main
oee_client
I already have an after insert trigger on oee_main as follows:
CREATE TRIGGER `oee_upd` AFTER INSERT ON `oee_main`
FOR EACH ROW UPDATE oee_client
SET END_DATE= NOW() where END_DATE IS NULL
I now need to develop the trigger even more so that the newly inserted row in oee_main will not only update the END_DATE of oee_client to NOW() where END_DATE is NULL but to only update the row(s) of oee_client where the column called Machine_ID is equal to the newly inserted column named NAME in oee_main.
I have tried adding this to the where condition of the above trigger to no success:
where END_DATE IS NULL and Machine_ID = new.`oee_main`.`NAME`
Therefore what I am after is that when a new record is inserted in oee_main which has a value of for example '2' in column 'NAME' the trigger updates only the columns END_DATE of oee_client where NULL and where Machine_ID of oee_client is equal to the newly inserted value of NAME in oee_main.
Thanks
I think this is the logic you want:
DELIMITER $$
CREATE TRIGGER `oee_upd` AFTER INSERT ON `oee_main`
FOR EACH ROW
BEGIN
UPDATE oee_client c
SET c.END_DATE = NOW()
WHERE c.END_DATE IS NULL AND c.Machine_ID = new.NAME;
END$$
DELIMITER ;
Remember to use the delimiter statement whenever you define triggers, stored procedures and functions. It will help prevent errors in the future.
As for your error, you don't need to mention the table name when you use NEW.
As an outsider, I find it confusing that NAME in one table matches MACHINE_ID in another table. I would expect both tables to have a column called MACHINE_ID.

Modifying values in existing records using trigger

I have a MySQL table with the following data
id INT
time_stamp timestamp
status INT
When inserting, the time_stamp attribute will always be current_timestamp, once the current timestamp is more than 48 hrs of the value inserted in the table, i need to change the status to 0.
I created the following trigger:
DELIMITER $$
CREATE TRIGGER new_table_AINS
BEFORE INSERT ON new_table
FOR EACH ROW begin
DECLARE now_value timestamp;
SET #now_value = current_timestamp;
UPDATE new_table SET new.status = 0 where #now_value = new.time_stamp + interval 48 hour;
END$$
But when I try to insert a new row, I get Error Code 1442 in MySQL
Error Code: 1442. Can't update table 'new_table' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger. Please help me.
Thanks a lot!
In a "before" trigger, you cannot re-modify the table that you are triggering on. However, I don't think you need a trigger. Instead, use a view:
create view v_ new_table_AINS as
select . . .,
(case when current_timestamp >= time_stamp + interval 48 hour then 0
else status
end)
from new_table_AINS;
At your leisure, you can then run a job that updates the status to zero for old timestamps.

MySql Triggers and update

I have a table with a date field in it tableA when a record is updated I would like a trigger to check the date field and if it is greater than a certain date set another field active to 'yes'
I've tried but can't seem to get it right
Managed to work it out as follows,
TRIGGER `active` BEFORE INSERT ON `tableA`
FOR EACH ROW
BEGIN
IF(NEW.install_date > '1999-01-01') THEN
SET NEW.active='yes';
ELSE
SET NEW.active = OLD.active;
END IF;
END