Advertisement

Google Ad Slot: content-top

MySQL Dates


MySQL Dates

In MySQL, date and time data are handled using specific data types and functions. Dates are commonly used in applications for storing events, records, transactions, and other time-based data.


MySQL Date Data Types

MySQL comes with the following data types for storing a date or a date/time value in the database:

Data Type

Description

Format

DATE

Stores date (without time)

YYYY-MM-DD

DATETIME

Stores date and time

YYYY-MM-DD HH:MM:SS

TIMESTAMP

Similar to DATETIME, but stores in UTC

YYYY-MM-DD HH:MM:SS

TIME

Stores time (without date)

HH:MM:SS

YEAR

Stores only the year

YYYY

Create Table with Date and Time Columns

Example
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
birth_date DATE,
admission_date DATETIME DEFAULT CURRENT_TIMESTAMP,
graduation_year YEAR
);

birth_date → Stores student’s date of birth.

admission_date → Automatically records current date and time.

graduation_year → Stores the year of graduation.