Database Reference
In-Depth Information
The way that you define a decimal datatype may make more sense if you realize that the
number is stored as a string. If you are not aware of what a string is, we will get to that in
the next section. The decimal declaration defines a certain string length, and all numbers
that are stored have to conform within that length, hence the rounding issues when you put
in a number that falls out of range into one of these types.
This all may seem very complicated but DECIMAL is the only way that you can accu-
rately store a number with a fractional part. You probably will only use it to store currency,
but it is the best way of doing this.
Floating Point Numbers
The datatypes that have previously been mentioned are for storing exact values, either
exact whole numbers or exact whole and fractions of numbers. Floating point numbers
enable you to store a very wide range of numbers but with some levels of inaccuracy. The
bigger the number gets, the less detail is stored about it. The whole topic of storing and
dealing with floating point numbers is a large one which this topic will not deal with.
I need to warn you now that even though I will not be going into great detail in the fol-
lowing sections, they can get a bit complex. I would not want you to get so confused with
this section that you think that MySQL itself is too hard to learn - it is not - but these
datatypes are! If you do not intend to store very large numbers in your databases you might
just jump ahead to the character types, remembering that this section is here should you
ever need to refer to it in a moment of courage later on!
FLOAT
MySQL supports floating point numbers with the FLOAT datatype. You can specify the pre-
cision of a floating point number as follows:
columnname FLOAT(precision)
precision in this instance is the number of digits that are to be stored. If you store up to
24 you are declaring a single precision number and between 25 to 53 it becomes a double
precision number.
You can also specify a FLOAT with two attributes as follows:
columnname FLOAT(magnitude, decimals)
With this type of declaration the float is forced to be of the single precision type. The
attributes are as follows:
magnitude is the number of digits used to store the number.
decimals is the number of decimal digits to be stored.
Table 3.3 shows some examples of numbers that have been stored using the FLOAT(5, 2)
declaration. You will notice that it stores 3.141 59 and the smaller numbers accurately, with
only the number of decimal places being rounded where specified. However, look at the
larger numbers at the bottom of the table; as these have gone out of the precision range
Search WWH ::




Custom Search