This responsibility can be given to MySQL to automatically assign current time values to these columns.
In MySQL 5.6 onwards, this can be done by setting the data type of the columns to be either date time or timestamp and creating date_created column with NOT NULL DEFAULT CURRENT_TIMESTAMP schema and date_updated column with NOT NULL DEFAULT ‘0000-00-00 00:00:00’ as schema with attribute ON UPDATE CURRENT_TIMESTAMP.
Below is a sample schema of a table containing date_created and date_updated columns:
CREATE TABLE `time_stamp` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’ ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
But there is a catch here, since this is not applicable to all MySQL versions. Earlier to 5.6 version, MySQL allows a table to have only one TIMESTAMP column with an automatic TIMESTAMP value, that is you can either have date_created or date_updated auto updated to CURRENT_TIMESTAMP not both.
Related to this the MySQL documentation has some very good info at this page: http://ift.tt/1wimyw9
Excerpt: "By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values, and assigning NULL assigns the current timestamp."
So the workaround this is to have date_created column with timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’ schema and date_updated column with timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’ ON UPDATE CURRENT_TIMESTAMP schema.
With above, the date_updated field is correctly updated by MySQL automatically when there is an update and to update date_created field with current timestamp value, we have to explicitly pass NULL value to date_created field which will then store the CURRENT_TIMESTAMP value in the field.
Below is a sample schema for above changes:
CREATE TABLE `time_stamp` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_created` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
`date_updated` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’ ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
So for a structure as above the insert query would be as below:
INSERT INTO `test`.`time_stamp`
(`id`, `date_created`)
VALUES
(NULL, NULL);
For update the query would be:
UPDATE
`test`.`time_stamp`
SET
`id` = ‘4’
WHERE
`time_stamp`.`id` = 1;
Above will just update the date_updated with the CURRENT_TIMESTAMP and the date_created value will remain same as earlier. Caution needs to be taken while updating records so as not update date_created field with NULL value.
One could also say that instead of MySQL automatically updating the date_updated field, I would like the date_created field to be updated automatically by it. Yes, it is just a reverse case and can be used, in which case while updating the record one has to pass NULL value to date_updated field. But the thing is that since there will be many updates and only a one time insert so passing a NULL while a one time insert reduces the overhead on us of passing a NULL for every subsequent update and let MySQL handle it for us. But yes this is debatable and there are cases where this reverse structure can be used.
Another workaround this is to have date_created field updated with current timestamp value using a trigger when a record is inserted.
Hope this helps 🙂
PlanetMySQL Voting: Vote UP / Vote DOWN
via Planet MySQL
MySQL auto update date_created and date_updated columns