Datatypes limitations - mysql

Why do we have the limitation on datatypes like Char and varchar etc. but not on Integer ?
Why its designed in such a way ?
Eg:
We can define char(8), but we cannot define Int(8) or integer(8). It would have a max of 11 characters saved for it.

Integer have a fixed size of bytes 1-8, depending which kind you have defined.
char has a fixed size of bytes per character so you defined the size of 8 character you get 8 times the size if the character.
As it makes no sense to limit that fixed size if bytes, mysqkl finally kicked it out(in future) and gives now a warning, when you create a integer with a size

Related

What is the difference between varchar and text?

What is the difference between data types var char and text in database design?
The main difference is than TEXT has a fixed max size of 2¹⁶-1 = 65535 characters.
VARCHAR has a variable max size M up to M = 2¹⁶-1.
There are very few differences between VARCHAR and TEXT. Most are not really important.
Summary of *TEXT, CHAR, and VARCHAR:
Never use TINYTEXT.
Almost never use CHAR -- it is fixed length; each character is the max length of the CHARACTER SET (eg, 4 bytes/character for utf8mb4).
With CHAR, use CHARACTER SET ascii unless you know otherwise.
VARCHAR(n) will truncate at n characters; TEXT will truncate at some number of bytes. (But, do you want truncation?)
*TEXT may slow down complex SELECTs due to how temp tables are handled.
VARCHAR column can be given with any size, but it is limited by the maximum size of a single row of data (including all columns), which is 64KB (2¹⁶-1) .TEXT columns do not add to the maximum row size, because the actual text is not stored with the rest of the row.

Recommended way to store a string in this case?

I am storing strings and 99.5+% are less than 255 characters, so I store them in a VARCHAR(255).
The thing is, some of them can be 4kb or so. What's the best way to store those?
Option #1: store them in another table with a pointer to the main.
Option #1.0: add an INT column with DEFAULT NULL and the pointer will be stored there
Option #1.1: the pointer will be stored in the VARCHAR(255) column, e.g 'AAAAAAAAAAA[NUMBER]AAAAAAAAAAAA'
Option #2: increase the size of VARCHAR from 255 to 32767
What's the best of the above, Option #1.0, Option #1.1 or Option #2, performance wise?
Increase the size of your field to fit the max size of your string. A VARCHAR will not use the space unless needed.
VARCHAR values are stored as a 1-byte or 2-byte length prefix plus
data. The length prefix indicates the number of bytes in the value. A
column uses one length byte if values require no more than 255 bytes,
two length bytes if values may require more than 255 bytes.
http://dev.mysql.com/doc/refman/5.0/en/char.html
The MySQL Definition says that VARCHAR(N) will take up to L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes where L is the length in bytes of the stored string.
So I guess that option #2 is quite okay, because the small strings will still take less space than 32767 bytes.
EDIT:
Also imagine the countless problems options 1.0 and 1.1 would raise when you actually want to query a string without knowing whether it exceeds the length or not.
Option #2 is clearly best. It just adds 1 byte to the size of each value, and doesn't require any complicated joins to merge in the fields from the second table.

mysql: issue with the Maximum Value for int type

I created two fields text(varchar), number(int) using mysql. But I am not sure what size i should put for varchar(), int() , for varchar(), I checked here: http://dev.mysql.com/doc/refman/5.0/en/char.html, and know if I put varchar(4), it means: can hold up to 4 characters. But for int(4), I checked here: http://dev.mysql.com/doc/refman/5.0/en/integer-types.html, It is said: Maximum Value:(Signed/Unsigned)(2147483647/4294967295)
Question:
for int(4), how did it get this value2147483647/4294967295? if i put int(8), what would the value be?
See the MySQL Numeric Type Documentation. These things are well-documented.
The range for a signed INT is [-2147483648, 2147483647].
Note that in the case of INT(x), x is the "display width" and has nothing to do with the range or space requirements:
MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits ... display width does not constrain [or expand] the range of values that can be stored in the column.
Mysql int(11) number out of range
int(4) an INT with a display width of four digits.
int(8) an INT with a display width of eight digits.
Minimum Value/Maximum Value are the same for an int type which use 4 bytes to storage.
-2147483648 to 2147483647 for signed, and 0 to 4294967296 for unsigned.
Well, from the browser console I note:
Math.pow(2, 4*8) is 4294967296 and half that is 2147483648
those are 1 off the maximum (unsigned/signed) integer that can be stored in binary form using 4 8-bit bytes.

What does INT(5) in mysql mean?

I always thought INT(5) means a number which has a max size of 5 digits. I tried entering a huge number inside it and it somehow got cut down to 2147483647
This obviously isnt 5 digits. So what does INT(5) have a limitation of ?
From MySQL Docs, Numeric Type Attributes
MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)
INT is always four bytes wide. The 5 is the "display width".
In MySQL, INT(5) does not mean that values are limited to 5-character values. It only means that MySQL will try to pad these values with spaces/zeroes when returning them.
The numeric range of any signed INT including INT(10), INT(5) or any other INT(n) is:
-2,147,483,648 ... 2,147,483,647, which is 10 digits at most.
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
"M indicates the maximum display width for integer types. The maximum legal display width is 255. Display width is unrelated to the range of values a type can contain, as described in Section 10.2, “Numeric Types”"
https://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/
M indicates the maximum display width for integer types. For floating-point and fixed-point types, M is the total number of digits that can be stored. For string types, M is the maximum length. The maximum allowable value of M depends on the data type.
An int can be between -2147483648 and 2147483647 signed, or 0 and 4294967295 unsigned.
Thats why it was cut down to that if you entered a number larger than it as a signed value
A very common misconception about what int(N) means in MySQL is that the column can store maximum integer value with N digits in length. However, this is not true. int(N) does not determines the maximum value that the column can store in it. N is the display width of the integer column, unlike the characters columns where the number means number of character that can be stored.
The number in the parenthesis does not determines the max and min values that can be stored in the integer field. The max and min values that can be stored are always fixed. The following table shows the required storage and range for each integer type.

MySql - size VARCHAR

Many people say to me that set VARCHAR(100) doesnt make sense. It the same as put 255. I'd like to know why...
That's rubbish. They may be talking about the fact that a varchar uses one byte for the length regardless of whether the maximum length is 100 or 255 ( lengths above that will use two bytes, up to ~64K) but they are treated differently.
If you insert a 150-character string into the former, it will be truncated to 100, that's not so for the latter case.
You should use the length that makes sense. If you have a column that will never exceed 30 characters, don't use varchar(255).
See here for the type details.
http://msdn.microsoft.com/en-us/library/aa258242%28v=sql.80%29.aspx.
VARCHAR(100) does makes sense, it says that the max size of the input is 100 chars(if you will insert a longer string it will cut it to a size 100).
VARCHAR(256) it says that the max size of the input is 256 chars.