MySQL - FLOAT: A Beginner's Guide

Hello there, aspiring database enthusiasts! Today, we're diving into the world of MySQL and exploring a fascinating data type called FLOAT. Don't worry if you're new to programming – I'll be your friendly guide through this journey, explaining everything step by step. So, let's get started!

MySQL - FLOAT

The MySQL FLOAT Data Type

What is FLOAT?

FLOAT is a data type in MySQL used to store decimal numbers. It's particularly useful when you need to work with numbers that have fractional parts, like prices, measurements, or scientific calculations.

Why use FLOAT?

Imagine you're running a fruit stand. You sell apples for $1.25 each. You can't use a whole number to represent this price, right? That's where FLOAT comes in handy!

Basic Syntax

Here's how you declare a FLOAT column in a MySQL table:

CREATE TABLE fruits (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    price FLOAT
);

In this example, we've created a table called 'fruits' with three columns: id, name, and price. The price column uses the FLOAT data type.

Inserting FLOAT Values

Let's add some fruits to our table:

INSERT INTO fruits (id, name, price) VALUES
(1, 'Apple', 1.25),
(2, 'Banana', 0.99),
(3, 'Orange', 1.50);

Here, we're inserting three fruits with their respective prices as FLOAT values.

Other Representations of MySQL FLOAT

MySQL offers different ways to represent floating-point numbers. Let's explore them!

FLOAT(M,D)

You can specify the precision of a FLOAT by using FLOAT(M,D):

  • M: Total number of digits
  • D: Number of digits after the decimal point

Example:

CREATE TABLE products (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    weight FLOAT(5,2)
);

INSERT INTO products (id, name, weight) VALUES
(1, 'Laptop', 2.35),
(2, 'Smartphone', 0.18);

In this case, weight FLOAT(5,2) means we can store numbers up to 999.99.

FLOAT vs. DOUBLE

FLOAT is a single-precision floating-point number, while DOUBLE is a double-precision floating-point number. DOUBLE is more precise but takes up more storage space.

Example:

CREATE TABLE measurements (
    id INT PRIMARY KEY,
    float_value FLOAT,
    double_value DOUBLE
);

INSERT INTO measurements (id, float_value, double_value) VALUES
(1, 1.12345678901234567890, 1.12345678901234567890);

SELECT * FROM measurements;

You'll notice that the DOUBLE column retains more decimal places than the FLOAT column.

Float Datatype Using a Client Program

Now, let's see how we can work with FLOAT data types using a MySQL client program.

Using MySQL Workbench

  1. Connect to your MySQL server using MySQL Workbench.
  2. Create a new schema (database) called 'floatexamples'.
  3. Run the following queries:
USE floatexamples;

CREATE TABLE temperatures (
    id INT PRIMARY KEY,
    city VARCHAR(50),
    temp_celsius FLOAT(5,2)
);

INSERT INTO temperatures (id, city, temp_celsius) VALUES
(1, 'New York', 25.5),
(2, 'London', 18.3),
(3, 'Tokyo', 30.1);

SELECT * FROM temperatures;

This will create a table of city temperatures using FLOAT values.

Performing Calculations

FLOAT values are great for calculations. Let's convert our temperatures to Fahrenheit:

SELECT 
    id, 
    city, 
    temp_celsius, 
    (temp_celsius * 9/5) + 32 AS temp_fahrenheit
FROM temperatures;

This query will show both Celsius and Fahrenheit temperatures.

Rounding FLOAT Values

Sometimes, you might want to round FLOAT values. MySQL provides functions for this:

SELECT 
    id, 
    city, 
    temp_celsius, 
    ROUND(temp_celsius, 1) AS rounded_celsius
FROM temperatures;

This will round the temperatures to one decimal place.

Common FLOAT Functions

Here's a table of useful MySQL functions for working with FLOAT values:

Function Description Example
ROUND() Rounds a number to a specified number of decimal places ROUND(3.14159, 2) returns 3.14
CEIL() Returns the smallest integer value not less than the argument CEIL(3.14) returns 4
FLOOR() Returns the largest integer value not greater than the argument FLOOR(3.14) returns 3
ABS() Returns the absolute value of a number ABS(-3.14) returns 3.14
TRUNCATE() Truncates a number to a specified number of decimal places TRUNCATE(3.14159, 2) returns 3.14

Conclusion

And there you have it, my dear students! We've explored the world of FLOAT in MySQL, from basic concepts to practical applications. Remember, working with decimal numbers is crucial in many real-world scenarios, from financial calculations to scientific data analysis.

As you continue your MySQL journey, keep experimenting with FLOAT and other data types. The more you practice, the more comfortable you'll become. And who knows? Maybe one day you'll be creating the next big database-driven application!

Happy coding, and may your queries always return the results you expect!

Credits: Image by storyset