Introduction to MySQL DOUBLE Data Type

The MySQL DOUBLE data type is used to store double-precision floating-point numbers, which are 64-bit (8-byte) IEEE floating-point numbers. The DOUBLE data type allows for a wider range of values to be stored compared to the FLOAT data type, with a maximum value of 1.7976931348623157E+308 and a minimum value of -1.7976931348623157E+308.

Syntax

When creating a table, you can use the following syntax to define a column with DOUBLE data type:

column_name DOUBLE(precision, scale)

Where precision is optional and represents the total number of digits in the numeric value, and scale is optional and represents the number of digits after the decimal point. If precision and scale are not specified, the default is DOUBLE(10,2).

Use Cases

The DOUBLE data type is typically used to store numeric values that require high precision, such as scientific calculations, financial data, and so on.

Examples

Here are two examples of using the DOUBLE data type:

Example 1

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  price DOUBLE(10,2)
);

INSERT INTO products (id, name, price) VALUES
(1, 'Product A', 19.99),
(2, 'Product B', 12.49);

The above example creates a table named products with three columns: id, name, and price. The data type of the price column is DOUBLE(10,2), which indicates that the maximum value is 99999999.99 and the minimum value is -99999999.99. The INSERT statement is used to insert the prices of two products into the table.

Example 2

SELECT SUM(price) FROM products;

The above example demonstrates how to perform aggregate operations using the DOUBLE data type. The SUM function is used to calculate the total sum of the price column and return a double-precision floating-point number.

Conclusion

The DOUBLE data type is used to store double-precision floating-point numbers with high precision. It is commonly used for storing numeric values that require high precision, such as scientific calculations, financial data, and so on. When creating a table, you can use the DOUBLE(precision, scale) syntax to define a column with DOUBLE data type.