{"id":29129,"date":"2024-04-26T13:36:05","date_gmt":"2024-04-26T12:36:05","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/aprende-a-configurar-valores-predeterminados-en-mysql\/"},"modified":"2024-06-03T17:44:36","modified_gmt":"2024-06-03T16:44:36","slug":"aprende-a-configurar-valores-predeterminados-en-mysql","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/learn-how-to-set-default-values-in-mysql\/","title":{"rendered":"Learn to Configure Default Values in MySQL"},"content":{"rendered":"<p>Default values in databases are essential to ensure the integrity and efficiency of stored information. In MySQL, the use of constraint <code>DEFAULT<\/code> in the definition of columns of a table allows you to specify a default value that will be assigned to the column when a specific value is not provided in a data insertion. Next, we&#039;ll explore how you can apply this functionality in your database projects. <\/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\/learn-how-to-set-default-values-in-mysql\/#%C2%BFQue_es_la_Restriccion_DEFAULT_en_MySQL\" >What is DEFAULT Constraint in MySQL?<\/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\/learn-how-to-set-default-values-in-mysql\/#Definiendo_un_valor_DEFAULT_al_crear_tablas\" >Defining a DEFAULT value when creating tables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/learn-how-to-set-default-values-in-mysql\/#Modificando_valores_DEFAULT_en_tablas_existentes\" >Modifying DEFAULT values in existing tables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/learn-how-to-set-default-values-in-mysql\/#Uso_practico_de_DEFAULT_en_situaciones_comunes\" >Practical use of DEFAULT in common situations<\/a><\/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\/learn-how-to-set-default-values-in-mysql\/#Consideraciones_importantes_sobre_DEFAULT\" >Important considerations about DEFAULT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/learn-how-to-set-default-values-in-mysql\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_la_Restriccion_DEFAULT_en_MySQL\"><\/span>What is DEFAULT Constraint in MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The restriction <code>DEFAULT<\/code> in MySQL it is a way to set an automatic value for a column in a table. When a record is inserted without specifying a value for that column, MySQL automatically assigns the defined default value. This behavior ensures that you never have a column with no value if required, which is crucial to maintaining the logic of your data and avoiding unexpected errors in your applications.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Definiendo_un_valor_DEFAULT_al_crear_tablas\"><\/span>Defining a DEFAULT value when creating tables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To start working with default values, you need to know how to define them when creating your tables. Here is the basic schematic to do it:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE example ( id INT AUTO_INCREMENT, name VARCHAR(100), record_date DATE DEFAULT CURRENT_DATE, asset BOOLEAN DEFAULT TRUE, PRIMARY KEY (id) );<\/code><\/pre>\n<p>In this example, the table <code>example<\/code> has a column <code>registration date<\/code> with a default value that is the current date (<code>CURRENT_DATE<\/code>) and a column <code>asset<\/code> which by default is <code>TRUE<\/code>. This means that if you do not specify values for <code>registration date<\/code> y <code>asset<\/code> When inserting a new record, MySQL will assign these default values automatically.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Modificando_valores_DEFAULT_en_tablas_existentes\"><\/span>Modifying DEFAULT values in existing tables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You may already have a table created and need to add or change a default value. For this, we will use the command <code>ALTER TABLE<\/code>. For example, if you want to add a default value to the column <code>name<\/code> In the table <code>example<\/code>, you could do it like this:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">ALTER TABLE example MODIFY name VARCHAR(100) DEFAULT &#039;New User&#039;;<\/code><\/pre>\n<p>With this command, every time a record is inserted without specifying the <code>name<\/code>, you will automatically be assigned &#039;New User&#039; to that column.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Uso_practico_de_DEFAULT_en_situaciones_comunes\"><\/span>Practical use of DEFAULT in common situations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Imagine you are designing an app where users can register, but not all of them provide their date of birth. You could configure the corresponding column so that, instead of being empty, it is filled with a default date that could be processed specially in your business logic.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">ALTER TABLE users ADD COLUMN date_of birth DATE DEFAULT &#039;1900-01-01&#039;;<\/code><\/pre>\n<p>With this, any analysis you perform on the ages of the users could be adjusted to consider that a date of birth <code>1900-01-01<\/code> indicates that the actual data was not provided.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Consideraciones_importantes_sobre_DEFAULT\"><\/span>Important considerations about DEFAULT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li><strong>Type of data:<\/strong> The value <code>DEFAULT<\/code> must be the same type as the column or a compatible function.<\/li>\n<li><strong>Features:<\/strong> MySQL allows the use of SQL functions as part of a value <code>DEFAULT<\/code>, as <code>CURRENT_DATE<\/code>.<\/li>\n<li><strong>Limitations:<\/strong> Not all data types allow default values. For example, data types <code>TEXT<\/code> y <code>BLOB<\/code> they do not admit a <code>DEFAULT<\/code>.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Proper handling of the restriction <code>DEFAULT<\/code> It will allow you to design more robust databases with more predictable data management. Whether you are creating new tables or modifying existing ones, understanding how to implement default values will give you a huge advantage in managing your data.<\/p>\n<p>To learn more about advanced techniques in MySQL and other relevant topics, feel free to visit <a href=\"https:\/\/nelkodev.com\/en\/\">my blog at NelkoDev<\/a> and if you have any specific questions or need help, <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Los valores predeterminados en las bases de datos son esenciales para garantizar la integridad y la eficiencia de la informaci\u00f3n almacenada. En MySQL, el uso de la restricci\u00f3n DEFAULT en la definici\u00f3n de columnas de una tabla permite especificar un valor por defecto que se asignar\u00e1 a la columna cuando no se proporcione un valor [&hellip;]<\/p>","protected":false},"author":1,"featured_media":29130,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185],"tags":[486,205,2156,1159,2189,352,2157,924],"class_list":["post-29129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","tag-aprende","tag-blog","tag-configurar","tag-constraints","tag-curso","tag-mysql","tag-predeterminados","tag-valores"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29129","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=29129"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29129\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29130"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}