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
Related
I'm using mysql on a project and I need to update a password_last_updated (timestamp) field every time the password field gets updated. Is there a way to detect updates on the password column using mysql and as soon as this update happens, the password_last_updated column will automatically get updated with the current timestamp?
You should be able to do that with a trigger which checks for a change in the password value, and sets password_last_updated to current_timestamp if it is different:
DELIMITER //
CREATE TRIGGER update_ts
BEFORE UPDATE ON accounts
FOR EACH ROW
BEGIN
IF NEW.password != OLD.password THEN
SET NEW.password_last_updated = CURRENT_TIMESTAMP();
END IF;
END //
DELIMITER ;
Demo on dbfiddle
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();
I have a simple DB-table with few fields for articles database among that is a TimeStamp containing the time of the last update made on the article.
I use "ON UPDATE CURRENT_TIMESTAMP"
Problem is that I want to update only if some fields of this record have changed, not all.
i.e. For each article, I have a field giving the number of time the article has been read by visitors. must not be updated after each web visit... only if I change the title, authors, etc...
Ideally with a nice mysql command...
I have coded the following:
CREATE TRIGGER `actu_lastupdate_date` BEFORE UPDATE ON `actuality`
FOR EACH ROW BEGIN
if NEW.title <> OLD.title OR NEW.chapeau = OLD.chapeau OR NEW.content = OLD.content
then
SET NEW.LastUpdate_date = current_timestamp;
end if;
END
it seems that even if another record field is changing, the Timestamp is updated. There should be something bad somewhere.
I'll look into it
You could write a trigger to add the timestamp on updates
delimiter |
CREATE TRIGGER actu_lastupdate_date AFTER UPDATE on actuality
FOR EACH ROW
BEGIN
if (NEW.title <> OLD.title
OR NEW.chapeau <> OLD.chapeau
OR NEW.content <> OLD.content)
AND (NEW.other_column = OLD.other_column
OR NEW.other_column2 = OLD.other_column2)
then
SET NEW.LastUpdate_date = current_timestamp;
end if;
END
|
delimiter ;
Basically I have a column called "ispushed". Whenever a button on the webpage is pushed all values in that column should be reset to zero except for the row I'm updating, that one should be set to 1.
Something like this
CREATE TRIGGER `TR_ispushed` BEFORE UPDATE on `questions`
FOR EACH ROW BEGIN
UPDATE questions set questions.`ispushed` = 0
WHERE id <> **Current id**
END$$
Trigger is not what you need to use for implementing such logic. Simple update statement will do it : UPDATE questions SET is_pushed = CASE WHEN id =[your_id] THEN 1 ELSE 0 END;
I don't know if this is possible, but I have a column named active in a table. Whenever the active column gets changed, I would like to reset the date in the date column, but ONLY if the active column gets changed.
If other columns are changed but not the active column, then the date would remain the same.
something like
DELIMITER //
CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.active <> OLD.active THEN
SET NEW.date = '';
END IF;
END
//
Ran into an issue with the IF test in the #2 example. When one of the values is null the <> test returns null. This leads to the test not getting met and the action of the trigger will not get run even though the one value does not equal null at all. To fix this I came up with this test that uses <=> (NULL-safe equal). Hope this helps someone out.
DELIMITER $$
DROP TRIGGER IF EXISTS updtrigger ;
$$
CREATE TRIGGER updtrigger AFTER UPDATE
ON yourTable FOR EACH ROW
BEGIN
IF ((NEW.active <=> OLD.active) = 0) THEN
SET NEW.date = '';
END IF;
$$