Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TIME type in MySQL?

I'm building my first DB through MySQL Workbench. In a table I need to store a time attribute that contains minutes and seconds. In this regard, I wanted to use the TIME data type. What is the argument required by the type TIME(...)?

like image 200
Fabio Avatar asked Jun 01 '15 08:06

Fabio


People also ask

Is there a time datatype in MySQL?

The date and time data types for representing temporal values are DATE , TIME , DATETIME , TIMESTAMP , and YEAR . Each temporal type has a range of valid values, as well as a “zero” value that may be used when you specify an invalid value that MySQL cannot represent.

What datatype is used for time?

The TIMESTAMP data type consists of a date and time, with optional time zone.

How do you make a table with time?

To create a table in the database, we will first select a database in which we want to create a table. Then we will write the following query to create a table: mysql> CREATE TABLE items_tbl(ID INT, Item_Name VARCHAR(20), Item_Quantity INT, Item_Price INT, Item_OrderDate DATE, Item_OrderTime TIME);

How do I insert date in YYYY-MM-DD format in MySQL?

You can use str_to_date to convert a date string to MySQL's internal date format for inserting.


1 Answers

As said in MYSQL docs:

MySQL retrieves and displays TIME values in 'HH:MM:SS' format

You have to use the TIME data type to represent the time of the day however to store only the minutes and seconds you need to use the DATE_FORMAT to format your time in your desired format.

On a side note, do note that TIME datatype takes 3 bytes of space.

like image 126
Rahul Tripathi Avatar answered Oct 14 '22 04:10

Rahul Tripathi