mysql float data field not accepting every float number - mysql

I have a float data field :
`total` float(20,2) unsigned NOT NULL,
I want to insert a float number like : "815032.68" but it's not accepted and the number is rounded to 815032.69. why?

Use a fixed-point data type
`total` DECIMAL(20,2) unsigned NOT NULL
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.
MySQL Doc
Floating-point data types can only store approximate numbers.

Floats are 32 bit numbers stored as mantissa and exponents,the maximum value a FLOAT can have is +8388608*10^127 and the minimum is -8388608*10^127. This means only 7 significant digits, and your FLOAT definition used 20.Use double data type.

Related

MySQL Compute int size if knowing the max ID

I want to convert a CSV database into a MySQL one, I know I will never add any new row in the database tables. I know the max ID of each table, for example : 9898548.
What should be the proper way to compute the int size ? Does a CEIL(LOG2(last_id)) could be sufficient for this ? With my example, it would be LOG2(9898548) = 23.2387 so int(24) ? is this correct ?
When you're defining your table and you know your max values you can refer to the max table sizes. See http://dev.mysql.com/doc/refman/5.7/en/integer-types.html for a table of numeric sizes.
IDs are usually positive so you can use the unsigned numbers. In your case 9898548 is less than 16777215 (the unsigned MEDIUMINT max value) so that would be the most space efficient storage option. So your calculation is correct. You need 24 bits or 3 bytes, or a UNSIGNED MEDIUMINT.
CREATE TABLE your_table (id UNSIGNED MEDIUMINT PRIMARY KEY);
The brackets with numbers inside are to help MySQL display the number correctly, they don't do anything to the storage size. So INT(11) and INT (24), can both sure the same range of numbers. But the one defined INT (11) will only display a number with a column width of equivalent to 11 digits even if the number is smaller. See
http://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html
"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"
Yes, in this case, you need an integer type with a least 24 bits (equals 3 bytes). The smallest in MySQL satisfying this is UNSIGNED MEDIUMINT, according to the documentation.
Edit: Added the UNSIGNED.

MySQL different integer datatype operation

There are different datatypes used to build a column in MySQL table. For example SMALLINT, MEDIUMINT, and INT. I understand that different datatype will affect table size according to column types. Therefore, its unnecessary to assign UNSIGNED INT to a value which ranges between 0-1000.
What happens if you multiply values of different datatypes in MySQL (e.g. SMALLINT * INT)? What MySQL datatype do you need to store the result?
MySQL defines some rules for arithmetic operations.
In case of +, -, * result is calculated to BIGINT (64-bit) precision if both operands are integers (e.g. SMALLINT, INT)
If both operands are integer and any of them are UNSIGNED, the result is an unsigned integer. For subtraction (-) if NO_UNSIGNED_SUBTRACTION SQL mode is enabled, the result will be signed.
If any of the operands of a +, -, /, *, % is real (e.g. FLOAT or DOUBLE) or string value (e.g. '10.13e-3') the precision of the result is the precision of the operand with the maximum precision.
It is safe to do arithmetic operations between columns with different datatypes. MySQL will automatically convert into appropriate format.
As for storing its result, there are some constraints.
If out-of-range value is inserted, largest endpoint will be stored (e.g. inserting 130 to TINYINT will store 127 and raise warning). If strict mode is enabled, insertion will fail and raise error.
Integer overflow results in silent wraparound (e.g. 9223372036854775807 + 1 = -9223372036854775808. This is because its the largest possible operation using BIGINT datatype).
When a floating-point or fixed-point column is assigned a value that exceeds the range implied by the specified (or default) precision and scale, MySQL stores the value representing the corresponding endpoint of that range
Floating-point overflow produces NULL result. Some operation can result in +INF, -INF or `NaN'.
DECIMAL datatype, if overflowed, will be truncated. And raise warning.
References (from dev.mysql.com):
Out of range & Overflow
Arithmetic functions

Out of range value for column 'contact_no' at row 1

I was trying to add a number with a length of 11 but when I input it in the database it can't. If I try to add a number with a length of 10 it can.
This is the error:
ERROR 1264: 1264: Out of range value for column 'contact_no' at row 1
SQL Statement:
INSERT INTO `mcs`.`new_table` (`id`, `contact_no`) VALUES ('1', '12345678901')
It's not clear what question you are asking.
Why is this error being returned?
Likely, the contact_no column is declared with datatype of INT, a 32-bit integer, and you attempted to assign a value larger than the supported maximum value.
The maximum value for a signed 32-bit integer is 2,147,483,647. (that decimal value of 2^31-1. For an unsigned 32-bit integer, maximum value is 2^32.)
Some other question that wasn't asked
If you need to store values larger than the maximum supported by the INT datatype, you could define the column as a different datatype.
For example, BIGINT gives a maximum value of 2^63-1 for unsigned.
If you need a maximum of 12 decimal digits, you could use a DECIMAL(12) datatype.
Change your data type of contact_no to BIGINT.
Check range of different data type at MYSQL official website.
I personally recommend you to use varchar, as you don't need to compare contact number with any field.
Please check datatype of contact_no column.
'12345678901' is exceeding its size.

Mysql won't store number larger than 9223372036854775808 with bigint

I'm hoping that there is a simple fix for this. I have a database column in which I store a number. I knew that the numbers would get pretty big, so I set the field to 'bigint'. However it will not store a number larger than 9223372036854775808.
Why is this?
Quoting from the manual:
BIGINT[(M)] [UNSIGNED] [ZEROFILL]
A large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.
You've hit the bigint maximum size. This is a limitation due to the way the number is stored on the computer. It's the maximum size number you can represent with 8 bytes.
If you need to store a bigger number, consider using another method. You could use varchars but you will need to convert if you're doing math operations on it.
I think you're using a signed BIGINIT:
Range of a signed bigint: -9223372036854775808 - 9223372036854775807
Range of an unsigned bigint: 0- 18446744073709551615
Use ALTER TABLE to modify the column:
ALTER TABLE t1 MODIFY col1 BIGINT UNSIGNED;

MYSQL inserting wrong info

I am inserting value
insert into user (name,fbid) Values ('Adi Mathur',100000564553314)
but in the database i see the value if fbid to be
2147483647
Why ? How should i fix it ?
fbid int(50)
As explained in the manual, the maximum value of a (signed) INT, which occupies 4 bytes/32 bits, is 2,147,483,647; for integer data types, the number in parenthesis is the display width, which only affects the way that the data is displayed, not how much space it is allocated for storage:
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 (the precision). For string types, M is the maximum length. The maximum permissible value of M depends on the data type.
You probably want a BIGINT.