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.


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.