The DATETIME
Characteristics
1. Format: YYYY-MM-DD HH:MM:SS
2. Range: From January 1, 1000 to December 31, 9999.
3. Storage Size: 8 bytes.
4. Timezone: Does not convert or store timezone information.
It stores the date and time as-is, without considering the server’s timezone
settings.
Using DATETIME:
CREATE TABLE events (
event_id INT AUTO_INCREMENT PRIMARY KEY,
event_name VARCHAR(100),
event_time DATETIME
);
INSERT INTO events (event_name, event_time)
VALUES ('Meeting', '2024-09-13 10:00:00');
################################################################################
The TIMESTAMP
Characteristics
1. Format: YYYY-MM-DD HH:MM:SS
2. Range: From January 1, 1970 to December 31, 2038 (UNIX epoch time).
3.Storage Size: 4 bytes for values before the year 2038; 8 bytes for values
after.
4. Tiimezone: Converts values to UTC for storage and converts back to the
current timezone for retrieval based on the server’s timezone settings.
Using TIMESTAMP:
CREATE TABLE logs (
log_id INT AUTO_INCREMENT PRIMARY KEY,
log_description VARCHAR(255),
log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO logs (log_description)
VALUES ('User logged in');