{"id":183,"date":"2011-03-21T05:31:16","date_gmt":"2011-03-21T05:31:16","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=183"},"modified":"2023-12-27T19:19:57","modified_gmt":"2023-12-28T02:19:57","slug":"understanding-mysql-timestamp","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/understanding-mysql-timestamp\/","title":{"rendered":"MySQL TIMESTAMP Data Type"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about MySQL <code>TIMESTAMP<\/code> data type and how to use it to define columns that store timestamp data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL TIMESTAMP data type<\/h2>\n\n\n\n<p>The MySQL <code>TIMESTAMP<\/code> is a temporal <a title=\"MySQL Data Types\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-data-types\/\">data type<\/a> that holds the combination of <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-date\/\">date<\/a> and <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-time\/\">time<\/a>. The <a title=\"MySQL DATE_FORMAT function\" href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-date_format\/\">format <\/a>of a&nbsp;<code>TIMESTAMP<\/code> is <code>YYYY-MM-DD HH:MM:SS<\/code> which is fixed at 19 characters.<\/p>\n\n\n\n<p>The <code>TIMESTAMP<\/code> value has a range from <code>'1970-01-01 00:00:01'<\/code> UTC to <code>'2038-01-19 03:14:07'<\/code> UTC.<\/p>\n\n\n\n<p>When you <a title=\"MySQL INSERT statement\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert <\/a>a <code>TIMESTAMP<\/code> value into a table, MySQL converts it from your connection&#8217;s time zone to UTC for storing.<\/p>\n\n\n\n<p>When you <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query<\/a> a <code>TIMESTAMP<\/code> value, MySQL converts the UTC value back to your connection&#8217;s time zone. This conversion does not occur for other temporal data types, such as <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-datetime\/\">DATETIME<\/a><\/code>.<\/p>\n\n\n\n<p>By default, the connection time zone is the MySQL Server&#8217;s time zone. You also have the option to use a different time zone when connecting to the MySQL Server.<\/p>\n\n\n\n<p>When you retrieve a <code>TIMESTAMP<\/code> value that was inserted by a client in a different time zone, you will receive a value different from the one stored in the database. <\/p>\n\n\n\n<p>However, as long as you don&#8217;t change the time zone, you can retrieve the originally stored <code>TIMESTAMP<\/code> value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL TIMESTAMP time zone example<\/h2>\n\n\n\n<p>Let&#8217;s take an example to see how MySQL handles <code>TIMESTAMP<\/code> values.<\/p>\n\n\n\n<p>First, <a title=\"MySQL Create Table\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">created a new table<\/a> called <code>t<\/code> that has a <code>TIMESTAMP<\/code> column: <code>t1<\/code>;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t (\n   ts <span class=\"hljs-built_in\">TIMESTAMP<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, set the session&#8217;s time zone to <code>'+00:00'<\/code> UTC by using the <code>SET time_zone<\/code> statement.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">time_zone<\/span>=<span class=\"hljs-string\">'+00:00'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, insert a <code>TIMESTAMP<\/code> value into the <code>t<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t(ts)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'2008-01-01 00:00:01'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, select the <code>TIMESTAMP<\/code> value from the <code>t<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> ts <span class=\"hljs-keyword\">FROM<\/span> t;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+\n| ts                  |\n+---------------------+\n| <span class=\"hljs-number\">2008<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span> <span class=\"hljs-number\">00<\/span>:<span class=\"hljs-number\">00<\/span>:<span class=\"hljs-number\">01<\/span> |\n+---------------------+\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, set the session&#8217;s time zone to a different time zone to observe what value we receive from the database server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">time_zone<\/span> =<span class=\"hljs-string\">'+03:00'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, query data from the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> ts <span class=\"hljs-keyword\">FROM<\/span> t;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+\n| ts                  |\n+---------------------+\n| <span class=\"hljs-number\">2008<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span> <span class=\"hljs-number\">03<\/span>:<span class=\"hljs-number\">00<\/span>:<span class=\"hljs-number\">01<\/span> |\n+---------------------+\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see, we received a time value adjusted to the new time zone, which is different from the original.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Automatic initialization and updating for TIMESTAMP columns<\/h2>\n\n\n\n<p>Consider the following example.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> named &nbsp;<code>categories<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> categories (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    created_at <span class=\"hljs-built_in\">TIMESTAMP<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">CURRENT_TIMESTAMP<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>categories<\/code> table, the <code>created_at<\/code> column is a <code>TIMESTAMP<\/code> column whose default value is set to <code>CURRENT_TIMESTAMP<\/code>.<\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert<\/a> a new row into the <code>categories<\/code> table&nbsp;without specifying the value for the <code>created_at<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> categories(<span class=\"hljs-keyword\">name<\/span>) \n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'A'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"228\" height=\"41\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png\" alt=\"MySQL TIMESTAMP - Automatic Initialization\" class=\"wp-image-6141\"\/><\/figure>\n\n\n\n<p>The output indicates that MySQL used the timestamp at the time of insertion as a default value for the <code>created_at<\/code> column.<\/p>\n\n\n\n<p>So a&nbsp;<code>TIMESTAMP<\/code> column can be automatically initialized to the current timestamp&nbsp;for inserted rows that specify no value for the column. This feature is called <strong>automatic initialization<\/strong>.<\/p>\n\n\n\n<p>Third, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-add-column\/\">add a new column<\/a> named <code>updated_at<\/code> to the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> categories\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> updated_at \n  <span class=\"hljs-built_in\">TIMESTAMP<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">CURRENT_TIMESTAMP<\/span> \n  <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">CURRENT_TIMESTAMP<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The default value of the <code>updated_at<\/code> column is <code>CURRENT_TIMESTAMP<\/code>.<\/p>\n\n\n\n<p>And, we have a new clause <code>ON UPDATE CURRENT_TIMESTAMP<\/code> that follows the <code>DEFAULT CURRENT_TIMESTAMP<\/code> clause. Let&#8217;s see its effect.<\/p>\n\n\n\n<p>Fourth, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert<\/a> a new row into the <code>categories<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> categories(<span class=\"hljs-keyword\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'B'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, query data from the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"57\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating.png\" alt=\"MySQL TIMESTAMP - Automatic Updating\" class=\"wp-image-6142\" title=\"MySQL TIMESTAMP - Automatic Updating\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating.png 341w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-300x50.png 300w\" sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/figure>\n\n\n\n<p>The default value of the column <code>created_at<\/code> is the timestamp when the row was inserted.<\/p>\n\n\n\n<p>Sixth, update the value in the column <code>name<\/code> of the row id 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> categories \n<span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">name<\/span> = <span class=\"hljs-string\">'B+'<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, query data from the <code>categories<\/code> table to check the update:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> *\n<span class=\"hljs-keyword\">FROM<\/span> categories\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature.png\" alt=\"MySQL TIMESTAMP - Automatic Updating feature\" class=\"wp-image-6143\" title=\"MySQL TIMESTAMP - Automatic Updating feature\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature.png 342w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature-300x35.png 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><\/figure>\n\n\n\n<p>Notice that the value in the <code>updated_at<\/code> column changed to the timestamp at the time the row was updated.<\/p>\n\n\n\n<p>The ability of a <code>TIMESTAMP<\/code> column to be automatically updated to the current timestamp when the value in any other column in the row changes from its current value is called <strong>automatic updating<\/strong>.<\/p>\n\n\n\n<p>The column <code>updated_at<\/code> is referred to as an auto-updated column.<\/p>\n\n\n\n<p>Note that if you execute the <code>UPDATE<\/code> statement to update the same value for the <code>name<\/code> column, the <code>updated_at<\/code> column will not be updated.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> categories \n<span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">name<\/span> = <span class=\"hljs-string\">'B+'<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The value in the <code>updated_at<\/code> remains unchanged.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature.png\" alt=\"MySQL TIMESTAMP - Automatic Updating feature\" class=\"wp-image-6143\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature.png 342w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Updating-feature-300x35.png 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><\/figure>\n\n\n\n<p>For more information on automatic initialization and updating, please check out the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/timestamp-initialization.html\">time initialization<\/a>.<\/p>\n\n\n\n<p class=\"note\">As of MySQL 5.6.5, the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-datetime\/\">DATETIME<\/a><\/code>&nbsp;columns also have automatic initialization and updating features. In addition, the <code>DEFAULT_CURRENT_TIMESTAMP<\/code> and <code>ON UPDATE CURRENT TIMESTAMP<\/code> can be applied to multiple columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the MySQL <code>TIMESTAMP<\/code> data type to represent date and time values.<\/li>\n\n\n\n<li>Set the <code>DEFAULT CURRENT_TIMESTAMP<\/code> attribute for a <code>TIMESTAMP<\/code> column to automatically initialize the column with the current timestamp when a new row is inserted.<\/li>\n\n\n\n<li>Set the <code>ON UPDATE CURRENT_TIMESTAMP<\/code> attribute to update the timestamp whenever the row is modified.<\/li>\n\n\n\n<li>MySQL stores the <code>TIMESTAMP<\/code> values in UTC format but converts them to the current session timezone when displayed.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"183\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/understanding-mysql-timestamp\/\"\n\t\t\t\tdata-post-title=\"MySQL TIMESTAMP Data Type\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"183\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/understanding-mysql-timestamp\/\"\n\t\t\t\tdata-post-title=\"MySQL TIMESTAMP Data Type\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial introduces you to MySQL TIMESTAMP and its features called automatic initialization and automatic update that allow you to define auto-initialized and auto-updated columns for a table.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":72,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-183","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL TIMESTAMP Data Type<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL TIMESTAMP Data Type\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-28T02:19:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx\",\"name\":\"MySQL TIMESTAMP Data Type\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2011\\\/03\\\/MySQL-TIMESTAMP-Automatic-Initialization.png\",\"datePublished\":\"2011-03-21T05:31:16+00:00\",\"dateModified\":\"2023-12-28T02:19:57+00:00\",\"description\":\"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2011\\\/03\\\/MySQL-TIMESTAMP-Automatic-Initialization.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2011\\\/03\\\/MySQL-TIMESTAMP-Automatic-Initialization.png\",\"width\":228,\"height\":41,\"caption\":\"MySQL TIMESTAMP - Automatic Initialization\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-timestamp.aspx#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL TIMESTAMP Data Type\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL TIMESTAMP Data Type","description":"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx","og_locale":"en_US","og_type":"article","og_title":"MySQL TIMESTAMP Data Type","og_description":"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-28T02:19:57+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx","url":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx","name":"MySQL TIMESTAMP Data Type","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png","datePublished":"2011-03-21T05:31:16+00:00","dateModified":"2023-12-28T02:19:57+00:00","description":"In this tutorial, you will learn about MySQL TIMESTAMP data type and how to use it to define columns that store timestamp data.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2011\/03\/MySQL-TIMESTAMP-Automatic-Initialization.png","width":228,"height":41,"caption":"MySQL TIMESTAMP - Automatic Initialization"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-timestamp.aspx#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL TIMESTAMP Data Type"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/183\/revisions"}],"predecessor-version":[{"id":13596,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/183\/revisions\/13596"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}