MSK Institute • Shikohabad
Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Connecting serverPlease wait...
💻 Practical Labs🚀 Free Demo📜 Verified Certificates
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Nearly every real-world application requires handling time: timestamps for when an order was placed, account creation dates, subscription renewal deadlines, and employee shift durations. Choosing between DATE, TIME, DATETIME, and TIMESTAMP requires understanding how MySQL handles time zones.
| Type | Format | Storage | Range | Time Zone Aware? |
|---|---|---|---|---|
DATE | YYYY-MM-DD | 3 bytes | 1000-01-01 to 9999-12-31 | No |
TIME | HH:MM:SS[.fraction] | 3 bytes | -838:59:59 to 838:59:59 | No |
DATETIME | YYYY-MM-DD HH:MM:SS | 5 bytes | 1000-01-01 00:00:00 to 9999-12-31 23:59:59 | No (Constant) |
TIMESTAMP | YYYY-MM-DD HH:MM:SS | 4 bytes | 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC | Yes (UTC converted) |
YEAR | YYYY | 1 byte | 1901 to 2155 | No |
DATETIME vs TIMESTAMPThe most common point of confusion is choosing between DATETIME and TIMESTAMP.
TIMESTAMP:created_at and updated_at record timestamps.DATETIME:TIMESTAMP is stored as a 32-bit integer representing seconds since the Unix Epoch (Jan 1, 1970). On January 19, 2038, 32-bit timestamps will overflow! Modern systems frequently adopt DATETIME or 64-bit timestamps to avoid this limitation.created_at & updated_atMySQL allows columns to populate and update their timestamps automatically:
YYYY-MM-DD): Never store dates as VARCHAR strings like "15/03/2026". Storing strings breaks date sorting ('02/10/2025' sorts before '15/01/2020'), breaks date arithmetic, and prevents indexing!DATETIME, adopt the industry best practice of saving all records in Coordinated Universal Time (UTC), converting to local time only in the UI layer.TIMESTAMP columns?VARCHAR(20) column considered a major database design mistake?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| String & Text Types | Specialized Types: ENUM, BOOLEAN, JSON |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.