Best data type to store money values in MySQL(在 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.
这篇关于在 MySQL 中存储货币值的最佳数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中存储货币值的最佳数据类型


基础教程推荐
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01