Introduction

Despite the seemingly simple topic, I often get questions from colleagues about the data types in MySQL. And these questions are frequently related to the cases of storing dates and times. What's more, even I, having more than 15 years of experience working with various versions of MySQL, sometimes get into trouble because of some subtle features of this database.

In this article I will analyze the features of various data types that can be used to store dates and times, and also give interesting tips on how to simplify working with them. But the most interesting thing is that I will give examples of documented behavior that few people usually know.

Types Overview

MySQL provides various data types for storing time data, namely DATETIME, TIMESTAMP, DATE, TIME, and YEAR. Some of them allow you to store time only partially, for example, you can use the DATE type to store a date, the TIME type to store time within a day, or, for example, the YEAR type if you only need to store the year value. However, the most widely used data types are DATETIME and TIMESTAMP, which nevertheless have significant differences, which I will discuss below.

DATETIME

TIMESTAMP

Known Unexpected Features

Using NULL to Initialize TIMESTAMP Columns

When explicit_defaults_for_timestamp is disabled, designating NULL for a TIMESTAMP field (but not a DATETIME) can set or refresh it to the current date and time—unless the column explicitly allows NULL. TIMESTAMP's unique oddity may surprise users who think all date-time types act the same. When explicit_defaults_for_timestamp is enabled, TIMESTAMP columns align more with DATETIME and require a clause like DEFAULT CURRENT_TIMESTAMP for auto-initialization.

Zero Dates

MySQL sometimes accepts a zero date (0000-00-00 00:00:00). Depending on your sql_mode (notably NO_ZERO_DATE), inserting this value might trigger warnings or revert silently to a default when NULL is not permitted.

Time Zone Quirks

Server Configuration Pitfalls

Summary

Knowing the specifics of different MySQL data types, understanding zero dates, time zone mechanics, and server settings will help you avoid important mistakes. This applies to both usability and performance, and most importantly, the correctness of the data you store.

Hopefully, this article will help you be more confident the next time you need to decide how to store data or time in MySQL.