what does 255 limitation for integer in Mysql mean? - mysql

As the question told, what if i want to have 256 numbers. Is this mean 256 as a number or byte? Because, i will definitely need more than 255

The 255 limit applys to a field with a type of byte, called a TinyInt in MySql. As the maximum value that can be represented in a single byte is 255.
An integer by default in MySql and most DBMS will be much larger than a single byte, in MySql it is 32bits, or 4 bytes long. This means it can store values from 0 to 4billion, or from -2billion to +2billion.
The official MySql reference for integer sizes is http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html
"255" is a limitation of unsigned tinyint.

What 255 limit are you referring to?
This page on MySQL numeric datatypes clearly details the ranges available -- SMALLINT can hold between -32768 and 32767, INTEGER can hold a 32-bit signed integer value, etc.

The argument to any INT/BIGINT/MEDIUMINT/SMALLINT/TINYINT has nothing to do with the limit on its size or its range of values. It's just a hint for display width.
This is more useful if you use the ZEROFILL option for integer types. So it pads the value with zeroes. For example, storing 1234 into an INT(10) ZEROFILL column and fetching it back returns "0000001234". This makes some reporting look nicer. But the value stored in the database is just 1234.
The display width number doesn't make a TINYINT store any more or any less than 8 bits, no matter how large you make the width. Likewise SMALLINT is always 16 bits, MEDIUMINT is always 24 bits, INT is always 32 bits, and BIGINT is always 64 bits.
Nor does the display width constrain any of those types to store less than the full range of values permitted by their data type size. E.g. TINYINT(1) still allows all values -128 to 127.
The number arguments of NUMERIC or DECIMAL have a totally different meaning. They do determine the precision and scale of the data type, according to standard SQL.

Related

What is the difference between an INT of length 20 and BIGINT of length 20 in MySQL

I understand int stores about 4 bytes and bigint stores about 8 bytes in MySQL
Since one can specify the length of each field in MySQL, Will an int(20) takeup the same space as a bigint(20)?
This is a bit long for a comment.
The number of bytes occupied by a value is determined by the type. You can see the list of numeric types and their sizes here.
In addition, MySQL supports "attributes" on numeric types. The (20) is a display width on output. The attributes are explained here.
So, for your example, the storage occupied by the fields is different because the types being used are different.

MySQL: VARCHAR(1024) vs VARCHAR(512)

In MySQL what is the difference between VARCHAR(1024) and VARCHAR(512)? If my item will never be more than 512 characters, what do I lose by using VARCHAR(1024)?
Don't know where you got that from, but it's not possible to create a table with varchar without specifying the length. It results in a syntax error. So your question is obsolete.
UPDATE:
Nothing. Varchar is as the name implies a datatype of variable length, at least to the maximum length you specified when creating the table. This means, that in a varchar column for each row one additional byte is used to store how long the string in the row actually is.
So the difference between varchar(1024) and varchar(512) is, that your data gets truncated when you try to insert more than 1024 or 512 bytes. Note: bytes, not characters. How much bytes each character uses is dependent on the character set you're using.
There is a actually a difference. And it can have a big performance impact if you manipulate big data. If a temporary table is used, the records on disk will take the full length indicated instead of the variable length. A high value will slow down the request even more in that case. Temporary tables can occur for various reasons (such as memory full, or some combinations of group by /order by).
VARCHAR(1024) 1024 this is lenght.
The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.
According to mySQL documentation
In contrast to CHAR, 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.
A deeper analysis of the performance impact of larger VARCHARs can be found here.

what can I save with int,tinyint,mediumint and so on

I all..
I have always used int(10) for everything, but couple days ago, I started a new project, and was hoping to do this 100% optimized ;)
So I am wondering, how many;
user_id => int(6) vs. mediumint (8) or similar will be possible to create/add
group_id => tinyint(1) vs tinyint (4) or similar will it be possible to create/add
and so on..
I know that the (X) is the width of the field, but, I can not quite understand the actual number of users/posts/messages ++ that can be created using example; mediumint(8) for id, instead of int(10).
Thanks for any reply on this!!
-Tom
Database IDs are usually always positive (0->∞) so the max value would be:
Integer Type Max Value
TINYINT 255
SMALLINT 65535
MEDIUMINT 16777215
INT 4294967295
BIGINT 18446744073709551615
I know that the (X) is the width of the field
The optional number in parens is the display width. It has nothing to do with how many unique values are in the range of the integer or how much storage space the integer needs. Application code is free to ignore your hint about the display width. "Display width" is a non-standard extension to SQL.
INTEGER(6) and INTEGER(2) both take 4 bytes to store, and both accept values ranging from -2147483648 to 2147483647.
All medium integers take 3 bytes to store, and accept values ranging from -8388608 to 8388607.
Assuming that a medium int is big enough (~ 16 million unique values) to identify your full domain of values you potentially save 1 byte per row over a 4-byte integer. (Potentially, because some platforms require padding to the next word boundary. For 32-bit systems, that would be 4 bytes, so no actual space savings. I don't know whether MySQL does that.) For 10 million rows, you might save 10 megabytes (plus some space savings in the index)--not very much these days. Narrower tables are generally faster than wider tables, but I don't think you'll notice the difference here.

Memory usage of storing strings as varchar in MySQL

I've begun to get very interested in the memory usage of MySQL. So I'm looking at this here:
http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
I get very excited about the prospect of saving memory by (for example) needing only a signed smallint where I was using an unsigned int in many places. Then I read about varchars...
"VARCHAR(M) - Length + 1 bytes if column values require 0 – 255 bytes"
What?! Now it appears to me as though storing a single varchar would use up so much memory, that I may as well not even get excited with my int vs. smallint because it's vastly overshadowed by the varchar field. So I come here asking if this is true, because it simply can't be? Are varchars really that terrible? Or should I really not be getting excited at all for my smallint discovery?
edit: Sorry! I should've been more clear. So, let's say I store a varchar with 7 characters, meaning 8 bytes. That means, then, that it uses the same as a number stored in a BIGINT column? That's what I'm concerned about.
What this is saying is that for a given string length, the amount of storage used is equal to the length of the string in bytes, plus one byte to tell MySQL how long the string is.
So for instance, the word "automobile" is 10 bytes (1 for each character), so if it is stored in a varchar column it will take up 11 bytes. 1 for the number 10 , and 1 each for each of the characters in the string.
From the link you posted:
http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
The storage requirements depend on these factors:
-The actual length of the column value
-The column's maximum possible length
-The character set used for the column, because some character sets contain multi-byte characters
For example, a VARCHAR(255) column can hold a string with a maximum length of 255 characters. Assuming that the column uses the latin1 character set (one byte per character), the actual storage required is the length of the string (L), plus one byte to record the length of the string. For the string 'abcd', L is 4 and the storage requirement is five bytes. If the same column is instead declared to use the ucs2 double-byte character set, the storage requirement is 10 bytes: The length of 'abcd' is eight bytes and the column requires two bytes to store lengths because the maximum length is greater than 255 (up to 510 bytes).
While I am no MySQL DBA, it appears there is a very simple answer to this question, and no need to go deeper into storage sizes - because it is NOT configureable.
Per MySQL memory storage documentation,
MEMORY tables use a fixed-length row-storage format. Variable-length types such as VARCHAR are stored using a fixed length.
Thus, you won't have any specific gains by using VARCHAR for a table using the MEMORY storage engine, no matter how VARCHAR is stored on other storage engines such as MyISAM or InnoDB.

What does `unsigned` in MySQL mean and when to use it?

What does "unsigned" mean in MySQL and when should I use it?
MySQL says:
All integer types can have an optional
(nonstandard) attribute UNSIGNED.
Unsigned type can be used to permit
only nonnegative numbers in a column
or when you need a larger upper
numeric range for the column. For
example, if an INT column is UNSIGNED,
the size of the column's range is the
same but its endpoints shift from
-2147483648 and 2147483647 up to 0 and 4294967295.
When do I use it ?
Ask yourself this question: Will this field ever contain a negative value?
If the answer is no, then you want an UNSIGNED data type.
A common mistake is to use a primary key that is an auto-increment INT starting at zero, yet the type is SIGNED, in that case you’ll never touch any of the negative numbers and you are reducing the range of possible id's to half.