What is the purpose of the column length in TINYINT(M)? [duplicate] - mysql

This question already has answers here:
Types in MySQL: BigInt(20) vs Int(20)
(6 answers)
Closed 9 years ago.
I studied the MySQL documentation and am now unsure what the column length specification (M) in TINYINT(M) means. A TINYINT UNSIGNED has a value range of 0 to 255, but TINYINT(1) UNSIGNED has a range of 0 to 9 ?
Is it better for compression?

From MySQL documentation about numeric type attributes:
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. For example, a column
specified as SMALLINT(3) has the usual SMALLINT range of -32768 to
32767, and values outside the range permitted by three digits are
displayed in full using more than three digits.
When used in conjunction with the optional (nonstandard) attribute
ZEROFILL, the default padding of spaces is replaced with zeros. For
example, for a column declared as INT(4) ZEROFILL, a value of 5 is
retrieved as 0005.
That said, you should consider this attribute only if you care about the 0s you could show on the left or if you are in a CLI environment.
Furthermore, if you declare a field as tinyint(2) and the number stored in is 113, all 3 characters will shown (despite what previous answer said)

Related

Why can't I change my INT from unsigned to signed? Is signed Zerofill possible?

I have a field called NUMBER in a table called TEST
NUMBER is int(8) Unsigned Zerofill Not Null
I need negative numbers in this field now and I'd still like them zerofilled
For example -00023419
I can change it to signed, but can't seem to get zerofilled.
if I do: alter table test modify number int(8) signed zerofill not null -
It stays unsigned zerofill
if I do: alter table test modify number int(8) zerofill not null -
It stays unsigned zerofill
if I do: alter table test modify number int(8) signed not null -
I get signed but no zerofill
When I had it set to signed, I put an a negative number then tried to change to zerofill and the number changed to 00000000 and everything was set to unsigned again. It it impossible to have signed zerofilled numbers?
https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html says:
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
Reported as a bug in 2006, closed as "Not a Bug".
https://bugs.mysql.com/bug.php?id=24356
Re your comment:
So does this mean no one out there has negative zero filled data in a mysql database?
Right. The most common use case for ZEROFILL is to make sure things like zip codes or bank account numbers use a fixed number of digits. These cases don't support negative values anyway, so why bother with ugly strings like -00001234?
can you convert it to zerofilled with PHP?
Yes, you can format numbers with zero-padding in PHP.
<?php
$n = -1234;
printf("Number is %08d\n", $n);
Output:
Number is -0001234
Read http://php.net/manual/en/function.sprintf.php for more neat formatting things you can do with printf()/sprintf().
ZEROFILL implies UNSIGNED.
MySQL Reference Manual https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
With all the potential problems, I avoid using the non-standard MySQL ZEROFILL attribute.
Well, if your column is a foreign key or a primary key, MySQL doesn't allow this modification to happen. You have to do this beforehand.

What will be the maximum limit for Int(8) Unsigned in MySQL? [duplicate]

This question already has answers here:
int(11) vs. int(anything else)
(7 answers)
Closed 6 years ago.
What is the maximum value that Int(8) can hold? Is there a difference between Int(8) and Int(11)?
Signed ranges between -2147483648 and 2147483647
Unsigned ranges between 0 and 4294967295
In MySQL, int numbers between brackets define the display width. They will, however, not cut off the number if the int is larger than the display width. You will mostly see the effect of int numbers when using zerofill.
More info: https://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/

When should I use UNSIGNED attribute for a column exactly?

Based on documentation, UNSIGNED columns doesn't accept negative numbers. So is that mean should I use UNSIGNED attribute for each column which always is containing positive or null values?
Suppose this table structure:
// vote
+----+---------+---------+-------+------------+----------+
| id | id_post | id_user | value | code_table | time |
+----+---------+---------+-------+------------+----------+
In table above, all columns are null or positive except value column. So should I set UNSIGNED attribute for all columns except value column? Am I right? Or using UNSIGNED attribute is based on another parameter?
If you are sure that all the columns except the value column is going to contain positive values then yes you should set it as UNSIGNED. Using UNSIGNED attribute is based only on this parameter as what value you expect in your column. Also to note that UNSIGNED ranges from 0 to n wheras SIGNED ranges from about -n/2 to n/2.
On the side of memory both signed and unsigned types take the same memory space (4 bytes for INT).
Use UNSIGNED attribute based on the values you will store in the column.
The IDs, for example, are usually unsigned values (this is just a convention, nothing prevents you have negative ID values).
On the other hand, an age column will always contain positive values and the UNSIGNED value fits well for it.

What does the number in mediumint(10) denote? [duplicate]

This question already has answers here:
What is the size of column of int(11) in mysql in bytes?
(11 answers)
Closed 8 years ago.
I'm creating a table in MySql and I came across columns with datatype as mediumint followed by a number in parenthesis. What does this number denote?
https://dev.mysql.com/doc/refman/5.7/en/integer-types.html says that unsigned mediumint can take a max value of 16777215, so how does it differ if have columns with different sizes mediumint(6) or mediumint(10) ?
I suppose here in an answer:
http://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html
so difference is only in formatting output, nothing else...

mysql tinyint(1) vs tinyint(2) vs tinyint(3) vs tinyint(4) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySql: Tinyint (2) vs tinyint(1) - Which difference?
What is the difference between:
TinyINT(1)
TinyINT(2)
TinyINT(3)
TinyINT(4)
TinyINT(M) always has a range from -128..+127 signed or 0..255 unsigned. M is the display width.
M indicates the maximum display width for integer types. The maximum
display width is 255. Display width is unrelated to the range of
values a type can contain, as described in Section 11.2, “Numeric
Types”. For floating-point and fixed-point types, M is the total
number of digits that can be stored.
from http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html
According to Mysql manual all decimal numeric types supports syntax:
Integer Types (Exact Value)
When using DECIMAL it allows you to specify precision.
With *INT types it's has mainly display function which also specifies how many places should be added when using ZEROFILL.
The byte size remains unaffected (1B for TINYINT).
TinyINT = -128...+127
(n) is for display purposes.