DOUBLE and FLOAT data types in MySQL 8: Explained with examples

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Floating-point numbers provide a way to represent real numbers in a computer by approximating real numbers within a certain precision. In MySQL, floating-point numbers can be represented using FLOAT and DOUBLE data types. This tutorial will explain the differences between these two data types and illustrate their usage with practical examples.

FLOAT Data Type

The FLOAT data type in MySQL is a single-precision floating-point number. It has a range of approximately 1.175E-38 to 3.402E+38, with a precision of up to 7 significant digits. FLOAT is typically used when you need to save space and precision requirements are not high.

CREATE TABLE example_float (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  value FLOAT(7,4)
);

In the above example, value is defined as a FLOAT with a precision of seven places and four places after the decimal point.

DOUBLE Data Type

The DOUBLE data type, also known as DOUBLE PRECISION, is a double-precision floating-point number. It has a range of approximately 2.225E-308 to 1.797E+308, with a precision of up to 16 significant digits. Use DOUBLE when you need more precision than FLOAT.

CREATE TABLE example_double (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  value DOUBLE(16,8)
);

Here, value has a precision of 16 and eight digits after the decimal point.

Comparison Between FLOAT and DOUBLE

When deciding between FLOAT and DOUBLE, consider the level of precision and the amount of storage required. FLOAT occupies 4 bytes and DOUBLE occupies 8 bytes of storage. Therefore, DOUBLE is more precise but also takes more space.

Choosing Between FLOAT and DOUBLE

Use FLOAT when you have limited disk space and precision is tolerable within 6-7 digits. However, when handling more precise calculations like scientific computations, use DOUBLE.

Code Examples

Let’s explore some code examples to understand how to use these data types in MySQL.

Creating a Table with FLOAT and DOUBLE

CREATE TABLE measurements (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  temperature FLOAT(5,2),
  distance DOUBLE(15,10)
);

This example creates a table that stores temperature as FLOAT and distance as DOUBLE. Notice the different precision and scale definitions.

Inserting Data

INSERT INTO measurements (temperature, distance)
VALUES (23.45, 102345.6789012345);

Inserting data into FLOAT and DOUBLE columns works just like any other data type in MySQL.

Updating Data and Precision Loss

UPDATE measurements
SET temperature = 24.1234
WHERE id = 1;

When updating the temperature in this example, the value is truncated to fit the defined precision of FLOAT(5,2), i.e., the stored value would become 24.12.

Selecting Data

SELECT id, temperature, distance
FROM measurements;

Select queries for FLOAT and DOUBLE data columns are straightforward; however, the output might show rounded values based on the column definition.

Notes: Certain functions and scenarios may behave differently with FLOAT and DOUBLE data types. Pay special attention to these cases and test your SQL queries to ensure accuracy in your applications.

Conclusion

This guide provided an overview of FLOAT and DOUBLE data types in MySQL 8 and should help you decide which to use in your own database designs, with consideration given to precision requirements and storage implications. Code examples highlighted how to create tables, insert data, and manage precision.