MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL provides several data types for storing decimal and floating point numbers:
FLOAT: The FLOAT data type is used to store single-precision floating point numbers. This means they have around 7 digits of precision. They're stored as 4 bytes.
Example:
CREATE TABLE example ( float_column FLOAT(7, 2) );
The (7,2) means that this number can be up to 7 digits long, with 2 digits after the decimal.
To insert data:
INSERT INTO example (float_column) VALUES (12345.67);
DOUBLE: The DOUBLE data type is used to store double-precision floating point numbers. This means they have around 15 digits of precision. They're stored as 8 bytes.
Example:
CREATE TABLE example ( double_column DOUBLE(15, 2) );
The (15,2) means that this number can be up to 15 digits long, with 2 digits after the decimal.
To insert data:
INSERT INTO example (double_column) VALUES (123456789012345.67);
DECIMAL: The DECIMAL data type is used to store exact decimal numbers. Unlike FLOAT and DOUBLE, which can have rounding errors due to the nature of floating-point arithmetic, DECIMAL will store exactly the number you put into it. This makes it ideal for financial and monetary data.
Example:
CREATE TABLE example ( decimal_column DECIMAL(10, 2) );
The (10,2) means that this number can be up to 10 digits long, with 2 digits after the decimal.
To insert data:
INSERT INTO example (decimal_column) VALUES (1234567.89);
It's worth mentioning that while FLOAT and DOUBLE can store very large or very small numbers (because they're floating point numbers), they're not always exact, and this could lead to rounding errors. On the other hand, DECIMAL is used for exact values where precision is required, such as monetary data.
CREATE TABLE numeric_table ( id INT PRIMARY KEY, float_column FLOAT, double_column DOUBLE, decimal_column DECIMAL(10, 2) );
CREATE TABLE float_table ( id INT PRIMARY KEY, float_value FLOAT(8, 4) );
CREATE TABLE decimal_table ( id INT PRIMARY KEY, decimal_value DECIMAL(10, 5) );
SELECT 1.1 * 1.1; -- Potential precision loss in FLOAT and DOUBLE SELECT DECIMAL(1.1, 2) * DECIMAL(1.1, 2); -- Precise arithmetic with DECIMAL
CREATE TABLE double_table ( id INT PRIMARY KEY, double_value DOUBLE(15, 10) );
CREATE TABLE financial_table ( id INT PRIMARY KEY, amount DECIMAL(10, 2), interest_rate FLOAT );
CREATE TABLE accuracy_table ( id INT PRIMARY KEY, float_val FLOAT, double_val DOUBLE );
CREATE TABLE exact_numeric_table ( id INT PRIMARY KEY, value DECIMAL(8, 4) );
CREATE TABLE precision_table ( id INT PRIMARY KEY, precise_value DECIMAL(12, 6) );
CREATE TABLE currency_table ( id INT PRIMARY KEY, total_amount DECIMAL(10, 2), exchange_rate DOUBLE );