在 MySQL 中存储货币值的最佳数据类型
我想在 MySQL 数据库中存储许多记录.它们都包含货币价值.但是我不知道每个数字会插入多少个数字.
为此,我必须使用哪种数据类型?
VARCHAR 还是 INT(或其他数字数据类型)?
I want to store many records in a MySQL database. All of them contains money values. But I don't know how many digits will be inserted for each one.
Which data type do I have to use for this purpose?
VARCHAR or INT (or other numeric data types)?
推荐答案
由于货币需要精确表示,因此不要使用像 float
这样仅近似的数据类型.您可以为此使用定点数字数据类型
Since money needs an exact representation don't use data types that are only approximate like float
. You can use a fixed-point numeric data type for that like
decimal(15,2)
15
是精度(值的总长度包括小数位)2
是小数点后的位数15
is the precision (total length of value including decimal places)2
is the number of digits after decimal point
参见 MySQL 数字类型:
这些类型用于保持精确精度很重要的情况,例如货币数据.
These types are used when it is important to preserve exact precision, for example with monetary data.
相关文章