英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论