What are numbers for when creating columns in databases?

Asked

Viewed 140 times

7

Everyone who has ever worked with databases has noticed that, during the data modeling of a table, we can define a number between parentheses.

Examples:

INT(20)
TINYINT(4)
BIGINT(10)
VARCHAR(8)

If each type of data has, inherent to itself the amount of bytes that one can store, why are these numbers needed then? In addition, the number of paper varies according to the type of data (for example, VARCHAR and INT)?

2 answers

7


It may vary according to 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 certainly does not determine is the amount of bytes it will occupy on the line. Texts can have encodings that a character has no direct relation to the number of bytes occupied. 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 data display control than for storage, contrary to popular belief. The size it occupies is implementation detail.

Then a 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 them to be stored more than this, some will occupy 8 bytes because this type has no encoding multibyte, but it depends on the implementation. You have to look closely at the documentation of each system.

  • 3

    Caution: in the case of Mysql, the value between parentheses in numeric types determines only one length exhibition of the values. Cf. http://everythingmysql.ning.com/profiles/blogs/data-type-confusion-what-is-an

  • 1

    @bfavaretto I remember only the Mysql that does this, if you or someone else knows of another, please put here or reply with this detail.

6

In the case of fields of the numeric type int, tinyint, bigint, the number in parentheses means the maximum number of digits. For varchar and char fields, the number indicates the maximum number of characters.

  • Note that this limitation can be used as field validation, as type and number of digits of a field CPF for example: INT(11) only number and only CPF, INT(14) numbers, for CPF or CNPJ, would thus force the standardization of development.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.