Why is it necessary to indicate the length of a column in a table

I am always wondering why we should limit the column length in the database table to a certain limit than the default value. For instance. I have a short_name column in my People table, the default length is 255 characters for the column, but I limit it to 100 characters. Who cares.

+4
source share
2 answers

The string will be truncated to the maximum length (usually in characters).

0
source

The way it is actually implemented depends on the database engine you are using.

For instance:

  • CHAR (30) will always use up to 30 characters in MySQL, and this allows MySQL to speed up access since it is able to predict the cost length indiscriminately;
  • VARCHAR (30) truncates any long lines to 30 characters in MySQL when strict mode is enabled, otherwise you can use longer lines and they will be completely saved;
  • In SQLite, you can store rows in any type of column, ignoring the type.

The reason that many SQL functions are supported in these database engines, although they are not used or are used in different ways, is to maintain consistency with the SQL schema.

0
source

Source: https://habr.com/ru/post/1345325/


All Articles