I'm new to web programming and doing different tutorials that I can find on the net.
I did my research and found out that in int(11), 11 is the maximum display width for integers and it's the default value if unless the integer is UNSIGNED (in this case it's 10).
When I see something like this:
id INT(11) not null AUTO_INCREMENT
I have no questions. But why do I see different things in different tutorials? For examlpe, in some, it says,
id INT(10) not null AUTO_INCREMENT
And even
id INT(4) not null AUTO_INCREMENT
What are the authors of those tuts trying to achieve? None of them seem to bother to give an explanation what 10 or 4 means.
Alright, they're obviously reducing the display width, but why? What's wrong with the default width of 11? Why would they want to change it? Or are there any other reasons I don't know of?
Thanks.
The x in INT(x) has nothing to do with space requirements or any other performance issues, it's really just the display width. Generally setting the display widths to a reasonable value is mostly useful with the UNSIGNED ZEROFILL option.
//INT(4) UNSIGNED ZEROFILL
0001
0002
...
0099
...
0999
...
9999
...
10000
//INT(2) UNSIGNED ZEROFILL
01
02
...
09
...
99
...
100
Without the UNSIGNED ZEROFILL option the value will be left-padded with spaces to the appropriate display width.
//INT(4)
1
2
...
99
...
999
...
9999
...
10000
//INT(2)
1
2
...
9
...
99
...
100
The number is just a representational option called "display width". It may be used by some applications to pad the field when displaying numeric datatypes.
The int size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.
This blog explains the meaning of int(size) in MySQL.
From the docs:
"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.)" That is, int(4) is politely asking that 123 be displayed as " 123". Don't know how many things actually pay attention or why they'd bother in a tutorial.
"The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly." So if you shove 123456 into an int(4) it will still be 123456.
Nothing to do with disk space or performance, just a hint to the application retrieving the data that it would like to be displayed with at least N digits.
The related ZEROFILL option can be used in conjunction to actually pad out the returned data. So 123 in an int(4) ZEROFILL is returned as 0123.
It's not performance increase neither difference in maximum allowed size.
It's only used when ZEROFILL is applied on that column. (See: http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/)
Yes it specifies the display width, and this comes into play only when ZEROFILL is specified. So at that time it can pad the field value with the required number of zeros.
I agree that the MySQL manual is a little vague on the length/display width of integers. You might think that this limits the value you can store in the column to the number of digits but the valid range of the colomn doesnt change, wether you set it to 1 or 11.
What it really does is determine the display width of the column's value. The only case this is usefull is if you use ZEROFILL to pad your values with zero's.
So basically there is no real difference between INT(1) and INT(11) in terms of what you can store in the column, the only case when it becomes relevant is when you want to ZEROFILL your values.
More info:
http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
Copying from this blog article:
MySQL has a little know feature for numerical types known as zerofill. This feature effects the display size of numerical types. Unlike the string types the number inside the parentheses is not the storage size in characters for the type. For numerical types the type name itself determines storage size.
The integer type it’s the padding size for zerofill.
Related
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
Can someone please explain the differences between Null, Zero and Blank in SQL to me?
Zero is a number value. It is a definite with precise mathematical properties. (You can do arithmetic on it ...)
NULL means the absence of any value. You can't do anything with it except test for it.
Blank is ill-defined. It means different things in different contexts to different people. For example:
AFAIK, there is no SQL or MySQL specific technical meaning for "blank". (Try searching the online MySQL manual ...)
For some people "blank" could mean a zero length string value: i.e. one with no characters in it ('').
For some people "blank" could mean a non-zero length string value consisting of only non-printing characters (SPACE, TAB, etc). Or maybe consisting of just a single SPACE character.
In some contexts (where character and string are different types), some people could use "blank" to mean a non-printing character value.
For some people could even use "blank" mean "anything that doesn't show up when you print or display it".
And then there are meanings that are specific to (for example) ORM mappings.
The point is that "blank" does not have a single well-defined meaning. At least not in (native) English IT terminology. It is probably best to avoid it ... if you want other IT professionals to understand what you mean. (And if someone else uses the term and it is not obvious from the context, ask them to say precisely what they mean!)
We cannot say anything generally meaningful about how ZERO / NULL / BLANK are represented, how much memory they occupy or anything like that. All we can say is that they are represented differently to each other .... and that the actual representation is implementation and context dependent.
You may correlate NULL-BLANK-ZERO case by child birth scenario( A real life Example.).
NULL Case: Child is not born yet.
BLANK Case: Child is born but we didn't give any name to him
ZERO Case: We defined it as zero, Child is born but of zero age.
See how this data will look in a database table:
Also NULL is a absence of value, where a field having NULL is not allocated any memory, where as empty fields have empty value with allocated space in memory.
Could you be more accurate about blank?
For what I understand of your question:
"Blank" is the lack of value. This is a human concept. In SQL, you need to fill the field with a value anyway. So that there is a value which means "no value has been set for this field". It is NULL.
If Blank is "", then it is a string, an empty one.
Zero: well, Zero is 0 ... It is a number.
To sum up:
NULL --> no value set
Blank ("") --> empty string
Zero --> Number equal to 0
Please, try to be more accurate next time you post an answer on Stack!
If I were you, I would check some resources about it, for example:
https://www.tutorialspoint.com/sql/sql-null-values.htm
NULL means it does not have any value not even garbage value.
ZERO is an integer value.
BLANK is simply a empty String value.
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.
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.
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.