7๏ธโƒฃ ๐Ÿ”ฃ MySQL Data Types
Estimated reading: 4 minutes 21 views

๐Ÿ“† MySQL Date & Time Types โ€“ DATE, TIME, DATETIME, TIMESTAMP Explained


๐Ÿงฒ Introduction โ€“ Why Date & Time Types Matter in MySQL?

In MySQL, handling dates and times accurately is essential for recording events, scheduling actions, generating reports, and managing logs. MySQL provides dedicated data types like DATE, TIME, DATETIME, and TIMESTAMP to store and manipulate temporal values efficiently.

Using the correct type ensures:

  • โœ… Accurate time zone conversions
  • โœ… Optimized storage
  • โœ… Correct sorting and comparisons
  • โœ… Auto-timestamping support

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The differences between DATE, TIME, DATETIME, and TIMESTAMP
  • Syntax, formats, storage, and default behaviors
  • Real-world use cases and best practices

๐Ÿ“˜ 1. DATE โ€“ Stores Only Calendar Dates

๐Ÿ”น Format

'YYYY-MM-DD'

๐Ÿ”น Example

CREATE TABLE employees (
  id INT,
  birth_date DATE
);

Explanation:

  • Stores only the date: e.g., '1990-01-15'
  • No time component is saved
  • โœ… Ideal for birthdays, due dates, join dates

๐Ÿ•’ 2. TIME โ€“ Stores Only Time of Day

๐Ÿ”น Format

'HH:MM:SS'

๐Ÿ”น Example

CREATE TABLE shifts (
  shift_id INT,
  start_time TIME,
  end_time TIME
);

Explanation:

  • Stores time values like '08:30:00'
  • Ranges from '-838:59:59' to '838:59:59'
  • โœ… Useful for durations, clock-in/out, recurring time blocks

๐Ÿ“† 3. DATETIME โ€“ Stores Date + Time (No Time Zone)

๐Ÿ”น Format

'YYYY-MM-DD HH:MM:SS'

๐Ÿ”น Example

CREATE TABLE appointments (
  id INT,
  scheduled_at DATETIME
);

Explanation:

  • Stores both date and time
  • No automatic time zone conversion
  • โœ… Great for storing local event timestamps (e.g., booking time)

๐Ÿ•“ 4. TIMESTAMP โ€“ Date + Time With Time Zone Awareness

๐Ÿ”น Format

'YYYY-MM-DD HH:MM:SS'

๐Ÿ”น Example

CREATE TABLE logs (
  id INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Explanation:

  • Behaves like DATETIME but converts to/from UTC internally
  • Automatically updates if configured with ON UPDATE CURRENT_TIMESTAMP
  • โœ… Best for audit logs, created/updated timestamps

๐Ÿ” TIMESTAMP vs DATETIME โ€“ Key Differences

FeatureDATETIMETIMESTAMP
Time zone conversionโŒ Noโœ… Yes (stored as UTC)
Default value allowedโœ… Yesโœ… Yes (with auto-updating options)
Range1000-01-01 to 9999-12-311970-01-01 to 2038-01-19 (4-byte limit)
Use for system time trackingโŒโœ… Yes
Use for local event timeโœ… YesโŒ Avoid if user time zone matters

โฑ๏ธ Storage & Range Overview

TypeFormatStorageValue Range
DATE'YYYY-MM-DD'3 bytes'1000-01-01' to '9999-12-31'
TIME'HH:MM:SS'3 bytes'-838:59:59' to '838:59:59'
DATETIME'YYYY-MM-DD HH:MM:SS'8 bytes'1000-01-01 00:00:00' to '9999-12-31'
TIMESTAMP'YYYY-MM-DD HH:MM:SS'4 bytes'1970-01-01' to '2038-01-19'

โณ Examples with Defaults & AUTO Timestamp

CREATE TABLE posts (
  id INT,
  published_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Explanation:

  • published_at: Automatically set when the row is inserted
  • updated_at: Auto-updates every time the row changes

๐Ÿ“˜ Best Practices

โœ… Tip๐Ÿ’ก Why It Matters
Use DATE when time is irrelevantAvoids unnecessary data
Use TIMESTAMP for auditing, logsSupports UTC tracking and automatic updates
Prefer DATETIME for local time recordsAvoids time zone issues with event-based systems
Avoid default values with TEXT/BLOB but use with date/timeTimestamp fields support defaults
Use ON UPDATE CURRENT_TIMESTAMP for updated_atAutomates audit trail

๐Ÿš€ Real-World Use Cases

ScenarioType UsedReason
User birthdayDATENo time component needed
Product sale scheduleDATETIMEEvent occurs at local time
Comment creation timestampTIMESTAMPAuto-updates and timezone conversion
Shift start and endTIMEOnly care about time, not date
Blog post published timeDATETIMEKeep exact moment of publishing

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQLโ€™s temporal types help you store and manipulate calendar dates, times, and timestamps with or without time zones. Using the correct type ensures your applications stay time-accurate and easy to audit.

๐Ÿ” Key Takeaways

  • DATE โ€“ Store only calendar dates
  • TIME โ€“ Use for durations and time-only values
  • DATETIME โ€“ Use for exact date+time (no time zone shift)
  • TIMESTAMP โ€“ UTC-based time for logs and auditing

โš™๏ธ Real-World Relevance

Whether you’re building event apps, logging systems, e-commerce reports, or calendar integrations, understanding MySQL date/time types ensures accurate, efficient, and timezone-safe data management.


โ“ FAQ โ€“ MySQL Date & Time Types


โ“ Should I use DATETIME or TIMESTAMP?

  • Use DATETIME for local times
  • Use TIMESTAMP for UTC/logging/auditing

โ“ Can TIMESTAMP auto-update?

โœ… Yes. Use:

updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

โ“ Can I compare a DATE with a DATETIME?

โš ๏ธ Yes, but comparisons auto-truncate the time portion of the DATETIME.


โ“ Whatโ€™s the range limit of TIMESTAMP?

TIMESTAMP maxes out at 2038-01-19 03:14:07, due to 32-bit UNIX time limit.


โ“ Can I format dates inside queries?

โœ… Yes, use DATE_FORMAT():

SELECT DATE_FORMAT(NOW(), '%d-%b-%Y');

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“† MySQL Date & Time Types (DATE, TIME, DATETIME, TIMESTAMP)

Or Copy Link

CONTENTS
Scroll to Top