MySQL:十进制数据类型的大小

时间:2021-11-15 00:50:51

I have a few values that come in from a server that need to be stored in my database. I'm not a MySQL expert, but I understand it well enough for basic input/output. Right now I am trying to figure out what length I should be using when storing the following decimals.

我有一些值来自需要存储在我的数据库中的服务器。我不是MySQL专家,但我对它的基本输入/输出非常了解。现在我试图找出存储以下小数时我应该使用的长度。

tax_rate [DECIMAL ?,?]: value(0.014840000000)
units [DECIMAL ?,?]: value(1.00)
initial_charge [DECIMAL ?,?]: value(2.5110)
charge [DECIMAL ?,?]: value(2.8967)
link_tax [DECIMAL ?,?]: value(0.385652)
exempt [DECIMAL ?,?]: value(0.0000)
tax [DECIMAL ?,?]: value(0.042986)
base_price [DECIMAL ?,?]: value(41.8500)

I'm hoping someone could suggest the correct data length I need to use for these values AS WELL explain why they chose the values. Or maybe link to an article that explains MySQL decimals in depth.

我希望有人可以建议我需要使用这些值的正确数据长度AS WELL解释他们为什么选择这些值。或者链接到一篇深入解释MySQL小数的文章。

Any help would be appreciated.

任何帮助,将不胜感激。

Thank you!

谢谢!

-------Edit--------

- - - -编辑 - - - -

After reading the mysql docs, this is what I've resolved the following decimals lengths to be:

阅读mysql文档后,这就是我已经解决了以下小数长度:

tax_rate [DECIMAL 15,12]: value(0.014840000000) ? max(999.999999999999)
units [DECIMAL 6,2]: value(1.00) ? max(9999.99)
initial_charge [DECIMAL 9,4]: value(2.5110) ? max(99999.9999)
charge [DECIMAL 9,4]: value(2.8967) ? max(99999.9999)
link_tax [DECIMAL 9,6]: value(0.385652) ? max(999.999999)
exempt [DECIMAL 9,4]: value(0.0000) ? max(9999.9999)
tax [DECIMAL 10,6]: value(0.042986) ? max(999999.999999)
base_price [DECIMAL 10,4]: value(41.8500) ? max(999999.9999)

1 个解决方案

#1


23  

From MySQL:

来自MySQL:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:

DECIMAL列的声明语法是DECIMAL(M,D)。 MySQL 5.1中参数的值范围如下:

M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

M是最大位数(精度)。它的范围是1到65.(较早版本的MySQL允许范围为1到254)。

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

D是小数点右边的位数(刻度)。它的范围为0到30,且不得大于M.

Consider this number: 123456789.12345 here M is 14 and D is 5 then based on this principle you can set DECIMALS(M,D) for each column based on Their expected maximum values.

考虑这个数字:123456789.12345这里M是14而D是5然后基于这个原则你可以根据它们的预期最大值为每列设置DECIMALS(M,D)。

#1


23  

From MySQL:

来自MySQL:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:

DECIMAL列的声明语法是DECIMAL(M,D)。 MySQL 5.1中参数的值范围如下:

M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

M是最大位数(精度)。它的范围是1到65.(较早版本的MySQL允许范围为1到254)。

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

D是小数点右边的位数(刻度)。它的范围为0到30,且不得大于M.

Consider this number: 123456789.12345 here M is 14 and D is 5 then based on this principle you can set DECIMALS(M,D) for each column based on Their expected maximum values.

考虑这个数字:123456789.12345这里M是14而D是5然后基于这个原则你可以根据它们的预期最大值为每列设置DECIMALS(M,D)。