{"id":87,"date":"2013-01-09T23:47:00","date_gmt":"2013-01-10T04:47:00","guid":{"rendered":"http:\/\/en.latindevelopers.com\/ivancp\/?p=87"},"modified":"2013-01-10T10:58:40","modified_gmt":"2013-01-10T15:58:40","slug":"mysql-the-strange-case-of-a-timestamp-field","status":"publish","type":"post","link":"http:\/\/en.latindevelopers.com\/ivancp\/2013\/mysql-the-strange-case-of-a-timestamp-field\/","title":{"rendered":"MySQL, the strange case of a timestamp field"},"content":{"rendered":"<p>I discovered a feature (or bug) of MySQL timestamp fields. Maybe is documented in some place what I not read yet:<\/p>\n<p>When I add a new timestamp field to a table, MySQL magically adds some features to new timestamp field like a &#8220;trigger&#8221; and a default value to CURRENT_TIMESTAMP.<\/p>\n<p>There is test-case script:<\/p>\n<pre lang=\"sql\">\r\n-- CREATING TABLE AND INSERT SOME DUMMY DATA\r\nmysql> create table t(\r\n    -> id int not null primary key auto_increment,\r\n    -> val varchar(50)\r\n    -> );\r\nQuery OK, 0 rows affected (0.15 sec)\r\n\r\nmysql> insert into t (val) values (\"foo\") ,(\"var\");\r\nQuery OK, 2 rows affected (0.08 sec)\r\nRecords: 2  Duplicates: 0  Warnings: 0\r\n\r\nmysql> select * from t;\r\n+----+------+\r\n| id | val  |\r\n+----+------+\r\n|  1 | foo  |\r\n|  2 | var  |\r\n+----+------+\r\n2 rows in set (0.00 sec)\r\n\r\n\r\n-- ADDING NEW TIMESTAMP FIELD AND ADD MORE DUMMY DATA\r\nmysql> alter table t add ts_field timestamp;\r\nQuery OK, 2 rows affected (0.35 sec)\r\nRecords: 2  Duplicates: 0  Warnings: 0\r\n\r\nmysql> insert into t (val) values (\"foo 2\") ,(\"var 2\");\r\nQuery OK, 2 rows affected (0.06 sec)\r\nRecords: 2  Duplicates: 0  Warnings: 0\r\n\r\n-- THERE IS THE MAGIC:\r\nmysql> select * from t;\r\n+----+-------+---------------------+\r\n| id | val   | ts_field            |\r\n+----+-------+---------------------+\r\n|  1 | foo   | 0000-00-00 00:00:00 |\r\n|  2 | var   | 0000-00-00 00:00:00 |\r\n|  3 | foo 2 | 2013-01-09 23:20:01 |    <---\r\n|  4 | var 2 | 2013-01-09 23:20:01 |    <---\r\n+----+-------+---------------------+\r\n4 rows in set (0.00 sec)\r\n<\/pre>\n<p>What happened? I dont know.<\/p>\n<p>The new table structure is:<\/p>\n<pre lang=\"text\">\r\nCREATE TABLE `t` (\r\n  `id` int(11) NOT NULL AUTO_INCREMENT,\r\n  `val` varchar(50) DEFAULT NULL,\r\n  `ts_field` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1\r\n<\/pre>\n<p>It only happens if the table doesn't have another timestamp field yet.<\/p>\n<p>Now, this is useful? maybe. This is a bug? maybe.<\/p>\n<p><strong>Update<\/strong>: This is a feature documented in <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/timestamp-initialization.html\" target=\"_blank\">https:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/timestamp-initialization.html<\/a>, sorry about my lapsus-post.<\/p>\n<p><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2013\/mysql-the-strange-case-of-a-timestamp-field\/\" data-text=\"MySQL, the strange case of a timestamp field\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2013\/mysql-the-strange-case-of-a-timestamp-field\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I discovered a feature (or bug) of MySQL timestamp fields. Maybe is documented in some place what I not read yet: When I add a new timestamp field to a table, MySQL magically adds some features to new timestamp field like a &#8220;trigger&#8221; and a default value to CURRENT_TIMESTAMP. There is test-case script: &#8212; CREATING [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2013\/mysql-the-strange-case-of-a-timestamp-field\/\" data-text=\"MySQL, the strange case of a timestamp field\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2013\/mysql-the-strange-case-of-a-timestamp-field\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[18,5,6],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2f09V-1p","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":7,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":94,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/87\/revisions\/94"}],"wp:attachment":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}