The DECIMAL data type is used in MySQL to store fixed-precision decimal numbers to avoid precision loss. The syntax is DECIMAL(M, D), where M is the maximum number of digits and D is the number of digits to the right of the decimal point. Features include accuracy, fixed length, and data integrity. Suitable for data that requires precise calculations, precise measurements, or where specific accuracy and range are enforced. Compared to FLOAT and DOUBLE, DECIMAL has higher precision, but is slower and has a fixed length.
Usage of DECIMAL data type in MySQL
The DECIMAL data type is used to store fixed-precision decimal numbers. It differs from the FLOAT and DOUBLE types, which use floating-point arithmetic, which may result in a loss of precision.
Syntax
<code>DECIMAL(M, D)</code>
For example:
<code>DECIMAL(10, 2)</code>
This data type can store decimal numbers with a maximum value of 99999999.99, where 8 digits to the left of the decimal point and 2 digits to the right.
Features
When to use it
Use the DECIMAL data type for the following situations:
Comparison with FLOAT and DOUBLE
DECIMAL | FLOAT | DOUBLE | |
---|---|---|---|
Accurate | Approximate value | Approximate value | |
Fixed | variable | variable | ##speed |
faster | faster |
The following is an example using the DECIMAL data type:
<code class="sql">CREATE TABLE orders ( order_id INT NOT NULL AUTO_INCREMENT, total_price DECIMAL(10, 2) NOT NULL ); INSERT INTO orders (total_price) VALUES (123.45); SELECT * FROM orders WHERE total_price > 100.00;</code>
The above is the detailed content of decimal usage in mysql. For more information, please follow other related articles on the PHP Chinese website!