{"id":59,"date":"2011-03-24T11:43:59","date_gmt":"2011-03-24T17:43:59","guid":{"rendered":"http:\/\/www.businesstechnologyassociates.com\/blog\/?p=59"},"modified":"2011-03-24T11:47:02","modified_gmt":"2011-03-24T17:47:02","slug":"mysql-setting-date-field-default-to-current-date","status":"publish","type":"post","link":"https:\/\/www.businesstechnologyassociates.com\/blog\/2011\/03\/mysql-setting-date-field-default-to-current-date\/","title":{"rendered":"MySQL: Setting date field default to current date"},"content":{"rendered":"<p>In MySQL, creating a date\/time stamp field that defaults to the current date\/time has been a problem in the past.\u00a0 MySQL restricted you to one timestamp field per table that is automatically updated with the current date\/time.\u00a0 Early versions of MySQL allowed multiple updating timestamp fields contrary to the specification, but this was &#8220;fixed&#8221; in later versions.\u00a0 Then triggers were added to MySQL v5.0.2 and can be used as a solution for this common issue.<\/p>\n<pre><code>CREATE TRIGGER `TableName_dtColumn_default` BEFORE INSERT ON `TableName`  FOR EACH ROW SET NEW.dtColumn = CURRENT_TIMESTAMP;<\/code><\/pre>\n<p>Replace &#8220;TableName&#8221; with your table&#8217;s name and &#8220;dtColumn&#8221; with your date\/time column&#8217;s name.\u00a0 Also the trigger name, &#8220;TableName_dtColumn_default&#8221; in this example, must be unique within a schema.<\/p>\n<p>MySQL triggers are a very versatile feature.\u00a0 For more details about creating and using triggers see the MySQL documentation for <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/create-trigger.html\">CREATE TRIGGER<\/a> syntax.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, creating a date\/time stamp field that defaults to the current date\/time has been a problem in the past.\u00a0 MySQL restricted you to one timestamp field per table that is automatically updated with the current date\/time.\u00a0 Early versions of MySQL allowed multiple updating timestamp fields contrary to the specification, but this was &#8220;fixed&#8221; in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.businesstechnologyassociates.com\/blog\/2011\/03\/mysql-setting-date-field-default-to-current-date\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MySQL: Setting date field default to current date&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,3],"tags":[15,29,16],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-mysql","category-technology","tag-database","tag-mysql","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":10,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}