Opinions, Expertise, Passion.

Information in black and white, and sometimes some color.

Sep
07

DateTime vs Timestamp

Link to this post

I was asked a question today, “DATETIME vs TIMESTAMP. When to use which & why?”

It’s a good MySQL introduction question, here are some general considerations for choosing one.

Do you need Date values other then an EPOCH value? (i.e. before 1970) If the answer is yes, then DATETIME is required.
If you do not however, then TIMESTAMP is the best choice for a few reasons.

1. The TIMESTAMP columns uses 4 Bytes to record it’s value, while DATETIME uses 8 bytes. Using the smallest storage is always a best practice for all columns.
2. The TIMESTAMP column supports the CURRENT_DATE syntax in the CREATE TABLE command. This enables the column to have a default value for INSERT or for UPDATE, but not both. Indeed this is the only data type that allows for any default value that is not a constant.
3. All date functions (at least the ones I use) work equally as well with TIMESTAMP and DATETIME.

I have yet to find any benchmarking to indicate any performance differences of not selecting TIMESTAMP.

And just for a piece of trivia, the DATE datatype is 3 bytes, the TIME datetype is 3 bytes, so why is the DATETIME 8 bytes?
Yes, for those that intend to reply I do know the answer, however others readers may not. Comments please!

Posted under Databases, MySQL, Professional on 07 Sep 2007

7 Comments »

  1. I test in mysql v5.1, TIMESTAMP is 6 bytes?

    Comment by Paul Gao — September 7, 2007 @ 5:09 pm

  2. “This enables the column to have a default value for INSERT or for UPDATE, but not both. ”

    Are you sure about this? From the mysql online manual:

    http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

    Auto-initialization and auto-update:

    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    Comment by Rory — September 7, 2007 @ 5:21 pm

  3. To quote the reference manual:
    A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation.
    (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html)

    It is good for that purpose but should not be used for anything else because:
    1) the range is limited, not only starting at 1970 but also ending somewhere in 2038, which is more relevant in many systems
    2) the server might update it automatically anyhow, and this depends on if it is the first timestamp column or not. If you do use them you should always specify the default. The rules are very complicated: (http://dev.mysql.com/doc/refman/5.0/en/timestamp.html)
    3) the difference in storage space is not important. I have never seem a system where the problem is to many bytes for datetime values. If your database is too large you should look at strings, BLOBs etc, too long primary keys (very important for clustered tables like in InnoDB), normalization, perhaps delete or archive old data.

    Comment by Erik — September 7, 2007 @ 5:26 pm

  4. There is a space between the date and time in the DATETIME

    Comment by Richard Thomas — September 7, 2007 @ 9:40 pm

  5. Timestamp also stores all values in UTC and converts it to whatever your @@time_zone is set to/from on retrieval/inserts…

    Comment by Tobias "flupps" Asplund — September 8, 2007 @ 2:54 am

  6. When attempting to create a table in MySQL 4.1.20 with two TIMESTAMP columns, one for modified_ts, one for created_ts, I get the following error, even though the docs say I should be able to do so.

    ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    What’s up with this, and how can I do what I need to do without resorting to triggers?

    Thanks,

    PJ

    Comment by PJ — September 11, 2007 @ 2:44 am

  7. Using MySQL 4.1, it’s not possible to have multiple columns with DEFAULT and ON UPDATE for CURRENT_TIMESTAMP. The best means is to do.

    use test;
    drop table if exists test1;
    create table test1 (
    id int unsigned not null auto_increment primary key,
    c1 char(10) not null,
    last_update_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    create_ts TIMESTAMP);

    truncate table test1;
    insert into test1 (c1,create_ts) values(’a',now()), (’b',now());
    select * from test1;
    # 5.0 syntax select sleep(3);
    update test1 set c1=’aa’ where c1=’a';
    select * from test1;

    Comment by Ronald — September 11, 2007 @ 4:55 am

RSS feed for comments on this post.

Leave a comment

Home
Professional Blog RSS Feed of Professional Blog
Consulting
Presentations
About Ronald
Related Links
Contact Ronald
  • « Aug spinner iCalendar Oct »
    September 2007
    M T W T F S S
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
  • Categories:
    • Professional
      • 42SQL
      • Apple
        • iPhone
        • MacBook
        • OS/X
      • Clever Design
      • Cloud Computing
        • 10gen
        • AppNexus
        • Kaavo
        • Kloudshare
      • Databases
        • Drizzle
        • Ingres
        • MySQL
          • Compiling
          • GUI Products
          • MySQL Events
            • mysqlcamp01
            • mysqlcamp02
          • MySQL Proxy
          • MySQL User Conferences
            • mysqluc06
            • mysqluc07
            • mysqluc08
          • Storage Engines
            • Non Transactional
              • Infobright
              • KickFire
              • Maria
              • Nitro
            • Transactional
              • Blob Streaming
              • Falcon
              • InnoDB
              • PBXT
              • Solid
        • Oracle
      • Extreme Programming (XP)
      • General
      • Java
        • Tomcat
      • Linux
        • One Liners
      • Microsoft
      • Open Source
        • Buildbot
        • Ubuntu
        • UltimateLAMP
        • Virtual Box
      • OSCON 2008
      • Packet General
      • PrimeBase Technologies
      • Solid State Drives
      • Sun
      • The Daily WTF
      • Web 2.0 NY
      • Windoze
      • Yahoo
    • Web
      • Google
        • App Engine
        • Summer of Code
      • SEO
        • Brand Identity
      • Web Development
        • Amazon
          • EC2
          • S3
          • SimpleDB
        • CSS
        • HTML
        • PHP
        • Web 2.0
      • Web Sites
        • Application Software
        • Content
        • Cool Tools
        • Linux Stuff
        • MySQL Related
        • Show Your Stuff
        • Twitter
        • Unype
      • WordPress
  • Pages:
    • Best Of PlanetMySQL Articles
    • Interesting Articles
    • MediaWiki Restyling (1)

  • Archives:
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • July 2005
    • June 2005
    • February 2005
    • October 2004
    • September 2004
    • July 2004
    • June 2004