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(...)
?
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.
The TIMESTAMP data type consists of a date and time, with optional time zone.
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);
You can use str_to_date to convert a date string to MySQL's internal date format for inserting.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With