{"id":29123,"date":"2024-04-28T22:45:53","date_gmt":"2024-04-28T21:45:53","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/domina-timestamp-en-mysql-funciones-de-inicializacion-y-actualizacion\/"},"modified":"2024-06-03T17:44:34","modified_gmt":"2024-06-03T16:44:34","slug":"domina-timestamp-en-mysql-funciones-de-inicializacion-y-actualizacion","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/","title":{"rendered":"Master TIMESTAMP in MySQL: Initialization and Update Functions"},"content":{"rendered":"<p>In the vast world of databases, especially when working with MySQL, understanding how to handle dates and times is crucial. A field that usually generates certain doubts and that is essential to master is TIMESTAMP. This data type in MySQL not only stores timestamps, but also offers very useful initialization and automatic update features. Below, we&#039;ll explore in detail how you can get the most out of TIMESTAMP, ensuring your database is more dynamic and easier to maintain.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#%C2%BFQue_es_TIMESTAMP\" >What is TIMESTAMP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Caracteristicas_Basicas_del_TIMESTAMP\" >Basic Features of TIMESTAMP<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Almacenamiento\" >Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Time_Zone\" >TimeZone<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Inicializacion_y_Actualizacion_Automatica\" >Automatic Initialization and Update<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Inicializacion_Automatica\" >Automatic Initialization<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Actualizacion_Automatica\" >Automatic Update<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Mejores_Practicas_y_Consideraciones\" >Best Practices and Considerations<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Unico_TIMESTAMP_con_InicializacionActualizacion_Automatica\" >Unique TIMESTAMP with Automatic Initialization\/Update<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Uso_de_TIMESTAMP_vs_DATETIME\" >Using TIMESTAMP vs DATETIME<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-timestamp-in-mysql-initialization-and-update-functions\/#Aplicando_TIMESTAMP_en_la_Practica\" >Applying TIMESTAMP in Practice<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_TIMESTAMP\"><\/span>What is TIMESTAMP?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>TIMESTAMP is a data type used in SQL that stores a combination of date and time in a particular format. This field has the ability to record the exact moment an event occurs, which is especially useful in data creation or modification records. A characteristic aspect of TIMESTAMP is that it has a useful date range that goes from &#039;1970-01-01 00:00:01&#039; UTC to &#039;2038-01-19 03:14:07&#039; UTC.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Caracteristicas_Basicas_del_TIMESTAMP\"><\/span>Basic Features of TIMESTAMP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before delving into the advanced functionalities of TIMESTAMP, it is essential to describe its fundamentals:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Almacenamiento\"><\/span>Storage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>TIMESTAMP requires only 4 bytes of storage. This is quite efficient, especially compared to other date and time data types like DATETIME, which needs 8 bytes.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Time_Zone\"><\/span>TimeZone<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A crucial difference between TIMESTAMP and DATETIME is how they handle time zones. While DATETIME stores the exact date and time that is entered, regardless of time zone, TIMESTAMP converts the stored date and time to UTC for storage, and converts them back to the server&#039;s local time zone when retrieved. This makes TIMESTAMP ideal for applications running in multiple time zones.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Inicializacion_y_Actualizacion_Automatica\"><\/span>Automatic Initialization and Update<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>One of the most powerful features of TIMESTAMP is its ability to initialize and update itself. Let&#039;s see how you can use these features in your projects.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Inicializacion_Automatica\"><\/span>Automatic Initialization<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>MySQL allows a TIMESTAMP field to be automatically initialized to the current date and time when the record is created. This is easily achieved by specifying <code>DEFAULT CURRENT_TIMESTAMP<\/code> in the field definition:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE Example( id INT, creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP );<\/code><\/pre>\n<p>In this example, every time a new record is inserted, <code>Creation date<\/code> will automatically be set to the date and time at the time of insertion.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Actualizacion_Automatica\"><\/span>Automatic Update<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Similarly, TIMESTAMP can be configured to automatically update its value whenever other fields in the record are modified. This is achieved using <code>ON UPDATE CURRENT_TIMESTAMP<\/code>:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE Example( id INT, creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, modification_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );<\/code><\/pre>\n<p>Here, <code>modification date<\/code> It will automatically update to the current date and time whenever any part of the record changes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Mejores_Practicas_y_Consideraciones\"><\/span>Best Practices and Considerations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Unico_TIMESTAMP_con_InicializacionActualizacion_Automatica\"><\/span>Unique TIMESTAMP with Automatic Initialization\/Update<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It is important to note that in MySQL versions prior to 5.6, only the first TIMESTAMP field in the table is allowed to be automatically initialized or updated. If you need more than one field to have these properties, we recommend upgrading to MySQL 5.6 or higher, where this limitation does not exist.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Uso_de_TIMESTAMP_vs_DATETIME\"><\/span>Using TIMESTAMP vs DATETIME<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Although TIMESTAMP and DATETIME may seem similar, it is crucial to choose wisely between one or the other depending on the context. Use TIMESTAMP when you need to take time zones into account, especially in globally accessible applications. DATETIME will be your best option when you do not want the logs to change with server time zone changes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Aplicando_TIMESTAMP_en_la_Practica\"><\/span>Applying TIMESTAMP in Practice<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To really understand the power of TIMESTAMP&#039;s capabilities, I invite you to experiment with the provided code and see how these fields behave in real situations. See how they react when you insert and modify data, and how this can simplify the management of time records in your applications.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the vast world of databases, especially when working with MySQL, understanding how to handle dates and times is crucial. One field that often raises some questions and is essential to master is TIMESTAMP. This data type in MySQL not only stores timestamps, but also offers very useful features.<\/p>","protected":false},"author":1,"featured_media":29124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185,2033],"tags":[2154,205,2189,639,1473,147,2152,352,2153,2194],"class_list":["post-29123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","category-mysql-data-types","tag-actualizacion","tag-blog","tag-curso","tag-data","tag-domina","tag-funciones","tag-inicializacion","tag-mysql","tag-timestamp","tag-types"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=29123"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29124"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}