mysql insert/update converts a number - mysql

I have two mysql columns both int unsigned zerofill. The first 5 in length, second 11 in length. First value takes any 5 digit number no problem. The second, no matter what converts any 11 digit number into 04294967295. Any clue on what I can do to solve this puzzle?

Your number is larger than the integer field can handle - 232 - 1.
Change the column to an unsigned BIGINT and you'll be good up to 18,446,744,073,709,551,615.

The length you give to an integer field is just for displaying. If you pass a length 11, a select commando on that column will display a number of 11 digits (which is the length of the number you got). The actual size of the field is determined by the type you chose for the column (int). int-fields are capable of storing 232 different values. For an unsigned field having 0 as its first value, this results in a maximum value of 232 - 1, which matches the number in your output. Since any 11 digit number you input will be bigger than this number, the value saved will be 232 - 1.
You can overcome this limitation by using the BIGINT-type, which allows you to store any number up to 264.

Range of UNSIGNED INT is 0 to 4294967295. so Any value above 4294967295, it will insert that max value, as that is the maximum value possible for unsigned INT.

Related

MySQL ERROR 1264 (22003): Out of range value for column

I get this error:
ERROR 1264 (22003): Out of range value for column 'median_comments' at
row 1
after running this query:
update influencers set `median_comments` = 1347 WHERE `id` = 1;
I'm not sure why this fails on this number which doesn't have any decimals and which is only 4 digits.
The field type is:
median_comments decimal(10,8)
You are using DECIMAL(10,8) that means max digits before decimal would be (10 - 8) = 2.
Reference: DECIMAL Data Type Characteristics
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
To fix the error, change your datatype to DECIMAL(10,2).
ALTER TABLE `influencers`
CHANGE COLUMN `median_comments` `median_comments` DECIMAL(10,2) NOT NULL DEFAULT 0;
If you are using decimal(10,8) as data type it means you are specifying 8 digits after decimal place which leaves only (10 - 8 i.e 2 digits) for your whole number.
In this case since your number 1347 contains 4 digits (whole number) hence you are getting the error as "Out of range value" since you are allowed only 2.
You should consider changing it to at least decimal (12,8) which will leave you 4 digits for your whole number part and your above command should work.
Please refer to post - Number format issue in Oracle. Same issue.
As you like me came here from google and your issue is related to Doctrine, and your column type is type="decimal", then you should configure precision and scale properties of your column in the right way.
For me, it was like before:
/** #ORM\Column(name="revenue", type="decimal", scale=4, nullable=true) */
private $revenue;
after
/** #ORM\Column(name="revenue", type="decimal", precision=14, scale=4, nullable=true) */
private $revenue;
It will be converted to DECIMAL(14,4), which means fourteen digits total, four of which are to the right of the decimal point.
Don't forget to prepare migration and run it to apply the changes.
Finally, you should get SQL like this:
ALTER TABLE project_finance CHANGE revenue revenue NUMERIC(14, 4) DEFAULT NULL

mySQL column without a one-size-fits-all precision for DECIMAL

When I define a table to store decimal values I use a statement like this:
CREATE TABLE myTable (
myKey INT NOT NULL,
myValue DECIMAL(10,2) NOT NULL,
PRIMARY KEY (myKey)
);
However, this results in every myValue being stored with a one-size-fits-all precision of (10,2). For instance
45.6 becomes 45.60
21 becomes 21.00
17.008 becomes 17.01
But what if each record has a myValue of different precision? I need 45.6 to remain 45.6, 21 to remain 21, and 17.008 to remain 17.008. Otherwise the precision of measurement is being lost. There's a big difference between 21 and 21.00.
If you don't need to do greater/less-than compares, store as a VARCHAR(..)
The strings '21' and '21.00' would have identical values, but present different "precision".
When needing the numeric value, add zero (col + 0).
This does not allow for "negative precision", such as "1.2M" being represented as 1200000. If you need that, then Norbert's approach is probably better.
You can store with high precision and exact recall by following a different way of storing the data:
Create a table with two columns:
CREATE TABLE precise (value BIGINT, decimaldot INT);
Use code to determine where the dot is, for example in your 21 value: 2 (assuming 1 indexing). So stored the value would be:
INSERT INTO precise values (21,2);
Retrieved it would return 21 exact (parsing back the dot in the value 21 at position 2, is 21)
Value 17.008 would also have decimaldot at 2:
INSERT INTO precise values (17008,2);
Etc..
Larger values can be stored by using a VARCHAR(4000) instead of a biginteger, or by using blob fields.

MySql Data size [duplicate]

What is the size of column of int(11) in mysql in bytes?
And Maximum value that can be stored in this columns?
An INT will always be 4 bytes no matter what length is specified.
TINYINT = 1 byte (8 bit)
SMALLINT = 2 bytes (16 bit)
MEDIUMINT = 3 bytes (24 bit)
INT = 4 bytes (32 bit)
BIGINT = 8 bytes (64 bit).
The length just specifies how many characters to pad when selecting data with the mysql command line client. 12345 stored as int(3) will still show as 12345, but if it was stored as int(10) it would still display as 12345, but you would have the option to pad the first five digits. For example, if you added ZEROFILL it would display as 0000012345.
... and the maximum value will be 2147483647 (Signed) or 4294967295 (Unsigned)
INT(x) will make difference only in term of display, that is to show the number in x digits, and not restricted to 11. You pair it using ZEROFILL, which will prepend the zeros until it matches your length.
So, for any number of x in INT(x)
if the stored value has less digits than x, ZEROFILL will prepend zeros.
INT(5) ZEROFILL with the stored value of 32 will show 00032
INT(5) with the stored value of 32 will show 32
INT with the stored value of 32 will show 32
if the stored value has more digits than x, it will be shown as it is.
INT(3) ZEROFILL with the stored value of 250000 will show 250000
INT(3) with the stored value of 250000 will show 250000
INT with the stored value of 250000 will show 250000
The actual value stored in database is not affected, the size is still the same, and any calculation will behave normally.
This also applies to BIGINT, MEDIUMINT, SMALLINT, and TINYINT.
According to here, int(11) will take 4 bytes of space that is 32 bits of space with 2^(31) = 2147483648 max value and -2147483648min value. One bit is for sign.
As others have said, the minumum/maximum values the column can store and how much storage it takes in bytes is only defined by the type, not the length.
A lot of these answers are saying that the (11) part only affects the display width which isn't exactly true, but mostly.
A definition of int(2) with no zerofill specified will:
still accept a value of 100
still display a value of 100 when output (not 0 or 00)
the display width will be the width of the largest value being output from the select query.
The only thing the (2) will do is if zerofill is also specified:
a value of 1 will be shown 01.
When displaying values, the column will always have a width of the maximum possible value the column could take which is 10 digits for an integer, instead of the miniumum width required to display the largest value that column needs to show for in that specific select query, which could be much smaller.
The column can still take, and show a value exceeding the length, but these values will not be prefixed with 0s.
The best way to see all the nuances is to run:
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`int1` int(10) NOT NULL,
`int2` int(3) NOT NULL,
`zf1` int(10) ZEROFILL NOT NULL,
`zf2` int(3) ZEROFILL NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `mytable`
(`int1`, `int2`, `zf1`, `zf2`)
VALUES
(10000, 10000, 10000, 10000),
(100, 100, 100, 100);
select * from mytable;
which will output:
+----+-------+-------+------------+-------+
| id | int1 | int2 | zf1 | zf2 |
+----+-------+-------+------------+-------+
| 1 | 10000 | 10000 | 0000010000 | 10000 |
| 2 | 100 | 100 | 0000000100 | 100 |
+----+-------+-------+------------+-------+
This answer is tested against MySQL 5.7.12 for Linux and may or may not vary for other implementations.
What is the size of column of int(11) in mysql in bytes?
(11) - this attribute of int data type has nothing to do with size of column. It is just the display width of the integer data type. From 11.1.4.5. 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.
A good explanation for this can be found here
To summarize : The number N in int(N) is often confused by the maximum size allowed for the column, as it does in the case of varchar(N). But this is not the case with Integer data types- the number N in the parentheses is not the maximum size for the column, but simply a parameter to tell MySQL what width to display the column at when the table's data is being viewed via the MySQL console (when you're using the ZEROFILL attribute).
The number in brackets will tell MySQL how many zeros to pad incoming integers with. For example: If you're using ZEROFILL on a column that is set to INT(5) and the number 78 is inserted, MySQL will pad that value with zeros until the number satisfies the number in brackets. i.e. 78 will become 00078 and 127 will become 00127. To sum it up: The number in brackets is used for display purposes.
In a way, the number in brackets is kind of usless unless you're using the ZEROFILL attribute.
So the size for the int would remain same i.e., -2147483648 to 2147483648 for signed and 0 to 4294967295 for unsigned (~ 2.15 billions and 4.2 billions, which is one of the reasons why developers remain unaware of the story behind the Number N in parentheses, as it hardly affects the database unless it contains over 2 billions of rows), and in terms of bytes it would be 4 bytes.
For more information on Integer Types size/range, refer to MySQL Manual
In MySQL integer int(11) has size is 4 bytes which equals 32 bit.
Signed value is : -2^(32-1) to 0 to 2^(32-1)-1
= -2147483648 to 0 to 2147483647
Unsigned values is : 0 to 2^32-1
= 0 to 4294967295
Though this answer is unlikely to be seen, I think the following clarification is worth making:
the (n) behind an integer data type in MySQL is specifying the display width
the display width does NOT limit the length of the number returned from a query
the display width DOES limit the number of zeroes filled for a zero filled column so the total number matches the display width (so long as the actual number does not exceed the display width, in which case the number is shown as is)
the display width is also meant as a useful tool for developers to know what length the value should be padded to
A BIT OF DETAIL
the display width is, apparently, intended to provide some metadata about how many zeros to display in a zero filled number.
It does NOT actually limit the length of a number returned from a query if that number goes above the display width specified.
To know what length/width is actually allowed for an integer data type in MySQL see the list & link: (types: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT);
So having said the above, you can expect the display width to have no affect on the results from a standard query, unless the columns are specified as ZEROFILL columns
OR
in the case the data is being pulled into an application & that application is collecting the display width to use for some other sort of padding.
Primary Reference: https://blogs.oracle.com/jsmyth/entry/what_does_the_11_mean
according to this book:
MySQL lets you specify a “width” for integer types, such as INT(11).
This is meaningless for most applications: it does not restrict the
legal range of values, but simply specifies the number of characters
MySQL’s interactive tools will reserve for display purposes. For
storage and computational purposes, INT(1) is identical to INT(20).
I think max value of int(11) is 4294967295
4294967295 is the answer, because int(11) shows maximum of 11 digits IMO

MySQL Hexadecimal Binary Limit

I have a Table with 2 columns: 'Id' (datatype=INT) , 'Representation' (datatype=binary).
I want to store a hexadecimal value in the form of binary digits in the 'Representation' Column.
What is the Max number of Binary digits that i can store in the 'Representation' column ?
MySql
BINARY
The MySql docs on the binary data type, mention:
The permissible maximum length is the same for BINARY and VARBINARY as it is for CHAR and VARCHAR, except that the length for BINARY and VARBINARY is a length in bytes rather than in characters.
So binary is put on the same level as char, and varbinary as varchar.
The docs on the char data type, mention:
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255.
So the maximum size for binary is therefore achieved with this:
CREATE TABLE mytable (
id int,
representation binary(255)
)
This corresponds to 255 bytes of data, which corresponds to 510 hexadecimal digits, or 2040 bits.
VARBINARY
The varbinary type can store up to 65,535 bytes, from which the sizes of the other columns must be subtracted. Again, this follows from the docs on varchar:
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
So let's say you would need room for about 500 bytes in other columns, then you could defined this table:
CREATE TABLE mytable (
id int, // takes 4 bytes
representation binary(65000),
// other fields come here, taking up less than 532 bytes
)
... you would have 65,000 bytes, i.e. 130,000 hexadecimal digits or 520,000 bits.
SQL Server Binary
The Transact-SQL docs on binary state:
binary [ ( n ) ]
Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes.
This means that with this table definition:
CREATE TABLE mytable (
id int,
representation binary(8000)
)
... you can store 8,000 bytes, i.e. 16,000 hexadecimal digits or 64,000 bits.
Note that the limit for varbinary is the same. The following advise is given in the docs:
Use varbinary when the sizes of the column data entries vary considerably.

mysql int field growing bigger than 11 digits

I have a mysql database, and I am using it as a temprorarily store captcha values. It has a auto incremented id key, with int(11) field. What happens if this value gets bigger than 11 digits?
A typical INT uses 4 bytes, so it can store the numbers:
Signed: -2147483648 to 2147483647
Unsigned: 0 to 4294967295
A BIGINT uses 8 bytes, so it can store the numbers:
Signed: -9223372036854775808 to 9223372036854775808
Unsigned: 0 to 18446744073709551615
This number (11) has absolutely nothing to do with column range - [SIGNED] INTEGER defines a range (-2147483648 - 2147483647).
Number within parentheses is being used only when combined with ZEROFILL. Then it defines the "length" of displayed number, ie. value 275552 will be returned as string 00000275552.
As others have said, you won't get 11 digits. When you reach the maximum value, all values exceeding it will be treated as if they were the maximum value. In the case of auto_increment fields, it will cause all future inserts to fail until you change the column type to a BIGINT:
ERROR 1062 (23000): Duplicate entry '4294967295' for key 1
int(11) will accept only first 11 digits. rest of them will be discarded.
You can modify your table and change your id datatype from int to bigint.
Hope this helps.
When you reach the maximum value, all values exceeding it will be treated as if they were the maximum value. In the case of auto_increment fields, it will cause all future inserts to fail until you change the column type to a BIGINT: