Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to set up last-modified and date-record-added fields?

Tags:

mysql

Hi I would like to set and forget two fields for tracking the date the record was added and also the date the record was last modified in a mySQL database.

I am using "ON UPDATE CURRENT_TIMESTAMP" and was hoping I would just change UPDATE to INSERT.

No luck however. Can anyone give me the heads up on the best way to achieve this? - preferably inside the database itself.

like image 453
Chin Avatar asked Jun 09 '11 15:06

Chin


People also ask

How do you add a date modified in access?

Open the data-entry form in Design view. On the Design tab, in the Tools group, click Add Existing Fields. In the Field List, under Fields available for this view, drag the Date Modified and the Time Modified fields to the form. Adjust the size and placement of the fields on the form as needed.

Can we update last modified date in Salesforce?

LastModifiedDate can be updated to any back-dated value if your business requires preserving original timestamps when migrating data into Salesforce. SystemModStamp is strictly read-only.

How do I change the created date and last modified date in Salesforce?

Created Date and Last Modified Date fields in Salesforce objects are audit fields and we can't edit or change these field directly or through code. You have to contact salesforce support team for them to grant permissions to you to edit or change these fields if needed.

What is last modified in Salesforce?

The date and timestamp for changes made to an individual record, including the User who made the change.


1 Answers

This assumes MySQL 5. Simply add two triggers:

create table foo (a1 INT, created timestamp, updated timestamp) engine=innodb;

DELIMITER |

CREATE TRIGGER foo_created BEFORE INSERT ON foo
FOR EACH ROW BEGIN
SET new.created := now();
SET new.updated := now();
END;

|

CREATE TRIGGER foo_updated BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
SET new.updated := now();
END;
|

DELIMITER     ;

insert into foo (a1) values(7);
select * from foo;

update foo set a1=9;
like image 190
Wayne Walker Avatar answered Sep 21 '22 16:09

Wayne Walker