Question:
Anyone who has worked with databases has noticed that when modeling data from a table, we can define a number in parentheses.
Examples:
INT(20)
TINYINT(4)
BIGINT(10)
VARCHAR(8)
If each type of data has, inherent to it, the amount of bytes that can be stored, why are these numbers necessary then? Also, does the number vary by role depending on the data type (eg VARCHAR
and INT
)?
Answer:
It may vary depending on the implementation of each database, but in general it determines the maximum number of characters that can be displayed in the column or the maximum that can be used in the column.
What it certainly doesn't determine is the amount of bytes it will occupy on the line. Texts can have encodings in which a character has no direct relationship with the number of occupied bytes. The storage of numbers will be done according to the implementation and each one can have a different consumption.
This number that appears is more for controlling the display of the data than for storing it, contrary to popular belief. The size it occupies is implementation detail.
So an INT(20)
, if this is possible, indicates that the number can have up to 20 digits in its textual representation.
A VARCHAR(8)
indicates that the text can be up to 8 characters long. Some banks allow more than that to be stored, some will occupy 8 bytes because this type does not have multibyte encoding, but it depends on the implementation. You have to look closely at the documentation for each system.