Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Numeric types

From: MySQL Essential Training

Video: Numeric types

MySQL Numeric Types include types for integer, The default values for decimal precision and scale You see we have two decimal numbers and two floating point MySQL has a rich set of numeric types

Numeric types

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.

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 6399 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.