Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
MySQL Numeric Types include types for integer, fixed point and floating point real numbers. Integer types are used for storing numbers that don't have a fractional part. There are five integer types in MySQL and they differ only on the scale of the values that they represent. This table shows the storage requirements and scale for the various integer types. Note that int, is an alias for integer so when you see int or integer they mean exactly the same thing. The SQL standard is integer, but MySQL like most database systems recognizes int as the same type.
The decimal type is used for fixed precision values. The type declaration for decimal takes two parameters. The first parameter is the precision that specifies the number of digits that will be stored. This number includes the digits after the decimal point. The second parameter is the scale, this specifies the position of the decimal point. Zero means no decimal point. For example, this declaration has a 9 for precision and a 2 for scale, so there are nine significant digits. And the decimal point is in the second position.
Numeric is an alias for decimal and does exactly the same thing. The default values for decimal precision and scale are 10 and 0, so decimal by itself. Is the same as decimal with a ten and a zero for its parameters. MySQL provides two floating point types, float and double. The float type is a standard IEEE 32-bit floating point number with a range of plus or minus 3.4 to the 38th power at a precision of 24 bits or about seven digits. It's worth noting that the real type is an alias for float, so when you see real, it's exactly the same as float.
The double type is a standard IEEE 64-bit floating point number with a range of plus or minus 1.79 to the 308th power. At a precision of 53 bits or about 16 digits. Floating point numbers are capable of representing very large or very small values, but in order to do so in a reasonable amount of space, they sacrifice precision. It's important to understand these limitations as they can have significant effect on calculations. To illustrate.
Lets select the scratch database and there's a table in here called numerics. So if I say describe numerics you'll see that it looks like this. It has five columns, two decimal numbers and two float numbers. And the decimal numbers are 10 and 2 so they have a precision of ten significant digits and the decimal point is in the second position. So if I say SELECT. Asterisk FROM numerics, like that.
You see we have two decimal numbers and two floating point numbers, and their values are 0.1 and 0.2, and 0.1 and 0.2. So there's an important distinction here, that you're going to need to see. If I say SELECT. Da plus db, you would expect that to be 0.3, right? Because 0.1 plus 0.2 equals 0.3. And the same with fa plus fb FROM numerics. And you'll notice that we get very different results.
The decimal numbers add up to 0.3 just like we would expect because. They have guaranteed precision. But the floating point numbers do not. You notice I add fa plus fb, 0.1 plus 0.2 and I get 0.3 with some garbage after a few zeros after the decimal point. So, in fact if I do this, da plus db equals 0.3, I would expect this to be true, which would be a 1.
So I get a 1 there, right? And if I do the same with fa and fb and say go, then I get a 0 which is false. In this case 0.1 plus 0.2 does not equal 0.3. Clearly it's reasonable to expect 0.1 and 0.2 to be equal to 0.3 but clearly the floating point numbers don't work that way. And in fact, if we just look at what that result is, we see that it's this other number. It's close to 0.3 but it's not exactly.
So this is a not a limitation of MySQL, this is a limitation of all floating point numbers. Because floating point numbers sacrifice precision for scale. To illustrate this point a little more dramatically, this is a photograph that I took at a local movie theater recently. This is a cash register. And I had purchased a number of items and they added them all up. And the total came to 13.50001. And you'll notice that it ruined the formatting on their screen because their screen was formatted for a certain amount of digits and there were some extra digits there.
This is as the sign says why you never use float for money. I gladly pay the extra thousandth of a cent if I had a convenient way of slicing up the pennies accurately. MySQL has a rich set of numeric types including integers, fixed point, and floating point numbers. Keep in mind that floating point numbers are not well suited for calculating money. Use the fixed point decimal type for financial applications.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 100782 Viewers
61 Video lessons · 87640 Viewers
71 Video lessons · 71497 Viewers
56 Video lessons · 103424 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.