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)?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
I prefer to use
BIGINT
, and store the values in by multiply with 100, so that it will become integer.For e.g., to represent a currency value of
93.49
, the value shall be stored as9349
, while displaying the value we can divide by 100 and display. This will occupy less storage space.It depends on your need.
Using
DECIMAL(10,2)
usually is enough but if you need a little bit more precise values you can setDECIMAL(10,4)
.If you work with big values replace
10
with19
.Indeed this relies on the programmer's preferences. I personally use:
numeric(15,4)
to conform to the Generally Accepted Accounting Principles (GAAP).If your application needs to handle money values up to a trillion then this should work: 13,2 If you need to comply with GAAP (Generally Accepted Accounting Principles) then use: 13,4
Usually you should sum your money values at 13,4 before rounding of the output to 13,2.
You can use
DECIMAL
orNUMERIC
both are samei.e.
DECIMAL(10,2)
Good read
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 like15
is the precision (total length of value including decimal places)2
is the number of digits after decimal pointSee MySQL Numeric Types: