Introduction to MySQL FLOAT Data Type

FLOAT is a numeric data type in MySQL used to store single-precision floating-point numbers (4 bytes). In MySQL, the range of FLOAT is from -3.402823466E+38 to -1.175494351E-38, 0, and from 1.175494351E-38 to 3.402823466E+38.

Syntax

The syntax for FLOAT in MySQL is as follows:

FLOAT[(M,D)]

where M represents the total number of digits, and D represents the number of digits after the decimal point. If D is omitted, it defaults to 0.

Use Cases

FLOAT data type is suitable for storing numbers that require decimal precision, such as temperature, weight, price, etc. It can provide higher precision and a wider range compared to integer types, which can only store integer values.

Examples

Here are two examples of using FLOAT data type:

Example 1

CREATE TABLE products (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    price FLOAT(8,2) NOT NULL,
    PRIMARY KEY (id)
);

INSERT INTO products (name, price) VALUES
    ('Product A', 12.99),
    ('Product B', 29.95),
    ('Product C', 8.75);

In this example, we create a products table with a price column of FLOAT data type, with 2 decimal places. We insert three products and specify a price for each product.

Example 2

SELECT * FROM orders WHERE total_price > 100.00;

In this example, we select orders from the orders table where the total price is greater than 100.00. If the total_price column uses FLOAT data type, it can retain decimal places and provide higher precision.

Conclusion

FLOAT is a MySQL data type suitable for storing numbers with decimal precision. It provides higher precision and a wider range, but caution should be exercised regarding rounding errors. The appropriate numeric data type should be chosen based on specific needs in practical usage.