How is datetime used in MySQL?
The MySQL datetime data type is used to store values for both dates and times. Its usage is as follows:
- Create a datetime column:
- Create a table named “table_name” with a column named “column_name” of type DATETIME.
- “Insert a datetime value:”
- Add a record with the timestamp ‘YYYY-MM-DD HH:MM:SS’ to the specified table and column.
- Retrieve datetime value:
- Choose the column_name from the table_name.
- Format the datetime value:
- Retrieve the column_name data from table_name in the specified format.
- In this, ‘format’ refers to the format of date and time, such as ‘%Y-%m-%d %H:%i:%s’ which represents displaying values in the format of “year-month-day hour:minute:second”.
- Compare datetime values.
- Retrieve all records from the table where the column value is greater than ‘YYYY-MM-DD HH:MM:SS’.
These are common uses of datetime, which you can further utilize based on your needs.