MySQL TIMESTAMP Data Type: Usage Guide

In MySQL, the TIMESTAMP type is used to store dates and times. You can use the TIMESTAMP type to store a specific date and time, such as the creation time or update time.

To create a table using the TIMESTAMP type, you can specify the data type of the field as TIMESTAMP in the CREATE TABLE statement, as shown below:

CREATE TABLE example_table (
    id INT PRIMARY KEY,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

In the example above, the field created_at is defined as a TIMESTAMP type and is set with a default value of the current time. This means that if you do not provide a value for the created_at field, it will automatically be set to the current time.

To insert a value of type TIMESTAMP, you can use the INSERT INTO statement and provide the date and time in the value list, for example:

INSERT INTO example_table (id, created_at) VALUES (1, '2022-01-01 12:00:00');

If you only provide the date portion without the time portion, MySQL will automatically set the time to ’00:00:00′.

You can also use the NOW() function to retrieve the current date and time and insert it into a field of type TIMESTAMP, for example:

INSERT INTO example_table (id, created_at) VALUES (2, NOW());

By doing this, you can easily store dates and times using the TIMESTAMP type and retrieve and manipulate these values as needed.

bannerAds