
When creating a new MariaDB or mySQL table, there are a few different types of columns that can be created to store Date and Time.
- date
- datetime
- timestamp
For example, to create a table named mytable in mydatabase that creates a date column named mydate.
create table mydatabase.mytable (mydate date)
Let's insert a Date and Time string.
"insert into mydatabase.mytable (mydate) values ('2023-01-01 01:00:00')
In this scenario, the column will only contain the date and will not contain the time.
~]$ mysql -e "select * from mydatabase.mytable \G"
mydate: 2023-01-01
Let's create a table named mytable in mydatabase that creates a datetime column named mydatetime.
create table mydatabase.mytable (mydatetime datetime)
Let's insert a Date and Time string.
"insert into mydatabase.mytable (mydatetime) values ('2023-01-01 01:00:00')
In this scenario, the column will only contain both the date and time.
~]$ mysql -e "select * from mydatabase.mytable \G"
mydatetime: 2023-01-01 01:00:00
Let's create a table named mytable in mydatabase that creates a timestamp column named mytimestamp. WIth timestamp the timestamp cannot be before 1970-01-01 00:00:01 UTC.
create table mydatabase.mytable (mytimestamp timestamp)
Let's insert a Date and Time string.
"insert into mydatabase.mytable (mytimestamp) values ('2023-01-01 01:00:00')
In this scenario, the column will only contain both the date and time.
~]$ mysql -e "select * from mydatabase.mytable \G"
mytimestamp: 2023-01-01 01:00:00
Did you find this article helpful?
If so, consider buying me a coffee over at