How much mysql varchar type can be stored, all-round analysis

mysql data type (mainly talking about tinyint int varchar)

1 In the integer field tinyint (1 byte), smallint (2 bytes), mediumint (3 bytes), int(4) and bigint(8)
what does the 1 in tinyint(1) mean in int(11) What exactly is 11?
I first create a table to combat it

CREATE TABLE t1 (
a tinyint(1) not null DEFAULT 0 comment "integer field a"
) engine=InnoDB CHARSET=utf8mb4;

Let me talk about the premise: the maximum number of bytes stored in a table is 65535.
Think about it, can only store 0 and 1? It was not at the time, how can I prove it? Next, you will know
Insert picture description here

Now let's insert a record

Insert picture description here
What I defined is signed, which can store -128 to 127, indicating that the 1 in tinyint(1) means that the default length is 1, which occupies 1 byte. The size that can be stored has nothing to do with whether the length is 1, for example It is tiny(3), when the length is less than 3, 0 will be used as a supplement in the front, and then continue to look, I will create another table

Insert picture description here
At this point, I insert another piece of data to
Insert picture description here
see if I can see clearly. What does tinyint(3) mean? Be clear. 3 does not mean that the length can only be 3. For plastic shaping, how much it can store has nothing to do with the number in brackets. The relationship is only related to the bytes occupied by the current type. The range that tinyint can store with signs is -128 to 127, and if it is unsigned, it can store 0-255, understand?

2

Char and varchar (important) When
we create mysql tables in our business, varchar is used the most. The storage size of varchar is closely related to the character set. What does char(10) varchar(10) mean? Does it occupy 10 bytes or 10 characters? Before mysql4.0, it occupies 10 bytes. After 4.0, it is characters. Next, I create a table whose character set is uft8mb4.
Insert picture description here
Note: At this time, char(255) is It means that the number of characters that can be stored in the utf8mb4 character set is 255. Whether it is English or Chinese, it can store 255. If 255 Chinese characters are stored, we know that a Chinese (I mean Chinese) characters in utf8mb4 occupy 4 (my According to the biggest statement, in fact, Chinese accounted for 3, the same as utf8. Some emoticons can only be uft8b4, occupying 4 characters) bytes, then the bytes stored in the char type (the bytes I said are not characters) Ha, pay attention) The number is
255 times 4 = 1020 bytes. How many characters can the char type store at most? It’s a character, look at the picture

Insert picture description here
See that no char type can store up to 255? Can store 255 characters and the character set has nothing to do

Here comes the point, what does varchar(10) mean?
What he means is that it can store 10 characters, and an error will be reported if more than 10 characters. If you don’t believe it, create a table to see what
Insert picture description here
happens if you insert another one at this time
![Insert the picture description here](https://img-blog.csdnimg.cn/20201114192751932.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNz5G4dzFF_center=70FF_size,

I didn't lie to you, varchar can store up to 10 characters, so how many bytes are occupied at this time?
Someone said that it should be 10 times 4=40 and the answer is wrong! ! ! ! !
Since when varchar is defined in mysql , I have time to look at the default row format of mysql . I summarized it myself: if you define m varchar type fields in the table, use m/8 to round up, for example, define one It will occupy 1/8=2 bytes of space (at this time, it is said that the definition of default null is allowed to be empty). The not null is defined in the above figure. The null flag in the row format does not occupy space Because varchar is a variable-length field type, the storage length is not enough to 10 will be supplemented with spaces, because the variable type definition field varchar(10) the number in brackets and the number of bytes occupied by the character set are multiplied, if Less than 255 will use 1 byte to represent the length, greater than 255 will use 2 bytes to represent the length, so the space occupied by this example is 10 times 4+1=15 bytes, if the a field is defined as default null Allowed to be empty, then the size of the occupied space is 10 times 4+1/8+1=16 bytes, if the type of a defined at this time is varchar(70), then 70*4=280>255 this When you need to use 2 bytes to store the length, the space occupied at this time is 70 times 4 + 1/8 (default null) + 2=280+1 +2=283 bytes. If you don’t believe me, let’s create the simplest example

Insert picture description here
According to what we just said, we can calculate that a character can occupy one byte at this time. Since the maximum is 65535, should the calculation method be like this (the field not null is defined at this time, so the null flag does not occupy space), use 65535-2-n=0, at this time n is 65533, let’s see if this is the case

Insert picture description here
I add another character
at this timeInsert picture description here

If I define as default null at this time, see how much can be stored at most?
Insert picture description here
Let's calculate, because it is allowed to be empty, the number of bytes that need to be occupied should be the number of varchar defined/8 rounded up, that is, 1/8=1, so n can store up to
65535-1-2 at this time -n=0, at this time n=65532, see if this is the case. See if
Insert picture description here
it is true, right? I didn’t lie, just verify it with practice

If you know this, here is a question, what is the actual number of bytes stored?
Question: Suppose there is a VARCHAR(64) CHARSET utf8mb4 column, which stores the Chinese cn string.
Then guess how many bytes does MySQL use for storage?
A: 4 Bytes
B: 5 Bytes
C: 8 Bytes
D: 9 Bytes
E: 10 Bytes
F: 10.125 Bytes
G: 11 Bytes
H: 12 Bytes
I: 12.125 Bytes
K: 13 Bytes

Answer: Since 64 times 4=256>=256, 2 bytes are needed to store its size. Since only one varchar is defined and the field is allowed to be empty, then 1/8=1 byte is required to record the null flag Bit, under utf8mb4, the actual Chinese character is 3 bytes, and storing two Chinese characters takes up 3 times 2=6 bytes. The two characters cn take up 2 bytes in total, so the total size is occupied Is
6 + 2 + 2 + 1=11
or
6+2+2+1/8=10.125 so the answer is f and g

Understand this time

Let’s take another example, please think for yourself why this is the case, read more about what I said before, you will understand
Insert picture description here

The last one as thinking about
CREATE TABLE t11 (
id int,
a VARCHAR(100) DEFAULT NULL,
b VARCHAR(100) DEFAULT NULL,
c VARCHAR(100) DEFAULT NULL,
d VARCHAR(100) DEFAULT NULL,
e VARCHAR(100) DEFAULT NULL ,
f VARCHAR(100) DEFAULT NULL,
g VARCHAR(100) DEFAULT NULL,
h VARCHAR(100) DEFAULT NULL,
i VARCHAR(n) DEFAULT NULL
) CHARSET=utf8;
What is the maximum value of n at this time, pay attention to the utf8 character set
The answer first tells you is 21037, consider this for yourself

Summary: 1. Plastic fields such as 3 in tinyint(3) have nothing to do with how large the value can be stored. This is just the default length of 3, and the length is not to 3 before it is filled with 0.
2. The field of char(n) type is fixed Yes, even if the content does not reach the character length and supplement with spaces, the actual space occupied is related to the character set, utf8 occupies n times 3 bytes, utf8mb4 occupies n times 4 bytes
3. Define varchar(n) points Two types, allow to be empty, and not allow to be empty.
When the definition field is allowed to be empty, the occupied size is related to the character set, the null flag is related to the definition of several varchars, if it is utf8mb4 at this time In the case, n times 4<=255, then the total occupied size is (when defining the field) n times 4+1 + the number of varchar defined/8
if n times 4>255, the space occupied is n times 4 + 2 + The number of defined varchar/8
If the definition is not allowed to be empty at this time, and the character set is uft8mb4, and
the space occupied by n times 4<=255 is n times 4+1, if n times 4>255 When the occupied size is
n times 4+2

Guess you like

Origin blog.csdn.net/weixin_37509194/article/details/109694720