{"id":30,"date":"2014-08-23T11:22:57","date_gmt":"2014-08-23T11:22:57","guid":{"rendered":"http:\/\/baselines.com\/blog\/?p=30"},"modified":"2014-08-23T14:04:25","modified_gmt":"2014-08-23T14:04:25","slug":"mysql-date-and-time-fields","status":"publish","type":"post","link":"https:\/\/baselines.com\/blog\/?p=30","title":{"rendered":"MySQL Date and Time Fields"},"content":{"rendered":"<h1 class=\"entry-title\">How to create a TIMESTAMP\/DATETIME column in MySQL to automatically be set with the current time<\/h1>\n<p>The TIMESTAMP data type is the only way to have MySQL automatically set the time when a row was inserted and\/or updated. DATETIME columns can\u2019t do this.<\/p>\n<p>TIMESTAMP columns are identical to DATETIME columns with one important exception \u2014 they can be set to take the current time value when a row is created or updated.<\/p>\n<p>You can define more than one TIMESTAMP colum in a table \u2014 however, <strong>only ONE TIMESTAMP column in a table can be configured for Auto-Update or Auto-Initialization<\/strong>.<\/p>\n<p>(For that reason, I usually make a practice of setting other columns to \u2018DATETIME\u2019 so there\u2019s only one \u2018TIMESTAMP\u2019 column per table.)<\/p>\n<p>There are four options when using TIMESTAMP, they are:<\/p>\n<ul>\n<li>Auto-initialization and auto-update:<\/li>\n<\/ul>\n<pre>  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP<\/pre>\n<ul>\n<li>Auto-initialization only:<\/li>\n<\/ul>\n<pre>  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP<\/pre>\n<ul>\n<li>Auto-update only:<\/li>\n<\/ul>\n<pre>  ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP<\/pre>\n<ul>\n<li>Neither:<\/li>\n<\/ul>\n<pre>  ts TIMESTAMP DEFAULT 0<\/pre>\n<p>&nbsp;<\/p>\n<p>Here\u2019s an example of a complete \u2018CREATE TABLE\u2019 statement that shows how it all works:<\/p>\n<pre>CREATE TABLE `foo`.`timestamp_example` (\r\n`id`                  INTEGER(10) UNSIGNED AUTO_INCREMENT,\r\n`some_text_field`     VARCHAR(20),\r\n`updated_at`          TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n`created_at`          DATETIME DEFAULT NULL,\r\nPRIMARY KEY (id)\r\n);\r\n<\/pre>\n<p>Note that only one of the fields can be TIMESTAMP, that\u2019s a limitation of MySQL.<\/p>\n<p>The choice to use the TIMESTAMP on the \u2018updated_at\u2019 field was random in this example. Choose whatever makes sense for your application.<\/p>\n<p class=\"back_to_toc\"><a href=\"http:\/\/www.kbedell.com\/mysql-programming\/\">Click here to go to the table of contents for all MySQL Programming Example Code<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to create a TIMESTAMP\/DATETIME column in MySQL to automatically be set with the current time The TIMESTAMP data type is the only way to have MySQL automatically set the time when a row was inserted and\/or updated. DATETIME columns can\u2019t do this. TIMESTAMP columns are identical to DATETIME columns with one important exception \u2014 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":1,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}