Delete Your Code #0: Timestamp

delete your code, development Add comments

In many cases your application must keep track of an entity creation or update time. Do you know, that this can be done automatically?

Yes, I know, there is such feature out of the box in many frameworks (for example, Symfony). Naming convention guaranties that database table’s column named ‘stamp_created‘ will be set by entity creation date, and ‘stamp_updated” – by time when any column of the entity has been changed. This done by application server side code (e.g. PHP).

The same can be done by good database table defenition (in MySQL).

  • Create a TIMESTAMP column with CURRENT_TIMESTAMP as default – and that’d be an auto-initializing ‘stamp_created field. 
  • Create a TIMESTAMP column with ON UPDATE CURRENT_TIMESTAMP  directive – and that’d be an autoupdating ‘stamp_updated’ field. 

Other options can be found in MySQL manual.

All this can be done in PHPMyAdmin application by correspondent settings. 

Drawback of such solution: table cannot have both ‘stamp_created’ and ‘stamp_updated’ fields – choose one pill, Neo.

One Response to “Delete Your Code #0: Timestamp”

  1. Snowcore Says:

    Yes, it’s really frustrating that I can’t define to columns with CURRENT_TIMESTAMP default value 🙁

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in