What does the n after int(n) in MySQL mean?

What does the n after int(n) in MySQL mean?

You may already know that the length 1 of int(1) does not represent the allowed storage width!

But many people haven't really studied what this length means. Today I will give a brief analysis!

Let's take a look at a simple example of table creation:

create table test(
 id int(11) unsigned NOT NULL AUTO_INCREMENT,
 uid int(3) NOT NULL,
 PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Here we take the uid field as an example, we set it to int(3)

So the question is, if we set int(3), can we not store the data 1234?

Then you can test it by entering the following SQL statement

insert into `test` (`uid`) VALUES(1234);
insert into `test` (`uid`) VALUES(12345678);

The resulting graph is as follows:

Friends can find that the data 1234 is successfully inserted through the above SQL statement, and we can also insert more digits of data! Why is this? See below

Here are the reasons:

We can simply understand this int(n) as:

This length is to tell MySQL that the width of the data stored in this field is n digits. Of course, if you do not store n digits, but (as long as it is within the storage range of this type) MySQL can also store it normally!

Then we can create the test2 table again, and this time we add the following constraints to the uid field: unsigned and zerofill

==Field constraints will be discussed in detail later==

The MySQL code is as follows:

create table test2(
 id int(11) unsigned NOT NULL AUTO_INCREMENT,
 uid int(3) unsigned zerofill NOT NULL,
 PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Now my uid field: length (n) = 3, field constraint = unsigned and zerofill: (unsigned constraint and use 0 to fill the digit constraint)

After setting this constraint field, when inserting data into the table, the system will automatically fill the uid field with 0 on the left if it is less than 3 digits.

You can test the code by inserting a number 33 into the uid field.

insert into `test2` (`uid`) VALUES(33);

The resulting graph is as follows:

Are you surprised to find that when the length is less than 3, it is really filled with 0s from the left! Hahahaha

So: Now we should know clearly that the length n after int has nothing to do with the size of the numeric value you store!

==Summary:==

When defining the table field data type as int, the length n behind it is meaningless. As long as it is within the storage range of this type, MySQL can store it normally! If you must pad 0 on the left, then this field must have a zerofill constraint and an unsigned constraint!

This is the end of this article about what the n after int(n) in MySQL means. For more relevant MySQL int(n) content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Difference between int(10) and int(11) in MySQL
  • Are the value ranges of int(3) and int(10) the same in mysql
  • Detailed explanation of the difference between MySQL int(3) and int(11)
  • A brief discussion on the difference between int(1) and int(10) in MySQL

<<:  JavaScript implements password box input verification

>>:  Detailed explanation of the solution to docker-compose being too slow

Recommend

Installation process of CentOS8 Linux 8.0.1905 (illustration)

As of now, the latest version of CentOS is CentOS...

Usage of the target attribute of the html tag a

1: If you use the tag <a> to link to a page,...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

Comprehensive explanation of CocosCreator hot update

Table of contents Preface What is Hot Change Coco...

CSS multi-column layout solution

1. Fixed width + adaptive Expected effect: fixed ...

Solutions to black screen when installing Ubuntu (3 types)

My computer graphics card is Nvidia graphics card...

html+css+js to realize the function of photo preview and upload picture

Preface: When we are making web pages, we often n...

Notes on configuring multiple proxies using vue projects

In the development process of Vue project, for th...

How to migrate the data directory in mysql8.0.20

The default storage directory of mysql is /var/li...

Analyzing the node event loop and message queue

Table of contents What is async? Why do we need a...

CentOS 7.x docker uses overlay2 storage method

Edit /etc/docker/daemon.json and add the followin...

How to implement checkbox & radio alignment

Not only do different browsers behave differently...