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 ;
Related
I have a special requirement where I need to automatically update a 'modifiedDate' column's datetime value to NOW(), IF:
Value provided is null
Value provided is the same as what it was before the update
BUT
If value provided is any other date string, then update column with value.
So, I've setup this trigger, but something's now quite right with it:
CREATE TRIGGER `tr_users_updateModDateOnUpdate` BEFORE UPDATE ON `tbl_users`
FOR EACH ROW IF (OLD.date_modified <> NEW.date_modified) THEN
SET NEW.date_modified = IFNULL(NEW.date_modified, NOW());
ELSE
SET NEW.date_modified = NOW();
END IF
I can't see what's wrong with it... , but I'm getting strange behavior when testing with my web application.. So trying to see if the problem is with my trigger, or my php code...
Can anyone tell me if my Trigger code seems okay for my requirements above? Thanks a million!
Pat
Your trigger contains an IF statement, so it needs at BEGIN/END block. Also, the conditions do not seem to do what you want. Finally, you need to set the DELIMITER.
I think that this does what you want:
delimiter //
create trigger tr_users_update_mod_date_on_update
before update on tbl_users
for each row
begin
if new.date_modified is null or old.date_modified = new.date_modified then
set new.date_modified = now();
end if;
end;
//
delimiter ;
Looking to use a trigger on a MYSQL table when the row is updated, anytime the row is updated. Only setting the id if the id field is null
create trigger before_id_update
BEFORE UPDATE
ON USERS for each row
BEGIN
if id iS NULL THEN
SET id = username
END IF;
END
Currently nothing is happening when the table row is updated
Your code should raise a syntax error, since id and username are undefined variables. If you want to access the values on the updated row, use pseudo-tables NEW or OLD.
You also need a delimiter statement at the beginning of your code, and the set command should be terminated with a semi-colon.
You presumably want:
delimiter //
create trigger before_id_update
before update on users
for each row
begin
if new.id is null then
set new.id = new.username;
end if;
end
//
delimiter ;
Note that this only makes sense if both id and username have the same datatype (which is not obvious from their names).
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
I have this reservation table that has RESERVATION_ID , ROOM_NUM, Date_Start , Date_End and cost columns. what I want to do is insert all the columns except cost and fill the cost automatically.
delimiter //
CREATE TRIGGER upd_check before INSERT ON reservation
FOR EACH ROW
BEGIN
IF NEW.RESERVATION_COST = NULL THEN
SET NEW.RESERVATION_COST = 'timestampdiff(day, NEW.RESERVATION_STARTD, NEW.RESERVATION_ENDD)*70';
END IF;
END;//
delimiter ;
I wrote this trigger to do it, but whenever I press Apply to insert everything nothing is inserted to the RESERVATION_COST column.
why?
I would put this in a comment if I had enough reputation, but anyways. Triggers cannot act on the same table which activated them. Seems limiting, but we've had the same issue.
This link doesn't explicitly say this but it does say "in the associated table". Not the same: https://dev.mysql.com/doc/refman/5.0/en/triggers.html
You can't compare a value to NULL, you need to check if it IS NULL instead.
e.g.
IF NEW.RESERVATION_COST IS NULL THEN
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