A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

时间:2022-04-10 00:16:33

转载自:http://www.mysqltutorial.org/mysql-timestamp.aspx

MySQL TIMESTAMP

 

Summary: in this tutorial, you will learn about MySQL TIMESTAMP and  TIMESTAMP column features such as automatic initialization and updating.

Introduction to MySQL TIMESTAMP

The MySQL TIMESTAMP is a temporal data type that holds the combination of date and time. The format of a TIMESTAMP column is YYYY-MM-DD HH:MM:SS which is fixed at 19 characters.

The TIMESTAMP value has a range from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

When you insert TIMESTAMP value into a table, MySQL converts it from your connection’s time zone to UTC for storage. When you query a TIMESTAMP value, MySQL converts the UTC value back to your connection’s time zone. Notice that this conversion does not take place for other temporal data types such as DATETIME.

By default, the connection time zone is the MySQL database server’s time zone. You can use a different time zone when you connect to MySQL database server.

When you retrieve a TIMESTAMP value that was inserted by a client in a different time zone, you will get a value that is not the same as the value stored in the database. As long as you don’t change the time zone, you can get the same TIMESTAMP value that you stored.

MySQL TIMESTAMP time zone example

Let’s look at an example to see how MySQL handles TIMESTAMP values.

First, created a new table named test_timestamp that has a TIMESTAMP column: t1;

Second, set session the time zone to ‘+00:00’ UTC by using the SET time_zone statement.

Third, insert a TIMESTAMP value into the test_timestamp table.

Fourth, select the TIMESTAMP value from the test_timestamp table.

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

Fifth, set the session’s time zone to a different time zone to see what value we get back from the database server:

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

As you see, we received a different time value adjusted to the new time zone.

Automatic initialization and updating for TIMESTAMP columns

Let’s start with an example.

The following statement creates a table named  categories:

In the categories table, the created_at column is a TIMESTAMP column whose default value is set to CURRENT_TIMESTAMP.

The following statement inserts a new row into the categories table without specifying the value for the created_at column:

 

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

As you can see, MySQL used the timestamp (at the time it inserted the row ) to initialize for the created_at column.

So a TIMESTAMP column can be automatically initialized to the current timestamp for inserted rows that specify no value for the column. This feature is called automatic initialization.

We will add a new column named updated_at to the categories table.

The default value of the updated_at column is CURRENT_TIMESTAMP.  However, there is a clause ON UPDATE CURRENT_TIMESTAMP that follows the DEFAULT CURRENT_TIMESTAMP clause. We will find out its meaning soon.

The following statement inserts a new row into the categories table.

 

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

The default value of the created_at column is the timestamp when the row was inserted.

Now, we update the value in the name column of the row with id 2 and query data from the categoriestable .

 

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

Notice that the value in the updated_at column changed to the timestamp at the time the row was updated.

The ability of a TIMESTAMP column to be automatically updated to the current timestamp when the value in any other column in the row changed from it current value is called automatic updating.

The updated_at column is known as an auto-updated column.

Note that if you execute the UPDATE statement to update a same value for the name column, the updated_at column will not be updated.

The value in the updated_at remains unchanged.

A2-04-11.MySQL DATA TYPES- MySQL TIMESTAMP

For more information on automatic initialized and updating, please check it out the time initialization on MySQL website.

As of MySQL 5.6.5, the DATETIME columns also have automatic initialization and updating features. In addition, the DEFAULT_CURRENT_TIMESTAMP and ON UPDATE CURRENT TIMESTAMP can be applied to multiple columns.

In this tutorial, we have introduced you to MySQL TIMESTAMP data type and shown you how to use automatic initialization and updating features of TIMESTAMP columns work.