Detailed explanation of the difference between tinyint and int in MySQL

Detailed explanation of the difference between tinyint and int in MySQL

Question: What is the difference between int(1) and tinyint(1)?

With a design like this, I would never write int(1) anyway.

I checked and found that after setting the storage type in MySQL, the storage is fixed-length, that is, int(1) 和int(4) 在硬盤中所占的字節數都是一樣的.

We know that the int type occupies 4 bytes and tinyint occupies 1 byte. int(1) and int(4) are the same in terms of their length and storage method. The only difference is the displayed length. However, a parameter must be set: if列制定了zerofill 就會用0填充顯示. In this case, int(4) will be displayed as 0002.

int(1)和tinyint(4) 相比,肯定int 大.

Note that the number in parentheses after the numeric type does not indicate the length but the display width, which is different from the meaning of the number after varchar and char.

That is to say不管int 后面的數字是多少,它存儲的范圍始終是-2^31 到2^31 - 1 .

In summary, no matter how many numbers are in the brackets of the integer data type, the storage space occupied is the same.

tinyint 1 byte smallint 2 bytes MEDIUMINT 3 bytes

Obviously, int(1) 和tinyint(1) ,選擇tinyint(1) when designing a database.占的儲存空間越少越好,當然要夠用才行. To store a single-digit field like this, it is better to use tinyint(1).

Summarize:

  • 1. After the type is specified, the storage is fixed-length. Int(1) and int(4) are the same in terms of their length and storage method. In MySQL, the difference between int(1) and int(4) is the displayed length, but a parameter must be set: if the column is specified with zerofill, it will be filled with 0s. For example, if 2 int(3) is specified, it will be displayed as 002.
  • 2.int takes up 4 bytes for storage, tinyint takes up 1 byte for storage, and the storage length determines the different ranges of numbers they represent. The range of numbers represented by int is: integer data (all numbers) from -2^31 (-2,147,483,648) to 2^31 – 1 (2,147,483,647). tinyint represents numbers between 0 and 255.
  • 3. Tinyint(1) is no different from tinyint(3). It can store 123. However, if tinyint(3) is zerofilled, when the value 12 is inserted, 012 will be stored. Zerofill automatically pads the left side with zeros, which limits the display length.

The above summary is a bit messy. Here is a brief summary:

There is no difference between tinyint(1) and tinyint(3). They both occupy one byte and have the same storage range.

tinyint(3) zerofill , when the inserted data is less than 3 digits,左邊自動補零, which is to limit the display length

Int(1) and tinyint(1), if sufficient,優先選擇tinyint(1) because占字節少、節省空間.

tinyint 1 byte smallint 2 bytes MEDIUMINT 3 bytes int 4 bytes BIGINT 8 bytes.

However, the 5 in varchar(5) limits the number of characters that can be stored, regardless of their value (regardless of Chinese, English, numbers, etc.).

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed introduction to the differences between int, bigint, smallint and tinyint in MySQL
  • The value range of TINYINT in mysql
  • Detailed explanation of the integer data type tinyint in MySQL
  • Detailed Analysis of the Differences between Tinyint(1) and Tinyint(4) in MySQL

<<:  Issues with Rancher deployment and importing K8S clusters

>>:  Detailed explanation of TypeScript's basic types

Recommend

How to connect to MySQL visualization tool Navicat

After installing Navicat The following error may ...

How to use React to implement image recognition app

Let me show you the effect picture first. Persona...

React uses routing to redirect to the login interface

In the previous article, after configuring the we...

Detailed steps for remote deployment of MySQL database on Linux

Linux remote deployment of MySQL database, for yo...

Summary of twelve methods of Vue value transfer

Table of contents 1. From father to son 2. Son to...

How to install MySQL and MariaDB in Docker

Relationship between MySQL and MariaDB MariaDB da...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

Solution to MySQL restarting automatically

Preface Recently, a problem occurred in the test ...

Differences between Windows Server 2008R2, 2012, 2016, and 2019

Table of contents Common version introduction Com...

Detailed explanation of JavaScript to monitor route changes

Table of contents history pushState() Method push...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

About deploying a web project to Alibaba Cloud Server (5 steps to do it)

1. First log in to the Alibaba Cloud website to r...