{"id":3219,"date":"2020-09-16T17:43:34","date_gmt":"2020-09-16T22:43:34","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=3219"},"modified":"2020-09-16T17:43:36","modified_gmt":"2020-09-16T22:43:36","slug":"mysql-how-to-automatically-update-timestamp-column-in-phpmyadmin-when-a-row-is-edited","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2020\/09\/16\/mysql-how-to-automatically-update-timestamp-column-in-phpmyadmin-when-a-row-is-edited\/","title":{"rendered":"MySQL: How to automatically update timestamp column in phpMyAdmin when a row is edited"},"content":{"rendered":"\n<p>I have a table in my database called &#8220;usercardsetplaycounts&#8221; that has a column called &#8220;updatedAt&#8221; that I want to get updated with a fresh timestamp every time a row is edited. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image.png\" alt=\"\" class=\"wp-image-3221\" width=\"416\" height=\"190\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image.png 831w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-300x137.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-768x350.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-676x308.png 676w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/figure>\n\n\n\n<p>When I originally set this column up, I set the default to <code>CURRENT_TIMESTAMP<\/code>. Here is what it looks like in phyMyAdmin:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"160\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-1024x160.png\" alt=\"\" class=\"wp-image-3222\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-1024x160.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-300x47.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-768x120.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-1536x240.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1-676x106.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-1.png 1812w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The updatedAt date is set to the timestamp of when the table was created, and does not change when rows are edited. <\/p>\n\n\n\n<p>To fix this, I went to the Structure view in phpMyAdmin and clicked the &#8220;Change&#8221; link in the Action column. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"225\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-1024x225.png\" alt=\"\" class=\"wp-image-3225\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-1024x225.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-300x66.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-768x169.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-1536x338.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2-676x149.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-2.png 1803w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Which then loads the details for this column:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"106\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-1024x106.png\" alt=\"\" class=\"wp-image-3226\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-1024x106.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-300x31.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-768x80.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-1536x159.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-2048x212.png 2048w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-3-676x70.png 676w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>I then changed the Type from <code>DATETIME<\/code> to <code>TIMESTAMP<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4-1024x734.png\" alt=\"\" class=\"wp-image-3227\" width=\"512\" height=\"367\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4-1024x734.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4-300x215.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4-768x551.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4-676x485.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-4.png 1096w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>And I changed the Attributes from blank to &#8220;on update CURRENT_TIMESTAMP&#8221;. For example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-5.png\" alt=\"\" class=\"wp-image-3228\" width=\"395\" height=\"249\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-5.png 527w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-5-300x189.png 300w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/figure>\n\n\n\n<p>Then on the right side of the page, I clicked the &#8220;Preview SQL&#8221; button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-7.png\" alt=\"\" class=\"wp-image-3231\" width=\"269\" height=\"146\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-7.png 359w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-7-300x162.png 300w\" sizes=\"auto, (max-width: 269px) 100vw, 269px\" \/><\/figure>\n\n\n\n<p>Which made this small box appear with the SQL that is being executed:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-8.png\" alt=\"\" class=\"wp-image-3232\" width=\"623\" height=\"246\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-8.png 831w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-8-300x118.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-8-768x303.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-8-676x267.png 676w\" sizes=\"auto, (max-width: 623px) 100vw, 623px\" \/><\/figure>\n\n\n\n<p>The actual SQL code for this table update is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ALTER TABLE `usercardsetplaycounts` CHANGE `updatedAt` `updatedAt` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;<\/pre>\n\n\n\n<p>After closing this dialog, all I had to do was click the &#8220;Save&#8221; button to make this change to the database. Now whenever any row in this table is updated, the updatedAt column will contain a fresh timestamp for that row. Here is what the Structure looks like after the change:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-1024x225.png\" alt=\"\" class=\"wp-image-3234\" width=\"1024\" height=\"225\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-1024x225.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-300x66.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-768x168.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-1536x337.png 1536w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-2048x449.png 2048w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/09\/image-9-676x148.png 676w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a table in my database called &#8220;usercardsetplaycounts&#8221; that has a column called &#8220;updatedAt&#8221; that I want to get updated with a fresh timestamp every time a row is edited. When I originally set this column up, I set the default to CURRENT_TIMESTAMP. Here is what it looks like in phyMyAdmin: The updatedAt date &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2020\/09\/16\/mysql-how-to-automatically-update-timestamp-column-in-phpmyadmin-when-a-row-is-edited\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">MySQL: How to automatically update timestamp column in phpMyAdmin when a row is edited<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[108,220],"class_list":["post-3219","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql","tag-phpmyadmin"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=3219"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3219\/revisions"}],"predecessor-version":[{"id":3237,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3219\/revisions\/3237"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=3219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=3219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=3219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}