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

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...

Let me teach you how to use font icons in CSS

First of all, what is a font icon? On the surface...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

Vue implements tree table

This article example shares the specific code of ...

Solve the problem of mysql data loss when docker restarts redis

Official documentation: So mysql should be starte...

VUE+Canvas implements the sample code of the desktop pinball brick-breaking game

Everyone has played the pinball and brick-breakin...

JS deep and shallow copy details

Table of contents 1. What does shallow copy mean?...

Docker-compose image release process analysis of springboot project

Introduction The Docker-Compose project is an off...

How to stop CSS animation midway and maintain the posture

Preface I once encountered a difficult problem. I...

Vue-cli framework implements timer application

Technical Background This application uses the vu...

Analyzing Linux high-performance network IO and Reactor model

Table of contents 1. Introduction to basic concep...

Tutorial on installing mysql under centos7

Recently, I plan to deploy a cloud disk on my hom...