MySQL out of range value for decimal column

2019-09-14 22:05发布

问题:

After searching, I found plenty of out of range problems with people not knowing that the first digit, m, in decimal(m,n) is the total amount of digits. However, that is not my problem.

For the column in question I have the following:

Field | Type                   | Null | Key | Default | Extra
preco | decimal(50,2) unsigned | NO   |     | 0.00    |

The setting decimal(50,2) is way more than I really want or need. I really only want 10 digits total. Its a price, so anything over 100 million is probably ridiculous, so decimal(10,2) would probably be more appropriate. However, MySQL Is not accepting the following query:

INSERT INTO `produto` (`tipo_id`, `preco`, `qtd`, `opcao1`) VALUES (110, '77888555.43', '10', 'Azul')

I tried the query through CodeIgniter, phpMyAdmin and directly in the MySQL command line client. I also tried it without the quotes on the decimal value but I always get the same error:

"Out of range value for column 'preco' at row 2"

回答1:

I have tried it with SQLyog, it is working perfect. I tried to change both ways in SQLyog directly through Table Data tab and using Query. It is just working. it seems there is no problem in Decimal Range, but problem is some where else. See the screenshots attached

Directly through Table Data Tab

Through insert query

Table Structure

Hope it help...



回答2:

You are sending the value for preco as a string which is accepted by MySQL in the same way as a numerical value.