The DECIMAL and
NUMERIC types store exact numeric
data values. These types are used when it is important to
preserve exact precision, for example with monetary data. In
MySQL, NUMERIC is implemented as
DECIMAL, so the following remarks
about DECIMAL apply equally to
NUMERIC.
Through version 4.1, MySQL stores
DECIMAL values as strings, rather
than in binary format. One character is used for each digit of
the value, the decimal point (if the scale is greater than 0),
and the “-” sign (for negative
numbers).
In a DECIMAL column declaration,
the precision and scale can be (and usually is) specified; for
example:
salary DECIMAL(5,2)
In this example, 5 is the precision and
2 is the scale. The precision represents the
number of significant digits that are stored for values, and the
scale represents the number of digits that can be stored
following the decimal point.
Standard SQL requires that DECIMAL(5,2) be
able to store any value with five digits and two decimals, so
values that can be stored in the salary
column range from -999.99 to
999.99. In versions up to and including 4.1,
MySQL varies from this limit in two ways due to the use of
string format for value storage:
On the positive end of the range, the column actually can
store numbers up to 9999.99. For positive
numbers, MySQL uses the byte reserved for the sign to extend
the upper end of the range.
DECIMAL columns in MySQL
before 3.23 are stored differently and cannot represent all
the values required by standard SQL. This is because for a
type of
DECIMAL(,
the value of M,D)M includes the bytes
for the sign and the decimal point. The range of the
salary column before MySQL 3.23 would be
-9.99 to 99.99.
In standard SQL, the syntax
DECIMAL( is
equivalent to
M)DECIMAL(.
Similarly, the syntax M,0)DECIMAL is
equivalent to
DECIMAL(,
where the implementation is permitted to decide the value of
M,0)M. As of MySQL 3.23.6, both of these
variant forms of DECIMAL syntax
are supported. The default value of M
is 10. Before 3.23.6, M and
D both must be specified explicitly.
If the scale is 0, DECIMAL values
contain no decimal point or fractional part.
The maximum range of DECIMAL
values is the same as for DOUBLE,
but the actual range for a given
DECIMAL column can be constrained
by the precision or scale for a given column. When such a column
is assigned a value with more digits following the decimal point
than are permitted by the specified scale, the value is
converted to that scale. (The precise behavior is operating
system-specific, but generally the effect is truncation to the
permissible number of digits.)

User Comments
Add your own comment.