Monday, October 1, 2012

A little caution with "Mysql's Timestamp"

In this post, I would like to bookkeep a interesting fact on Timestamp.

Fundamentally, Mysql Timestamp is nothing but seconds from epoch and differs from datetime as it stores the date time in UTC and abstracts the date time conversion to system's timezone. Also it supports auto-initialization ('CURRENT_TIMESTAMP or 0000-00-00 00:00:00') and auto-updates (CURRENT_TIMESTAMP).

Now the thing which one has to understand is passing "null" references to Timestamp column coupled with not null references and hence want to emphasize it in this post.

Consider a case where we create a table with definition of modified column as follows:
create table demo_null_passing_to_timestamp(...., modified timestamp not null default '0000-00-00 00:00:00')

The behavior for "modified" column will be as one expects if there is no value passed in to modified column, it will use the default value of 0000-00-00 00:00:00, but the behavior changes when null is passed in this case because of the "not null" constraint. In this case, it uses the value from current timestamp. The only way is to allow null value for this modified column.

Try passing null value to such a column to verify this behavior...

1 comment:

  1. Caesars agrees to buy online gambling group Caesars Entertainment for $15.6B
    Caesars Entertainment has agreed to buy online 문경 출장마사지 gambling 안동 출장안마 group 의왕 출장안마 Caesars Entertainment, an online gambling group 의정부 출장샵 that is 충주 출장마사지

    ReplyDelete