{"id":28981,"date":"2024-04-01T05:52:37","date_gmt":"2024-04-01T04:52:37","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/domina-el-tipo-de-dato-int-en-mysql-con-ejemplos-practicos\/"},"modified":"2024-06-03T17:43:41","modified_gmt":"2024-06-03T16:43:41","slug":"domina-el-tipo-de-dato-int-en-mysql-con-ejemplos-practicos","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/master-the-int-data-type-in-mysql-with-practical-examples\/","title":{"rendered":"Master the INT Data Type in MySQL with Practical Examples"},"content":{"rendered":"<p>A detailed journey through handling integers in MySQL may prove to be more exciting and useful than you think. Whether you&#039;re just starting out in the world of databases or simply need to hone your knowledge of specific data types, understanding the INT data type is essential. In working practice, integers are a crucial part of defining database schemas and optimizing the performance of SQL queries. Let&#039;s dive into how to use integers in MySQL effectively.<\/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-the-int-data-type-in-mysql-with-practical-examples\/#%C2%BFQue_es_INT_en_MySQL\" >What is INT 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\/master-the-int-data-type-in-mysql-with-practical-examples\/#Creando_una_tabla_con_INT\" >Creating a table with INT<\/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\/master-the-int-data-type-in-mysql-with-practical-examples\/#Insertando_y_manipulando_datos_enteros\" >Inserting and manipulating integer data<\/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\/master-the-int-data-type-in-mysql-with-practical-examples\/#Consultas_y_operaciones_con_INT\" >Consultations and operations with INT<\/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\/master-the-int-data-type-in-mysql-with-practical-examples\/#Optimizacion_y_buen_uso_del_INT\" >Optimization and good use of INT<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_INT_en_MySQL\"><\/span>What is INT in MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>INT<\/strong> or integer, is a common data type in SQL used to store integers, that is, numbers without fractions or decimal numbers. Depending on how we declare this data type, it can store both positive and negative numbers.<\/p>\n<p>MySQL offers several types of integers, but the most common is INT. An INT type takes up 4 bytes of storage and can contain values from -2,147,483,648 to 2,147,483,647. For applications that require a smaller or larger range, MySQL offers other types such as <code>TINYINT<\/code>, <code>SMALLINT<\/code>, <code>MEDIUMINT<\/code>, and <code>BIGINT<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Creando_una_tabla_con_INT\"><\/span>Creating a table with INT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Suppose we are implementing a system for a small e-commerce. We are going to create a table to store information about the products that the store has available:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE products ( product_id INT AUTO_INCREMENT, name VARCHAR(100), price INT, quantity_in_stock INT, PRIMARY KEY(product_id) );<\/code><\/pre>\n<p>In this example, <code>product_id<\/code> It is a column that we use as a primary key that autoincrements every time a new product is added. Fields <code>price<\/code> y <code>quantity_in_stock<\/code> They are integers.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Insertando_y_manipulando_datos_enteros\"><\/span>Inserting and manipulating integer data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now that we have our table, let&#039;s insert some products:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">INSERT INTO products (name, price, quantity_in_stock) VALUES (&#039;Sneakers&#039;, 79900, 12); INSERT INTO products (name, price, quantity_in_stock) VALUES (&#039;Polyester T-shirt&#039;, 19900, 30);<\/code><\/pre>\n<p>By observing the operations, you will notice that it is easy to handle integers in SQL statements. We can modify any value of these columns using simple update commands:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">UPDATE products SET quantity_in_stock = 10 WHERE product_id = 1;<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Consultas_y_operaciones_con_INT\"><\/span>Consultations and operations with INT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Imagine that we want to make a report of the average, minimum and maximum prices of our products. SQL aggregation functions make working with <code>INT<\/code> be very efficient:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT AVG(price) AS average_price, MIN(price) AS minimum_price, MAX(price) AS maximum_price FROM products;<\/code><\/pre>\n<p>Outputs like these are key to making business decisions based on accurate data that is quick to calculate thanks to the use of integers.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Optimizacion_y_buen_uso_del_INT\"><\/span>Optimization and good use of INT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Finally, it is vital to know that the INT data type, despite being very useful, should be used with caution. Use <code>INT<\/code> It can unnecessarily consume storage and processing space unnecessarily. For example, if you know that a field will never exceed a value of 255, it is more optimized to use <code>TINYINT<\/code> rather <code>INT<\/code>.<\/p>\n<p>If you would like to read more about database techniques and tips, I invite you to visit my <a href=\"https:\/\/nelkodev.com\/en\/\">Blog<\/a> where I explore these and other topics that might interest you in depth. And if you have any questions or need to contact me, feel free to visit <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">my contact section<\/a>.<\/p>\n<p>Studying, implementing and perfecting the use of data types such as INT in MySQL will not only make you better at database management, but will optimize your applications by making better use of resources. So, are you ready to put into practice everything you&#039;ve learned about the INT data type in MySQL? Forward!<\/p>","protected":false},"excerpt":{"rendered":"<p>Un viaje detallado por el manejo de los enteros en MySQL puede revelarse m\u00e1s emocionante y provechoso de lo que piensas. Ya sea que est\u00e9s comenzando en el mundo de las bases de datos o simplemente necesites afinar tus conocimientos en tipos de datos espec\u00edficos, entender el tipo de dato INT es fundamental. En la [&hellip;]<\/p>","protected":false},"author":1,"featured_media":28982,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185,2033],"tags":[205,90,2189,639,2030,1473,977,2074,352,1303,645,2194],"class_list":["post-28981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","category-mysql-data-types","tag-blog","tag-con","tag-curso","tag-data","tag-dato","tag-domina","tag-ejemplos","tag-int","tag-mysql","tag-practicos","tag-tipo","tag-types"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28981","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=28981"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28982"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}