Modifying values in existing records using trigger - mysql

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.

Related

How to embed an aggregate function in an event in SQL

I've recently begun learning SQL and currently working on a project tracking the progress of the vaccination rollout. I'm trying to make an event which will automatically tally up the number of patients who have received both vaccine doses at a certain time each day.
This is what I've got so far. The event should return the timestamp and total number of second doses given (as defined in the patient_vaccine_history table), and add these entries to the vaccinated_tally table.
Workbench is kindly telling me that "COUNT" is not valid in line 17.
SET GLOBAL event_scheduler = ON;
CREATE TABLE vaccinated_tally(
ID INT NOT NULL AUTO_INCREMENT,
last_update TIMESTAMP,
fully_vaccinated_tally INT,
PRIMARY KEY (ID));
DELIMITER //
CREATE EVENT daily_tally
ON SCHEDULE AT NOW() + INTERVAL 1 SECOND
DO BEGIN
INSERT INTO vaccinated_tally(last_update)
VALUES (NOW());
INSERT INTO vaccinated_tally(fully_vaccinated_tally)
VALUES COUNT(pvh.nhs_number) -- this is the problem line
FROM patient_vaccine_history pvh
WHERE pvh.dose = 2
);
END//
DELIMITER ;
You would seem to want insert . . . select:
INSERT INTO vaccinated_tally (fully_vaccinated_tally)
SELECT COUNT(pvh.nhs_number) -- this is the problem line
FROM patient_vaccine_history pvh
WHERE pvh.dose = 2;
You are not inserting the timestamp. Perhaps you have a trigger or other mechanism for setting it.

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.

Creating a trigger that will update a column with an interval

Sorry if this is a noob question as I am one right now but
I am seem to be having a problem on the mysql trigger. I am trying to create a trigger that will automatically updates the data dateValid with an interval of 5 minutes from the dateCreated's data after insert.
DROP TRIGGERIF EXISTS `restrictionotp`;TRIGGER `add_interval` beforeINSERT
on `date` FOR each row
UPDATE date
SET datevalid=date_add(new.datecreated, interval 5 minute)
WHERE dateid=new.dateid
The code above was successfully been created but once I try to insert a data on the said table this error shows up
Can't update table 'verification' in stored function/trigger because
it is already used by statement which invoked this stored function/trigger
Thanks for answering my question
Just set the new value in the trigger. The database will then insert the modified value instead of the original.
DELIMITER //
CREATE TRIGGER Add_Interval
BEFORE INSERT ON verification
FOR EACH ROW
BEGIN
SET new.datevalid = new.datecreated + INTERVAL 5 MINUTE;
END;
//
DELIMITER ;

MySQL update automatically a column after update

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();

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.