Auto Rounding SQL d.

huangapple go评论55阅读模式
英文:

Auto Rounding SQL d

问题

MySQL 中有一个数据库,其中包含课程和价格(DECIMAL 10.0)。问题在于,当我添加一个价格,例如12.5时,SQL 将其转换为13。为什么会发生这种情况?

英文:

I have a database on mySQL, with course and prices(DECIMAL 10.0). The problem is when I add a price, eg 12.5, sql converts to 13.
Why this is happened?

答案1

得分: 1

要定义数据类型为DECIMAL的列,您可以使用以下语法:

column_name DECIMAL(P, D)

其中,P表示小数点左侧的数字位数,而D表示小数点右侧的数字位数。

因此,在您的示例中 prices(DECIMAL 10.0) 中,

P为10,D为0。

因此,在您的列prices中,小数点左侧始终有10位数字,小数点右侧有0位数字。

要更新prices列以允许更多小数点右侧的数字,语法如下:

alter table table_name modify column prices DECIMAL(10,4);
英文:

To define a column whose data type is DECIMAL you use the following syntax:

column_name DECIMAL(P, D)

Where P is the precision that represents the number of digits to the left of the decimal, & D is the scale that represents the number of digits to the right of the decimal

Hence, in your example prices(DECIMAL 10.0)

P is 10 and D is 0.

Therefore, in your column prices, there will always be 10 digits to the left of the decimal, and 0 to the right of the decimal.

To update the prices column to allow more digits to the right of the decimal, the syntax is as follows:

alter table table_name modify column prices DECIMAL(10,4);

huangapple
  • 本文由 发表于 2020年7月29日 05:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63143048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定