Sling Academy
Home/MySQL/DOUBLE and FLOAT data types in MySQL 8: Explained with examples

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

Last updated: January 26, 2024

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.

Next Article: DATE and TIME data types in MySQL 8: Explained with examples

Previous Article: MySQL 8: INT, TINYINT, SMALLINT, MEDIUMINT, BIGINT data types – explained with examples

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples