{"id":29139,"date":"2024-04-29T18:14:46","date_gmt":"2024-04-29T17:14:46","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/como-maneja-mysql-los-valores-boolean-mediante-tinyint1\/"},"modified":"2024-06-03T17:44:40","modified_gmt":"2024-06-03T16:44:40","slug":"como-maneja-mysql-los-valores-boolean-mediante-tinyint1","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/how-does-mysql-handle-boolean-values-using-tinyint1\/","title":{"rendered":"How MySQL Handles Boolean Values Using TINYINT(1)"},"content":{"rendered":"<p>When it comes to working with databases, especially MySQL, it is essential to understand how data types and their implementation are handled. One of the most interesting aspects is the treatment of Boolean values. Although superficially one might think that MySQL has a specific type of data <code>BOOLEAN<\/code>, the reality is that it is intrinsically linked to the type <code>TINYINT(1)<\/code>. Throughout this text, we will explore how MySQL manages these values and the practical implications this has on database design and management.<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#El_Tipo_de_Dato_TINYINT1\" >The Data Type TINYINT(1)<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#Implementacion_y_Uso\" >Implementation and Use<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#Consultas_y_Condicionales\" >Consultations and Conditions<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#Implicaciones_de_Performance\" >Performance Implications<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#Buenas_Practicas\" >Good practices<\/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\/how-does-mysql-handle-boolean-values-using-tinyint1\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"El_Tipo_de_Dato_TINYINT1\"><\/span>The Data Type <code>TINYINT(1)<\/code><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In MySQL, the type <code>BOOLEAN<\/code> is a synonym for the data type <code>TINYINT(1)<\/code>. This means that, although <code>BOOLEAN<\/code> can be specified in table definitions, MySQL internally converts it to <code>TINYINT(1)<\/code>. Each <code>TINYINT(1)<\/code> takes up one byte of storage and can store numeric values between -128 and 127. However, when used as a boolean, the values are restricted to 0 (false) and 1 (true).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Implementacion_y_Uso\"><\/span>Implementation and Use<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you declare a column as <code>BOOLEAN<\/code> in MySQL, internally a column is being created <code>TINYINT(1)<\/code>. This can be somewhat confusing for developers coming from other database management systems that explicitly support the data type <code>BOOLEAN<\/code>.<\/p>\n<p>To assign a value to a boolean field in MySQL, you can use 0 and 1. Also, MySQL supports true and false values using the words <code>TRUE<\/code> y <code>FALSE<\/code>, which are aliases of 1 and 0, respectively. Let&#039;s look at an example:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE example ( is_active BOOLEAN ); INSERT INTO example VALUES (TRUE); INSERT INTO example VALUES (FALSE);<\/code><\/pre>\n<p>In this case, although we are using <code>TRUE<\/code> y <code>FALSE<\/code>, MySQL stores them as 1 and 0.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Consultas_y_Condicionales\"><\/span>Consultations and Conditions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When performing queries that involve boolean fields, it is possible to use both 0 and 1 or <code>TRUE<\/code> y <code>FALSE<\/code>. This makes the code more readable and easier to understand, especially for those who are not familiar with the equivalence between <code>BOOLEAN<\/code> y <code>TINYINT(1)<\/code>.<\/p>\n<p>For example, if we want to select all the records where <code>is_active<\/code> is true, we can write:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT * FROM example WHERE is_active = TRUE;<\/code><\/pre>\n<p>o <\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT * FROM example WHERE is_active = 1;<\/code><\/pre>\n<p>Both queries will return the same result, but the first one is more explicit about the intent.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Implicaciones_de_Performance\"><\/span>Performance Implications<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Wear <code>TINYINT(1)<\/code> to represent boolean values is efficient in terms of storage, since only one byte is used per field. Compared to other systems that could offer a type of data <code>BOOLEAN<\/code> native that takes up less space, could seem like a disadvantage. However, in practice the performance difference is negligible for most applications.<\/p>\n<p>Furthermore, the use of <code>TINYINT(1)<\/code> offers flexibility as it technically allows other values to be stored besides 0 and 1, which can be useful in certain scenarios where multiple states need to be represented.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Buenas_Practicas\"><\/span>Good practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although MySQL allows you to use any value within the range of <code>TINYINT<\/code> To represent booleans, it is good practice to restrict this use to 0 and 1. This ensures data consistency and avoids confusion in data handling. Furthermore, it is advisable to use <code>TRUE<\/code> y <code>FALSE<\/code> in insertion and query operations to improve code readability.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Understand how MySQL handles boolean values using <code>TINYINT(1)<\/code> It is crucial for the effective and efficient development of database-driven applications. Although on the surface it may seem like an unusual approach, it has its advantages in terms of flexibility and simplicity.<\/p>\n<p>In summary, the correct handling of data types in MySQL, such as the case of <code>BOOLEAN<\/code>, not only contributes to performance and efficiency, but also significantly affects the quality of the code and its long-term maintainability.<\/p>","protected":false},"excerpt":{"rendered":"<p>When it comes to working with databases, especially MySQL, it is essential to understand how data types are handled and their implementation. One of the most interesting aspects is the treatment of boolean values. Although superficially you might think that MySQL has a specific BOOLEAN data type, the reality is that there is no such thing as a boolean type.<\/p>","protected":false},"author":1,"featured_media":29140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185,2033],"tags":[38,205,2160,2189,639,221,1807,366,352,2161,2194,924],"class_list":["post-29139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","category-mysql-data-types","tag-como","tag-blog","tag-boolean","tag-curso","tag-data","tag-los","tag-maneja","tag-mediante","tag-mysql","tag-tinyint1","tag-types","tag-valores"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29139","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=29139"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29139\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29140"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}