The difference between int (x) and int and varchar and char in mysql

The difference between int (x) and int and varchar and char in mysql


Preface

In the process of database design using mysql, when defining metadata, we need to clearly know the difference between each data type to facilitate our standardized design. The database design is not standardized, which will cause unexpected troubles in the development process. Therefore, we need to clarify the basic concepts. Below, I will briefly summarize the int, char, and varchar that we often use. I hope to see friends who leave a message actively, welcome to correct me and learn together.

One, int(x) and int

For example, if I want to represent an integer value, I can use int. As for the range of values ​​that int can represent, you can check the information. For example: store 123456, the data type is designed to be int(x) or int, the values ​​we query are all 123456, but if I increase auto_zerofill, the value of int(x) will be changed to display x-bit length empty after querying again Add 0 to the value (for example: int(7) to represent 123456, then 0123456 is displayed), and the int does not change. Therefore, the x in int(x) does not refer to the length of the display, but refers to the number of digits with zeros. Therefore, when we don't need to make up 0 before, just set it to int.

Two, varchar and char

Take varchar(10) and char(10) as examples. Varchar represents a variable-length character string, and char represents an immutable-length character string. When we store '123', the corresponding length in varchar(10) is 3, and the length corresponding to char(10) is 10 when retrieved from the database, and the corresponding length is 123 when retrieved from the database. Blank means 7 bits Space placeholder. Because varchar is variable, its storage efficiency is lower than that of fixed-length char. For occupying space, varchar is more space-saving.

Guess you like

Origin blog.csdn.net/qq_44092306/article/details/112911353