如何使用MySQL DECIMAL? [英] How to use MySQL DECIMAL?

查看:221
本文介绍了如何使用MySQL DECIMAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解MySQL的DECIMAL.我需要该行能够包含一个从00.0001到99.9999的数字.我如何构造它才能像这样工作?

I can't quite get a grasp of MySQL's DECIMAL. I need the row to be able to contain a number anywhere from 00.0001 to 99.9999. How would I structure it to work like so?

推荐答案

DOUBLE列与DECIMAL列相同,如果使用会遇到麻烦财务数据的双栏.

DOUBLE columns are not the same as DECIMAL columns, and you will get in trouble if you use DOUBLE columns for financial data.

DOUBLE实际上只是 FLOAT 的双精度(64位而不是32位)版本. .浮点数是实数的近似表示,并不精确.实际上,像0.01这样的简单数字在FLOAT或DOUBLE类型中没有确切的表示形式.

DOUBLE is actually just a double precision (64 bit instead of 32 bit) version of FLOAT. Floating point numbers are approximate representations of real numbers and they are not exact. In fact, simple numbers like 0.01 do not have an exact representation in FLOAT or DOUBLE types.

DECIMAL列是精确的表示形式,但它们占用的空间更多,而可能的数字范围更小.要创建一个能够保存要求的0.0001至99.9999值的列,您将需要以下语句

DECIMAL columns are exact representations, but they take up a lot more space for a much smaller range of possible numbers. To create a column capable of holding values from 0.0001 to 99.9999 like you asked you would need the following statement

CREATE TABLE your_table
(
    your_column DECIMAL(6,4) NOT NULL
);

列定义遵循DECIMAL(M,D)格式,其中 M 是最大位数( precision )和 D 是小数点右边的位数( scale ).

The column definition follows the format DECIMAL(M, D) where M is the maximum number of digits (the precision) and D is the number of digits to the right of the decimal point (the scale).

这意味着上一条命令创建了一个列,该列接受-99.9999到99.9999之间的值.您还可以创建一个UNSIGNED DECIMAL列,范围从0.0000到99.9999.

This means that the previous command creates a column that accepts values from -99.9999 to 99.9999. You may also create an UNSIGNED DECIMAL column, ranging from 0.0000 to 99.9999.

有关MySQL DECIMAL的更多信息,请官方文档总是很好的资源.

For more information on MySQL DECIMAL the official docs are always a great resource.

请记住,所有这些信息对于MySQL 5.0.3及更高版本都是正确的.如果您使用的是以前的版本,则应该升级.

Bear in mind that all of this information is true for versions of MySQL 5.0.3 and greater. If you are using previous versions, you really should upgrade.

这篇关于如何使用MySQL DECIMAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆