Numerical data has fixed space always, when you choose the type you are determining how much space you will occupy in that table row, it does not change. And the table presented is showing this. It’s the same in any language.
So if you said you want one BIGINT
it will take 8 bytes, but it can store something that fits in a smaller type. It’s like on a sheet of paper, the space to put multiple digits is there, if you don’t use all the space is your problem. If you do not always use, you probably have chosen the wrong type. If you have a number that does not fit in the space also you have chosen wrong, it will be a mistake to try to put there.
VARCHAR
is another thing, there is an inherently variable datum. What we are talking about here goes further on the line of CHAR
which has the size defined in its statement of the table structure. Just remembering that the CHAR
you define how many characters will be in that column, and then it doesn’t change, if you say it is 10, it will always be 10, even if you don’t use all of them. And so maybe people think that the SMALLINT(3)
has to do with characters, and it’s something else entirely, the numbers have the allocated space set independent of that number in parentheses.
Not so in all databases, Sqlite for example only has the INT
, and the occupied size varies according to the accuracy that the number requires. Occupies 1 byte (if not null) up to 9 bytes (each extra byte adds a 128 multiplication since 1 bit is used to tell if it has a new byte to evaluate, other than the ninth byte which, if it exists, can multiply the 256 possible of a byte, which would have 8 useful bytes and one which is the sum of the control bits.
There is reserve of all bytes, I still do not understand why it can give the opposite perception if the table presented makes this clear. And I still don’t understand if some of the other questions make sense.
But nothing would prevent it from being different, as it is in some databases. In general the lines may have different sizes because of the VARCHAR
or some kind of BLOB
, if the number is also like this, nothing changes, in many cases it can be advantageous. Think of a line as if it were always a VARCHAR
which has several segments (a BLOB
would be even better), and in fact internally it is usually treated like this, there is a key, usually the ID
and a value (key-value) that would be this BLOB
. Already in an index the pair what is called key is the key that everyone knows and the value is the ID
or other table PK.
Ele não "separa" todos os bytes para aquele registro igual ao VARCHAR?
I don’t understand what that means. And probably the rest became meaningless so.– Maniero
Probably @RBZ wants to know if bigint only considers the most significant bytes, discarding the leftmost bytes that are zeroed
– Lisângelo Berti
@Maniero would be if he "reserves" the space for when to use all necessary space. Because if it does so, even if the value is equal to the 2 (240) o
BIGINT
will take up more space.– rbz
I changed it to get better.
– rbz
"related": link, Link2
– rbz